• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not throwing IllegalStateException

 
mallika shah
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
i have written a code to test reponse.sendRedirect().whether after commiting a response , calling this method throws an exception.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
byte[] someBytes = new byte[1];
someBytes[0] = '1';
response.setContentType("text/html");
PrintWriter pw1 = response.getWriter();
pw1.write (" Succeeded");
pw1.flush();

if(true){
response.sendRedirect("try4.jsp");
}

It should have generated an Exception , but it simply shows me the output of try4.jsp.

Please help me here,
thanks,
mallika
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I bet you're using Tomcat. For whatever reason, that culprit does not throw an Exception in this case.

http://www.coderanch.com/t/174381/java-Web-Component-SCWCD/certification/sendRedirect

PS : I've added an entry to the SCWCD FAQ, and added a comment to the IllegalStateException wiki
[ April 05, 2007: Message edited by: Satou kurinosuke ]
 
Sunder Ganapathy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Tomcat 6 and Java 6 for my studies.
I got the following in the logs\localhost2007-04-06.log file.

Apr 6, 2007 4:54:08 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Apr 6, 2007 4:54:08 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Apr 6, 2007 4:56:05 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet IseServlet threw exception
java.lang.IllegalStateException

at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:434)
at com.example.web.ise.doGet(ise.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:212)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
Apr 6, 2007 4:59:16 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet IseServlet threw exception
java.lang.IllegalStateException

at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:434)
at com.example.web.ise.doGet(ise.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:212)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
 
mallika shah
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satou and Sunder ,
But Satou , as you said Tomcat doesent show the Exception occured.
That means for the exam ,we have to burn this up that the excpetion occured if the response is commited.
BUT,there are two conditions related to this kind of questions:

1. when there is
out.println("something");
response.sendRedirect("...");

2.
out.println("...");
out.flush();
response.sendRedirect("...");

One is with flush() and one is without.
does IllegalStateException occured in botht the condition or just in the second where we flush()

As many quiestions contain such situations, so what should we select for the exam???

thanks,
mallika
 
Sunder Ganapathy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The specification very clearly states that Calling flush() on the PrintWriter commits the response ( Page 211 ).

Hence calling sendRedirect after getWriter DOES NOT throw illegalStaeException.

You need to have called flush, before SendRedirect, to get illegalStateException.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic