• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using sendRedirect and requestDispatcher

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

it got solved by adding the following line in the jsp

<%@ page buffer="16kb"%>
 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Chinmaya,
The default buffer size is 8 kb. as you can see at the following link
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic