0

When we are trying to forward request using RequestDispatcher from servlet to jsp we user request.setAttribute()method.

  request.setAttribute("uname", name);
  RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
  rd.forward(request, response);

But when we are trying to forward request from servlet to another servlet we don't use request.setAttribute() method, and result prints just fine.

  RequestDispatcher dis=request.getRequestDispatcher("welcome");          
  dis.forward(request, response);      

Can you exmplain why is that? Thank you.


login.jsp

 <form action="login" method="post"> 
     User Name:<input type="text" name="uname"/><br/> 
     Password:<input type="password" name="upass"/><br/> 
     <input type="submit" value="SUBMIT"/> 
 </form> 

LoginDetails servlet

    String name = request.getParameter("uname");
    String pass = request.getParameter("upass");
    if (name.equals("dilini") && pass.equals("123")) {
    // ONLY ONE OPTION IS USING HERE
    // for a jsp
    request.setAttribute("uname", name);
    request.getRequestDispatcher("welcome.jsp").forward(request, response);

    // for a servlet
    request.getRequestDispatcher("welcome").forward(request, response);

   }
Dil.
  • 1,996
  • 7
  • 41
  • 68
  • Then the request probably already had the attribute in it. Unless you expect it to somehow magically appear in the request attributes? A JSP is compiled to a servlet at runtime, so there's no difference whether you're forwarding to a JSP or a servlet. – Kayaman May 09 '18 at 07:32
  • @Kayaman my question is how the servlet has that request attribute and jsp doesn't? Since they are using the same method to forward the result. – Dil. May 09 '18 at 07:35
  • It's the request that either does or doesn't have the attribute. Servlets and JSPs have nothing to do with anything here. – Kayaman May 09 '18 at 07:36
  • If your method is written exactly as you have shown it - with the two lines following each other then you set the attribute in the request for the JSP and then forward the same request (already having the attribute) to the servlet – Veselin Davidov May 09 '18 at 07:45
  • @VeselinDavidov no I don't use both methods. this is only for example – Dil. May 09 '18 at 08:02
  • @Kayaman Please look at this question. https://stackoverflow.com/questions/11457336/passing-data-from-servlet-to-another-servlet-using-requestdispatcher – Dil. May 09 '18 at 08:07
  • @VeselinDavidov Please look at this question. https://stackoverflow.com/questions/11457336/passing-data-from-servlet-to-another-servlet-using-requestdispatcher – Dil. May 09 '18 at 08:08
  • Please make sure you understand my comments. – Kayaman May 09 '18 at 08:13
  • @kayaman yes I do. The problem is that in that second scenario request doesn't have the attribute assigned. So how is it working? – Dil. May 09 '18 at 08:20
  • Like I said, either the request has the attribute or it doesn't have it. If it's working, then the request has the attribute. Again, JSP vs. servlet has nothing to do with anything here. The attributemap is nothing special, you can imagine that it's a `HashMap` if it makes it easier for you to understand. – Kayaman May 09 '18 at 08:51
  • If you forward same request servlet or jsp, they should have the parameters . Can you show the welcome servlet and welcome jsp code? – Shubham Kadlag May 09 '18 at 09:22
  • @Kayaman yes I understand it loud and clear. Can you please give me a suggestion that when it came to servlet section only how could the request has that attribute? since they didn't assign it all as an attribute. I know this doesn't related to servlet or jsp. I just trying to figure out in which place that attribute was assigned to the request? – Dil. May 10 '18 at 06:28

0 Answers0