File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes A doubt on forward(req,resp) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "A doubt on forward(req,resp)" Watch "A doubt on forward(req,resp)" New topic
Author

A doubt on forward(req,resp)

Manjunath Subramanian
Ranch Hand

Joined: Jul 18, 2001
Posts: 236
I have two servlets..
1)FirstServlet,which forwards a request to ,
2)SecondServlet.
Take a look at the code below..
FirstServlet.java
*****************
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class FirstServlet extends HttpServlet{
public void init(ServletConfig sc) throws ServletException{
super.init(sc);
System.out.println("init() firing in FirstServlet");
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
System.out.println("In doGet() of FirstServlet");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("hello in FirstServlet");
out.flush();
System.out.println("hello in FirstServlet");
RequestDispatcher req = request.getRequestDispatcher("/servlet/SecondServlet");
req.forward(request,response);
}
}
SecondServlet.java
******************
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SecondServlet extends HttpServlet{
public void init(ServletConfig sc) throws ServletException{
super.init(sc);
System.out.println("init() firing in SecondServlet");
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
System.out.println("In doGet() of SecondServlet");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello");
}
}
***********************************************
Just two questions..
1)Can you determine the output of the following?
Could you explain it to me..
2)What kind of situations will result in throwing an IllegalStateException?
Thanks a lot,
Manjunath
Manjunath Subramanian
Ranch Hand

Joined: Jul 18, 2001
Posts: 236
My Fault..
Catalina did throw a IllegalStateException,but the stacktrace was printed in the log file..
I was expecting it either in the browser window or the dos-window..but these windows did not give any message.
Ram Dhan Yadav K
Ranch Hand

Joined: Aug 13, 2001
Posts: 321
If you call a writer or output stream before forwarding ,then an illegal exception is thrown. If you want to forward a request, then don't open the writer or outputstream in the forwarding servelt.
correct me if i am wrong.


Ram Dhan Yadav (SCJP, SCWCD, SCJA-I, IBM EC(483))
"We are what we repeatedly do. Excellence, then, is not an act, but a habit."
Manjunath Subramanian
Ranch Hand

Joined: Jul 18, 2001
Posts: 236
Ram: Calling getWriter() or getOutputStream() in the calling servlet before forwarding does not throw an IllegalStateException.Infact even if you write out.println() statements before forwarding,does not result in any exceptions.These statements are ignored as the container clears the buffer before forwarding(i,e any uncommited response).If there is an explicit flushing of the buffer with the help of flush() in the calling servlet (as shown in the code above), then the ServletContainer throws a IllegalStateException.
Hope this helps,
Manjunath
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
If there is an explicit flushing of ...
ummmm....doesn't have to be explicit.
Any time the response is committed to the client, a forward after this commit will cause an IllegalStateException.
The other way a reasponse gets committed to the client is when the buffer is full and the server flushes the content implicitly.
Right?
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Ram Dhan Yadav K
Ranch Hand

Joined: Aug 13, 2001
Posts: 321
Hi Guys,
Thanks for the eye opening!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A doubt on forward(req,resp)