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

response already committed exception when calls to one of my servlet

 
Greenhorn
Posts: 11
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String email = request.getParameter("email");
String password = request.getParameter("password");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String tempAge = request.getParameter("age");

HttpSession session = request.getSession();

int age = 0;
try{
age = Integer.parseInt(tempAge);
}catch (NumberFormatException e) {
request.setAttribute("errorAge", "Invalid Age");
request.getRequestDispatcher("RegisterCustomer.jsp").forward(request, response);
}

if(email.isEmpty() || password.isEmpty() || firstName.isEmpty() || lastName.isEmpty()){
request.setAttribute("errorNull", "Please file all fields");
request.getRequestDispatcher("RegisterCustomer.jsp").forward(request, response);
}

Customer customer = new Customer();
customer.setAge(age);
customer.setEmail(email);
customer.setPassword(password);
customer.setFirstName(firstName);
customer.setLastName(lastName);

try{
CommandExecutor.executeCommand(new RegisterCustomerCommand(customer));
}catch (Exception e) {
e.printStackTrace();
response.sendRedirect("error.jsp");
}

System.out.println("FINAL RESPONSE........................................");
session.setAttribute("customer", customer);
session.setAttribute("sessionId", session.getId());
response.sendRedirect("home.jsp");

Above is my doPost method body. when I enter correct details method works fine till it reach to the last statement and it will throw a IlleagleStateException exception mentioning response is already committed.

Another thing is if enter invalid age value it will go to the catch block in the age casting try block then without forwarding the request it will continue the program. please if someone can help with this i really appreciate it.

Thank you.

Stack Trace for this exception :


java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:312)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at servlet.RegisterCustomerAction.doPost(RegisterCustomerAction.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
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:857)
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)
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to return from the method after forwarding. You cannot forward twice.
 
Tharaka Peiris
Greenhorn
Posts: 11
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Christophe,
Actually I'm new to JSP and Servlet. Could you please elaborate this little bit further how to do that?

Thank you...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like

 
Tharaka Peiris
Greenhorn
Posts: 11
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Christophe,

Ya.. its working... Thank you very much. you saved my day...
If you have any free time is it possible to describe me what has happened in the code and why I had to return?

Thank you.
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic