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)