开发者

How to pass a value of a variable from a java class to the jsp page

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);
        开发者_StackOverflow社区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.


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:

  • How to update current page by Servlet/Ajax?


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"
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜