This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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?
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
Joined: Sep 08, 2005
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.
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 ]
Joined: Sep 08, 2005
Hi Neeraj Can you plz clarify what you want to convey.
Joined: Mar 30, 2005
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.