• 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

Servlet + response.sendredirect & request dispatcher

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 147
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Rishabh Shah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok..thanks Pete Nelson
 
reply
    Bookmark Topic Watch Topic
  • New Topic