• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Servlet request attribute

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the help
 
reply
    Bookmark Topic Watch Topic
  • New Topic