This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet + response.sendredirect & request dispatcher" Watch "Servlet + response.sendredirect & request dispatcher" New topic
Author

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 http://onlinemedicineshopping.jelastic.com. 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(Request.java:2381)
at org.apache.catalina.connector.Request.getSession(Request.java:2098)
at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:833)
at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:844)
at nl.captcha.servlet.SimpleCaptchaServlet.doGet(SimpleCaptchaServlet.java:73)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown 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...

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


OCPJP
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: http://aspose.com/file-tools
 
subject: Servlet + response.sendredirect & request dispatcher