Archana
Archana

Reputation: 237

Getting null values while using request.setAttribute and get

I need to pass a variable from Admin.java file to index.jsp. I get the value when I print it in Admin.java. I need to pass that value to another variable which needs to be sent to index.jsp. This new variable gets null value.

The code in Admin.java is

public string static rest;

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
{
    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);
    System.out.println("The value of rest is "+rest);
    request.getRequestDispatcher("index.jsp").forward(request, response);

if(res != null)
 {
       request.getSession().setAttribute("access", true);

       System.out.println("Admin:doGet:login:true");
       response.getWriter().write("existsnoadmin");
       return;
}

Output:

The value of res been passed is C Language.
The value of rest is null.

As per the questions asked in stack overflow we need to use forward or redirect when we need to send the value to jsp page. But in my case, I am trying to return value from a function so I do not know whether the way I am trying to do in the above code is right or not.

The code in index.jsp is:

if(response=="existsnoadmin")
 {
    alert(response);
    alert("The value obtained by the admin.java is " +request.getAttribute("rest"));
    out.println("The value obtained by the admin.java is " +request.getAttribute("rest"));
    alert("we have done in index.jsp");
}

The output is that I am getting the alert box which says "existsnoadmin".

But I am not able to get the value of rest here nor in Admin.java.

What is the mistake I have done here? Please help.

Regards,

Archana.

Upvotes: 1

Views: 9648

Answers (1)

Stephen C
Stephen C

Reputation: 718698

You say that the code in the JSP is this:

if(response=="existsnoadmin")
{
    alert(response);
    alert("The value obtained by the admin.java is " +request.getAttribute("rest"));
    out.println("The value obtained by the admin.java is " +request.getAttribute("rest"));
    alert("we have done in index.jsp");
}

I'm having problems understanding what this really means.

If the above code is Java code that appears inside scriptlet tags <% ... %>, then I don't understand how alert(response); is showing you anything. In fact, it should give you a compilation error in the JSP.

On the other hand, if the above is Javascript code that is embedded in the page that the JSP generates, then

  • request.getAttribute("rest") cannot possibly work ... because the request object that you set the attribute on does not exist in the web browser, and

  • out.println(...) cannot work because the JspWriter does not exist in the web browser.

Either you have not transcribed the JSP excerpt accurately, or your Java and/or Javascript doesn't make sense.


Based on your comment, I think you need the following.

if(response=="existsnoadmin")
{
    alert(response);
    alert('The value obtained by the admin.java is ' +
          '<% request.getAttribute("rest") %>');
    // The value obtained by the admin.java is <% request.getAttribute("rest") %>
}

Or if you want to get rid of the scriplet stuff ...

if(response=="existsnoadmin")
{
    alert(response);
    alert('The value obtained by the admin.java is ' +
          '${requestScope.rest"}');
    // The value obtained by the admin.java is ${requestScope.rest"}
}

If you want the stuff that I've turned into a // JS comment to be visible on the page, you been to move it to some content part of the HTML. Currently it is (I assume) inside a <script> element, and therefore won't be displayed.

The key to all of this black magic is understanding what parts of a JSP are seen/evaluated by what:

  • JSP directives e.g. <@ import ...> are evaluated by the JSP compiler.
  • Stuff inside scriptlet tags e.g. <% ... %>, EL expressions e.g. ${...} or JSTL tags e.g. <c:out ...\> gets evaluated when the JSP is "run".
  • Anything generated by the JSP (HTML content, embedded Javascript) is displayed / executed in the user's browser after the HTTP response has been received.

Now is it neccessary to use the request.dispatcher....forward command in admin.java.

Your primary servlet can do one of two things.

  • It can use the request dispatcher to forward the request to your JSP. If it does this it can forward additional values by setting request attributes.

  • It can open the response output stream and write stuff to it.

It should not attempt to do both! (I'm not sure exactly what will happen, but it is likely to result in a 500 Internal Error.)

Upvotes: 2

Related Questions