• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Servlet request attribute

 
Abhishek Tiwari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone!

Hope all of you are doing well...
I have the following doubt in Servlet:-
I have 3 servlets ServletA, ServletB and ServletC. I pass a request attribute from ServletC to ServletA using request.setAttribute("url",request.getRequestURL()); . The ServletA has a login form with 2 textboxes username/password and a submit button on clicking the button it takes me to ServletB. On doing request.getAttribute("url") in ServletB i get null.

ServletA
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
System.out.println(request.getAttribute("url"));
pw.println("<html>");
pw.println("<form action=\"ServletB\" method=\"POST\">");
pw.println("<input type=\"text\" name=\"username\">");
pw.println("<input type=\"password\" name=\"password\">");
pw.println("<input type=\"submit\" value=\"Login\">");
pw.println("</form");
pw.println("></html>");
}


ServletB
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
pw.println("<HTML>");
pw.println(request.getAttribute("url"));
pw.println("</HTML>");
}

ServletC
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("url",request.getRequestURL());
request.getRequestDispatcher("/ServletA").forward(request,response);
}

Called using
http://localhost:8080/Servlets/ServletC

The output comes as null in ServletB.

Can anybody explain why this is happening?

Thanks in advance!!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Abhishek

Before I could go on reading your query, a suggestion firsthand is, please UseCodeTags which helps the source code to be neatly appearing and avoids the hindrances if any caused.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Tiwari wrote:

The output comes as null in ServletB.

Can anybody explain why this is happening?



It may be because of the "scope" being request. As and when you dispatch it to a new resource, the original request might have got abandoned and a fresh set of request,response objects get created to delegate/dispatch your request. Try setting it in a session scope.

Out of the context, emitting the complete HTML code via Servlet's output stream is NOT a good practice. May be for a practicing purpose, you can try. Better making use of JSPs and have MVC involved. Redirect to the jsp page (which has all your HTML/JSP code in it) from inside your servlet.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because ServletA is not passing its input parameter request to ServletB. But ServletC is passing its request parameter to ServletA from this line.



In ServletA you are making a new form, but this new form will only pass its own input parameter like username, password only.

As said by Raghavan you can do these things:

in ServletA:


In ServletC:


 
Abhishek Tiwari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raghavan and Punit for quick response...
So with every new form submit click new request and response Objects are created and send to the receiving servlet?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Tiwari wrote:
So with every new form submit click new request and response Objects are created and send to the receiving servlet?


yes.you got it
 
Abhishek Tiwari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic