Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Not throwing IllegalStateException

 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I bet you're using Tomcat. For whatever reason, that culprit does not throw an Exception in this case.

https://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 ]
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic