• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am not getting any error message

 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this code

package MyServlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TestSession extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/html");
String user = request.getParameter("user");
HttpSession session = request.getSession();

if(session.getAttribute("user")==null)
session.setAttribute("user",user);

PrintWriter out = response.getWriter();
out.println("session :"+session.getAttribute("user"));
out.flush();
//response.sendError(501,"My Name is Sreeraj."); //1
//response.sendRedirect("red"); //2
RequestDispatcher rd = request.getRequestDispatcher("/JspTest.jsp");
rd.forward(request,response);

}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
doGet(req,res);
}

}



http://localhost:8080/HFSJ/TestSession.do?user=Sreeraj
When i execute this servlet i am getting the output session : Sreeraj.

Why i am not getting any error message? Because i am calling response.flush() before forward the request to JspTest.jsp.

Please give me an explanation.
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I've tried that as well....both with sendRedirect() and forward() but didn't get an exception....I mean you're supposed to get an IllegalStateException if you try to do any of these after the reponse has been committed(that's what the spec. says) but Tomcat is not throwing one....what's happening is that the response is getting committed and that's it...maybe only Tomcat behaves this way...I'm really interested in knowing how the other containers behave
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using tomcat and i have tried to run this code and i am getting illegalStateException. I have also got the output session:Sreeraj but my page was not forwarded to JspTest.jsp after this output.
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.....I got it man....Thanks Gohar....an IllegalStateException is thrown but you'll not see it in the output, but you'll find it if you check the log....Since the response has already been flushed, you get to see that, what has already been committed, as the output....in this case session:Sreeraj
[ December 21, 2006: Message edited by: Sayak Banerjee ]
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ali Gohar,Sayak Banerjee.
Ali Gohar, You are right its not forwarding to JspTest.jsp. But i think it will get only an error page.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic