如何将变量值从java类传递到jsp页面

发布于 2024-10-24 20:41:39 字数 1593 浏览 1 评论 0原文

我有 2 个名为 Admin.javaindex.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有 2024-10-31 20:41:39

从您的代码中,

request.setAttribute("rest", res);

您不应将其设置为请求属性。仅当您转发到 JSP 文件时,设置请求属性才有用。您需要自己直接将其写到回复中。将该行替换为

response.getWriter().write(res);

这样,它将最终出现在响应正文中,并可作为 JS 函数中的变量 response 使用。

另请参阅:

From your code,

request.setAttribute("rest", res);

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);

This way it'll end up in the response body and be available as variable response in your JS function.

See also:

断舍离 2024-10-31 20:41:39

看起来您正在使用 AJAX,所以我想说您的响应需要以 AJAX 兼容的方式(JSON、XML,...)进行编码。

如果您进行 AJAX 编码,您的函数可能如下所示:

function(response)
{
 var toplevel = response.<your_top_level_element>;
} 

编辑:

我们使用 JSON Simple 用于 JSON 编码。

我们的 Java 后端看起来像这样(没有错误检查的简化版本):

public String execute()
{
  JSONObject jsonResult = new JSONObject();

  jsonResult.put( "result", "ok");

  return jsonResult.toJSONString();
}

在 Javascript 函数中:

function(response)
{
 var result = response.result; //now yields "ok"
}

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:

function(response)
{
 var toplevel = response.<your_top_level_element>;
} 

Edit:

We're using JSON Simple for JSON encoding.

Our Java backend then looks like this (simplified version without error checking):

public String execute()
{
  JSONObject jsonResult = new JSONObject();

  jsonResult.put( "result", "ok");

  return jsonResult.toJSONString();
}

And in the Javascript function:

function(response)
{
 var result = response.result; //now yields "ok"
}
廻憶裏菂餘溫 2024-10-31 20:41:39

如果这是一个ajax请求,您可以将请求转发到另一个jsp页面而不是返回。 另一种方法是用

getServletContext().getRequestDispatcher("/ajax.jsp").forward(request, response);

create the jsp page(ajax.jsp) on your webcontent and add this sample code.

<p>${rest}</p> 
<!-- Note: You can actually design your html here. 
     You can also format this as an xml file and let your js do the work.
//-->

它替换你的 System.out.println

PrintWriter out = response.getWriter();
out.print("The value of res been passed is "+res);

但我想这是一个不好的做法。请参阅此处示例。

If this is an ajax request, you can forward the request into another jsp page rather than return. With this

getServletContext().getRequestDispatcher("/ajax.jsp").forward(request, response);

create the jsp page(ajax.jsp) on your webcontent and add this sample code.

<p>${rest}</p> 
<!-- Note: You can actually design your html here. 
     You can also format this as an xml file and let your js do the work.
//-->

Another way is replace your System.out.println with this

PrintWriter out = response.getWriter();
out.print("The value of res been passed is "+res);

but I guess this is a bad practice. See example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文