• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to send request data

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

How to send some request data (request attributes) from a servlet to a known JSP file?
Please give me the code snippet and some examples?

regards,
hari
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hari
Hope the following example solves yr problem......
Suppose there is a String var which u want to send as a request attribute to a jsp(for eg next.jsp). Then the servlet code which you wud use is


use "me" to fetch the value of the name Attribute in your jsp.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bhavna,

forward() method sends request and response to the *receiver* (resource for that request dispatcher is created) but at the same time *control* (flow) also goes to that resource.

But suppose the requirement is like this, we just want to send some data to another JSP or servlet but don't want to loose control (we don't want to go on that JSP or servlet), we want to do some more processing here but at the same time we want to send data there. How we can do this?

I hope my question is clear. Please help.

Thanks.

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi hari please refer following code

public void dispatch(HttpServletRequest request,HttpServletResponse response,String page)
throws IOException,ServletException {
RequestDispatcher rd = request.getRequestDispatcher("jsp/" + page);
log.debug("Contex Path :" + request.getRequestURI());
log.debug(rd);
rd.forward(request,response);
log.debug(request);
}

most of the time we store our files in some folder in my case i have stored them in jsp folder
from Servlet's doPost or doGet Method
to dispatch
1] write request.setAttribute method
2] call dispatch method

consider following
request.setAttribute(Constants.MESSAGE,Constants.MESSAGE_FOR_DELETE_APPLICATIONS);
dispatch(request,response,"Draft.jsp");
dispatching to Draft.jsp
 
Bhavna Jharbade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rathi ji
The control goes to the next page only after request.forward() is called.So even after setting the request attribute, you can do anything u like and then use forward when you want to forward the control to the jsp.

I hope this clears your doubt.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bhavna Jharbade:

The control goes to the next page only after request.forward() is called.So even after setting the request attribute, you can do anything u like and then use forward when you want to forward the control to the jsp.




From Sun's Doc:

forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.



Since the output buffer is flushed before the forward, any response written in the calling( dispatching ) servlet is also flushed, hence lost. So, while forwarding a request, request attributes, response headers etc, setting beans etc is possible but writing some response here and some response there is not possible.

Also, the control from the calling servlet is not 'lost'. Using rd.forward(...) works just like a method call and the calling servlet gains control after the service method of the dispatched request returns.

[Edited to add last paragraph]
[ October 17, 2005: Message edited by: Neeraj Dheer ]
 
Bhavna Jharbade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neeraj
Can you plz clarify what you want to convey.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bhavna,

What i meant was:



In this case, the output is


Nothing written to the ouput by the first servlet is seen because the response buffer is cleared before the forward, but the last System.out.println(...) in the first servlet gets written to the server console/log as well.

Also, if an Exception occurs in the called servlet, which is handled by the calling servlet, the response does not get committed and the calling servlet can add its own response after the response written by the called servlet.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic