aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes sendRedirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "sendRedirect()" Watch "sendRedirect()" New topic
Author

sendRedirect()

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
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 ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
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


SCJP 1.5<br />SCWCD 1.4
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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...
Prabu Bala
Greenhorn

Joined: Feb 27, 2007
Posts: 23
I tried the same coding in websphere i got IllegalStateException .
[ February 27, 2007: Message edited by: Prabu sb ]
Sandeep Krish
Ranch Hand

Joined: Jan 02, 2007
Posts: 58
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Thanks Prabu, that seems to clear the doubt.
Tomcat is a bit flexible on that one.
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

As the spec says, it throws one.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9377
    
    2

Yes, as per the specs, it should throw and IllegalStateException!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Thanks guys for your replies. It helped me...
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

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 ]

rgrds,
Khushhal
Amit K Jain
Ranch Hand

Joined: Nov 06, 2008
Posts: 55
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.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1669
    
  25

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

    Joined: Nov 06, 2008
    Posts: 55
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: sendRedirect()