9

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Archana
  • 237
  • 3
  • 9
  • 17
  • Put a breakpoint; check if your Admin code actually gets executed. – Joeri Hendrickx Mar 21 '11 at 09:03
  • Yes the admin code is getting executed and the value of res is getting populated there. I have checked that but I am not able to pass it to the desired jsp page. – Archana Mar 21 '11 at 09:07

3 Answers3

4

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"
}
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • I am sorry I do not know JSON. Can i straight away put this code in my java class file and use the object in the jsp file? – Archana Mar 21 '11 at 08:52
  • You can do that in your jsp as well. Just use `<%=jsonResult.toJSONString()%>`. For filling the JSON object, just have a look at the JSON Simple documentation. It's really simple - just set properties or fill arrays and eventually call `toJSONString()` for the correct formatting. – Thomas Mar 21 '11 at 09:52
4

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

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.

ace
  • 6,775
  • 7
  • 38
  • 47
  • Thanks for the suggestion. But i need to access the value of the variable from the java file in my index.jsp. So if i send the value to another ajax.jsp it still needs to be sent to index.jsp. So it will be better if I can pass the value of the varible to index.jsp in the first place. – Archana Mar 21 '11 at 08:54
  • You mean before loading the index.jsp in the browser you already have the value. I thought the value is requested via ajax. If that is the case may I know the flow of you application. Ex. which file is first executed. – ace Mar 21 '11 at 09:09
  • Index.jsp has the function of login. And through ajax I call admin.java which does the part of authenticating the user. Every user when registers will tell about his/her interest in any language say c,c++, java, etc. All this information is stored in the database. – Archana Mar 21 '11 at 09:26
  • Once this is done.When the user logs in the application through admin.java is finding for the interest who is trying to login. This is done through the function named SemanticSearch. This function is returning the value of interest in the varaible 'res' which I need to pass back to index.jsp after the admin.java returns. – Archana Mar 21 '11 at 09:27
  • like as I said about ajax.jsp this is the one who will handle the res. Then after this was called it will return to your ajax request. You may also not include the return in your Servlet. About my second suggestion it is one of the simplest way. Try to alert(request) if you can see the response. Also add error handler on your ajax, see this "http://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages" – ace Mar 21 '11 at 10:16