This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using sendRedirect and requestDispatcher

 
Gurvinder Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

I have a requirement to forward a request a third party link from my servlet.
For this i m using response.sendRedirect("http://google.com") in my servlet. But this is sometimes failing in some particular cases without any errors but giving a blank page.
So i thought to probably use
RequestDispatcher dispatcher =
request.getRequestDispatcher("http://google.com");
if (dispatcher != null) dispatcher.forward(request, response);
return;

but the above is giving me the following error
11:07:08,318 ERROR [ContainerBase] Servlet.service() for servlet jsp threw exceptio
java.lang.IllegalStateException: Cannot forward after response has been committed


any pointers will be highly appreciated

-Gurvinder

 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You cannot use RequestDispatcher to forward a request outside the Servlet context of the application.
2. IllegalStateException is thrown when you send something as a response before forwarding the request. See if you are calling response.getWriter/getOutputStream to send some response before forwarding.
3. Check the server logs, if you see a blank page when you sendRedirect, there might be an exception on the server causing the problem...
 
Gurvinder Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

it got solved by adding the following line in the jsp

<%@ page buffer="16kb"%>
 
Chinmaya Chowdary
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurvinder.

it got solved by adding the following line in the jsp

<%@ page buffer="16kb"%>

Can you please explain, how you got solved by specifying the above page directive? As far I know, the default buffer size is 64kb.
 
Gurvinder Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read it on the following forum

http://forums.sun.com/thread.jspa?threadID=604166

which basically has the following post
Basically you are doing too much output before you decide to you need to forward to a new page.

Output could just be the plain HTML text in the jsp document - that all gets converted to out.println();
Basically if it writes more than your buffer (default is 8kb) then the buffer is flushed, and you can't forward/redirect anymore as you are committed to complete the current response.

There are two ways of handling this issue
1 - shift your forward/redirect code, so that it happens as early as possible. BEFORE any HTML is output.

2 - increase the buffer size. <%@ page buffer="16kb"%> for instance.
This means it can write out more text before you decide to forward.
That might stop the error on your page.

3 - Oh, and if you have set your buffer to zero, or you have directly called flush() somewhere in your code, that would be the cause. Don't do those things :-)



Can someone tell me does it have a performance impact if we increase or decrease page buffer

Thanks
Gurvinder
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Chinmaya,
The default buffer size is 8 kb. as you can see at the following link
 
Mohammed Yousuff
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gurvinder i guessing by setting the autoFlush="true" you can overcome this issue. because it will not flush untill you do that ....

i think this will not bring down the performance of the servlet...

let me if my guess is wrong
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally requests go to the JSP through a servlet and you should decide in the servlet whether to sendRedirect or not. I would not recommend redirecting or forwarding a request from a JSP. And Mohammed, you've set autoFlush to true, so the buffer will be flushed automatically, setting autoFlush to false does what you said...
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gurvinder i guessing by setting the autoFlush="true" you can overcome this issue


In a jsp, autoFlush="true" is the default behavior you need not to set it explicitly. Kindly refer the following link
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic