Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

response already committed exception when calls to one of my servlet

 
Tharaka Peiris
Greenhorn
Posts: 11
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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 Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like

 
Tharaka Peiris
Greenhorn
Posts: 11
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!