File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet + response.sendredirect & request dispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet + response.sendredirect & request dispatcher" Watch "Servlet + response.sendredirect & request dispatcher" New topic

Servlet + response.sendredirect & request dispatcher

Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
Hello Everyone,

Happy new year all of you,

I am making one website. Its demo website. URL is I have completed only 10 %. Now, The thing is, When I make new user, and submit, it redirects on my login page. but, When I click on back button, It gives me following error.

Note : I have not put any client side validation yet.

SEVERE: Servlet.service() for servlet SimpleCaptcha threw exception
java.lang.IllegalStateException: Cannot create a session after the response has been committed
at org.apache.catalina.connector.Request.doGetSession(
at org.apache.catalina.connector.Request.getSession(
at org.apache.catalina.connector.RequestFacade.getSession(
at org.apache.catalina.connector.RequestFacade.getSession(
at nl.captcha.servlet.SimpleCaptchaServlet.doGet(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.Http11Processor.process(
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(
at Source)

I know why is it happening..I also show you my code of servlet.

As you can see above, I get all the parameters in request,

1) I check Capcha on this servlet class. and becuase of this capcha..I am getting this problem.
2) I have used only response.sendredirect only..and In remaining code, I have used just response.sendredirect..but, can anyone tell us, in real time application, when do I use sendredirect or requestdipatcher. some of programmers say you can also use only sendredirect..but, I still want to know, In which situation I should use... ?
3) If I get any exceptions or error from any servet, or any other class, how to handle it on jps page...

Please reply..and also tell me whether I have used logic correct or not..and give me suggetions.

If you don't get anything, please let me know...

Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

I think the most important distinction between HttpServletResponse.send*(...) methods and using a RequestDispatcher is that the former are all part of the HTTP communication between server & browser, where the RequestDispatcher is specific to internal communication within your application server.

When you call sendRedirect, an HTTP "Location:" header is added to the send buffer to be flushed to the client. You're using the HttpServletResponse interface to speak HTTP to the client browser. And then you're done talking to the browser!

I use the sendRedirect when the servlet cannot fulfill the request, for example when a login is required (redirect to login page), or required form data is missing (redirect back to form). Basically, I'm just sending back in HTTP "I can't help you, go HERE".

When you use a RequestDispatcher, you're passing the request & response objects back to your app server, and telling it to do further work in order to fulfill the request. The browser never sees any of these URIs where the request & response objects are being dispatched.

RequestDispatcher plays a major role in Front Controller Pattern or MVC. The majority of my servlets serve no content directly, but look at the incoming data, and then use a RequestDispatcher to dispatch the request (and response) onto the appropriate worker and/or JSP template.

In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
ok..thanks Pete Nelson
I agree. Here's the link:
subject: Servlet + response.sendredirect & request dispatcher
It's not a secret anymore!