• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A doubt on forward(req,resp)

 
Manjunath Subramanian
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manjunath Subramanian
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
Thanks for the eye opening!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic