如何将变量值从java类传递到jsp页面
我有 2 个名为 Admin.java
和 index.jsp
的文件。
在 Admin.java
中,我通过一个函数检索名为 res
的变量的值。该变量需要传递到 JSP 页面。
Admin.java
位于 C:\Users\praveen\workspace\SemanticWeb\src\controller
中,而 index.jsp
位于 >C:\Users\praveen\workspace\SemanticWeb\WebContent
。
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;
}
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。我无法在互联网上为我的代码获得任何适当的帮助。请有人帮我解决这个问题。
I have 2 files named Admin.java
and index.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.
The Admin.java
is in C:\Users\praveen\workspace\SemanticWeb\src\controller
whereas the index.jsp
is in C:\Users\praveen\workspace\SemanticWeb\WebContent
.
The code of Admin.java is:
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;
}
The code of index.jsp is
function login(user, pass)
{
$.ajax({
type:"GET",
url: "Admin?action=login",
dataType: "text",
data: { username: user, password: pass },
success: function(response){
}
within the
function(response)
{
......
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您的代码中,
您不应将其设置为请求属性。仅当您转发到 JSP 文件时,设置请求属性才有用。您需要自己直接将其写到回复中。将该行替换为
这样,它将最终出现在响应正文中,并可作为 JS 函数中的变量
response
使用。另请参阅:
From your code,
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
This way it'll end up in the response body and be available as variable
response
in your JS function.See also:
看起来您正在使用 AJAX,所以我想说您的响应需要以 AJAX 兼容的方式(JSON、XML,...)进行编码。
如果您进行 AJAX 编码,您的函数可能如下所示:
编辑:
我们使用 JSON Simple 用于 JSON 编码。
我们的 Java 后端看起来像这样(没有错误检查的简化版本):
在 Javascript 函数中:
Seems like you're doing AJAX, so I'd say your response would need to be encoded in an AJAX-compatible way (JSON, XML, ...).
If you do AJAX-encoding, your function might look like this:
Edit:
We're using JSON Simple for JSON encoding.
Our Java backend then looks like this (simplified version without error checking):
And in the Javascript function:
如果这是一个ajax请求,您可以将请求转发到另一个jsp页面而不是返回。 另一种方法是用
它替换你的 System.out.println
但我想这是一个不好的做法。请参阅此处示例。
If this is an ajax request, you can forward the request into another jsp page rather than return. With this
Another way is replace your System.out.println with this
but I guess this is a bad practice. See example here.