• Post Reply Bookmark Topic Watch Topic
  • New Topic

sendRedirect()  RSS feed

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We cannot call this method if the response is committed.



In this code, we are forcing the servlet container to send the header & generated text to the browser immediately by calling pw.flush(). The response is said to be committed at this point.



I doubt the correctness of this statement. Does it means that when we call flush() the text will be seen in the browser? I think this is not correct.



I see both the messages together (after waiting for 10 seconds).

I think by calling flush(), we say the response it committed (but not shown in browser immediately) and now we can't call sendRedirect().


Moreover, what's the significant of flush() method ?
[ February 26, 2007: Message edited by: Sandeep Vaid ]
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure either about the flush() method forcing output. I think that calling flush() on the PrintWriter only commits the response.

ServletResponse's flushBuffer() method is supposed to force the content to be output to the client.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i tried i got the output as Hello World! . It didn't generate any IllegalStateException.



But in the specification of sendRedirect() it is mentioned as

throws java.lang.IllegalStateException - If the response was committed or if a partial URL is given and cannot be converted into a valid URL

Could you please explain why i am not getting the exception
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's weird indeed. I've tried it myself on Tomcat and didn't get the exception either. I think that we're supposed to get the IllegalStateException though. If anybody using another container could try...
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the same coding in websphere i got IllegalStateException .
[ February 27, 2007: Message edited by: Prabu sb ]
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like this is a containter problem.

See the following classes from catalina.jar in Tomcat org.apache.catalina.connector.Response and org.apache.catalina.connector.OutputBuffer.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Prabu, that seems to clear the doubt.
Tomcat is a bit flexible on that one.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real exam is not dependent on App/Web servers. What should we do if we have the same question in the exam? I mean will we say it will through IllegalStateExcption or not?
Regards,
Sri.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the spec says, it throws one.
 
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as per the specs, it should throw and IllegalStateException!
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for your replies. It helped me...
 
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

I tried this code on jakarta-tomcat-5.0.28 . And getting IllegalStateException on my tomcat console.
What I did. I just created a listener to listen to addition of attribute to request.
To see the snapshot, Click Here

ServletH instance is initialized
ServletL instance is created
ServletL instance is initialized
Request org.apache.coyote.tomcat5.CoyoteRequestFacade@8acfc3 initialized at Sun Aug 19 19:44:48 IST 2007
Request org.apache.coyote.tomcat5.CoyoteRequestFacade@8acfc3 destroyed at Sun Aug 19 19:44:48 IST 2007
Request org.apache.coyote.tomcat5.CoyoteRequestFacade@8acfc3 initialized at Sun Aug 19 19:44:55 IST 2007
Attribute javax.servlet.error.exception with value java.lang.IllegalStateException added to request org.apache.coyote.tomcat5.CoyoteRequestFacade@8acfc3 at Sun Aug 19 19:44:55 IST 2007
Request org.apache.coyote.tomcat5.CoyoteRequestFacade@8acfc3 destroyed at Sun Aug 19 19:44:55 IST 2007



In ouput, I saw
Hello World!

Code was



Regards,
Khushhal
[ August 19, 2007: Message edited by: khushhal yadav ]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

What is the solution for this exception? I am executing the following code with sendRedirect(). The redirects tkaes me to correct page, however, it also throws exception at the control. I tried removing flushBuffer(). I also tried response.encodeRedirectURL(), but no luck. Can somebody please help me on this ?
Here is my code:


ERROR on Console:

[7/8/10 20:12:46:044 IST] 000000d3 ServletWrappe E SRVE0068E: Uncaught exception thrown in one of the service methods of the servlet: Faces Servlet. Exception thrown : java.lang.IllegalStateException: Cannot forward. Response already committed.
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:157)
at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:325)
at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:174)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:220)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1075)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:550)
at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3391)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:267)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:811)
at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1455)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:115)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:387)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:267)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:195)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:743)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:873)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1473)

[7/8/10 20:12:46:091 IST] 000000d3 LocalTranCoor E WLTC0017E: Resources rolled back due to setRollbackOnly() being called.
[7/8/10 20:12:46:091 IST] 000000d3 WebApp E [Servlet Error]-[Faces Servlet]: java.lang.IllegalStateException: Cannot forward. Response already committed.
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:157)
at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:325)
at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:174)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:220)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1075)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:550)
at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3391)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:267)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:811)
at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1455)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:115)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:387)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:267)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:195)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:743)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:873)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1473)

[7/8/10 20:12:46:091 IST] 000000d3 SRTServletRes W WARNING: Cannot set status. Response already committed.
[7/8/10 20:12:46:091 IST] 000000d3 SRTServletRes W WARNING: Cannot set header. Response already committed.

 
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 3064
248
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit,

You are replying on a very old thread. Next time start a new one as the discussion in that thread doesn't seem related to your problem.

Please privide us with some extra information:
  • What is the URL you are using?
  • What servlets are configured in your web-app?
  • You are getting an IllegalStateException on your Faces Servlet. Is the code in the beginning from the Faces Servlet?


  • Regards,
    Frits
     
    Amit K Jain
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Frits,

    Firstly thank you for replying on this. As per your suggestion, I have started new thread for this issue with below link. I would really appreciate any help with this.
    http://www.coderanch.com/t/502121/java-Web-Component-SCWCD/certification/java-lang-IllegalStateException-sendRedirect
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!