File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Using sendRedirect and requestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using sendRedirect and requestDispatcher" Watch "Using sendRedirect and requestDispatcher" New topic
Author

Using sendRedirect and requestDispatcher

Gurvinder Singh
Greenhorn

Joined: Nov 10, 2008
Posts: 27
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Gurvinder Singh
Greenhorn

Joined: Nov 10, 2008
Posts: 27
Thanks

it got solved by adding the following line in the jsp

<%@ page buffer="16kb"%>
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
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

Joined: Nov 10, 2008
Posts: 27
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

Joined: Sep 06, 2009
Posts: 253

Hey Chinmaya,
The default buffer size is 8 kb. as you can see at the following link


Regards
Salil Verma
Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
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


My Thoughts : http://passion4java.blogspot.com
Try not to become a man of success but rather to become a man of value.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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

Joined: Sep 06, 2009
Posts: 253

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using sendRedirect and requestDispatcher
 
Similar Threads
Error page mechanism
cannot use the gotopage method
ServletExec and RequestDispatcher
forward() in servlets.
Pass request parameters to getRequestDispatcher()