wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to send request data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to send request data" Watch "How to send request data" New topic
Author

How to send request data

pulijala hari
Ranch Hand

Joined: May 14, 2005
Posts: 70
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
Bhavna Jharbade
Ranch Hand

Joined: Sep 08, 2005
Posts: 69
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.


SCJP1.4 (86%)
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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.

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
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

Joined: Sep 08, 2005
Posts: 69
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.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
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

Joined: Sep 08, 2005
Posts: 69
Hi Neeraj
Can you plz clarify what you want to convey.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to send request data