如何从一个Java类传递一个变量的值到JSP页面 [英] How to pass a value of a variable from a java class to the jsp page
问题描述
我有一个名为2个文件 Admin.java
和的index.jsp
。
I have 2 files named Admin.java
and index.jsp
.
在 Admin.java
通过函数我取回了名为 RES
的varible的价值。这个变量需要传递到JSP页面。
In Admin.java
through a function I retrieve the value of the varible named res
. This variable needs to be passed to a JSP page.
在 Admin.java
是 C:\ Users \用户普利文\工作区\ SemanticWeb的\ src \控制器
而在的index.jsp
是 C:\ Users \用户普利文\工作区\ SemanticWeb \的WebContent
的code Admin.java 是:
public Admin()
{
super();
}
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
if (action.equals("login"))
{
String userName="";
String password="";
userName = request.getParameter("username");
password = request.getParameter("password");
response.setCharacterEncoding("UTF-8");
SemanticSearch semsearch = new SemanticSearch(request.getSession());
semsearch.loadData(REALPATH + RDFDATASOURCEFILE1);
String res=semsearch.searchForUser(userName, password);
System.out.println("The value of res been passed is "+res);
request.setAttribute("rest", res);
return;
}
的code 的index.jsp 是
function login(user, pass)
{
$.ajax({
type:"GET",
url: "Admin?action=login",
dataType: "text",
data: { username: user, password: pass },
success: function(response){
}
在
function(response)
{
......
}
我需要访问的 RES
按 Admin.java通过
的值。我不能得到任何适当的帮助,我在互联网上code。请有人可以帮我这个。
I need to access the value of res
passed by Admin.java
. I am not able to get any proper help for my code on the Internet. Please can someone help me with this.
推荐答案
从code,
request.setAttribute("rest", res);
您不应该将它设置为请求属性。如果你转发到JSP文件设置请求属性才有用。你需要将它写入直接到自己的反应。通过
You shouldn't set it as request attribute. Setting request attributes is only useful if you're forwarding to a JSP file. You need to write it straight to the response yourself. Replace the line by
response.getWriter().write(res);
此方式,它最终会在响应正文和可作为变量响应
在你的JS函数。
This way it'll end up in the response body and be available as variable response
in your JS function.
这篇关于如何从一个Java类传递一个变量的值到JSP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!