user3457789
user3457789

Reputation: 21

HTTP Status 500 - Cannot forward after response has been committed

How would I go about fixing this? I have tried numerous things and looked at alot of different things on here with the same question but I cannot fix it. Could someone explain what is going on. If it helps, I want the customer to submit a form, once they do it goes to the servlet and sends it to a receipt where they see the info they have entered. If there is an easier solution I am all ears.

Below is the stacktrace

Mar 31, 2014 2:40:17 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [edu.witc.Assignment03.controller.CustomerServlet] in context with path [/Justin_EJ_Assignment03_15400579] threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:348)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
    at edu.witc.Assignment03.controller.CustomerServlet.sendCustomerInfo(CustomerServlet.java:85)
    at edu.witc.Assignment03.controller.CustomerServlet.doPost(CustomerServlet.java:139)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

SERVLET

package edu.witc.Assignment03.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;




import javax.servlet.http.HttpSession;

import edu.witc.Assignment03.model.Customer;
import edu.witc.Assignment03.model.Phone;
import edu.witc.Assignment03.model.States;


@WebServlet(description = "servlet to get act as controller between form and models", urlPatterns = { "/customerServlet" })
public class CustomerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public CustomerServlet() {
        super();
    }

    private void processRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        HttpSession session = request.getSession();

        //create the JavaStudents object    
        Phone phone = new Phone();

        //declare the student collection
        Collection<Phone> phones = phone.getPhoneCollection();
        //flesh out the collection from the arraylist
        //students = new JavaStudents().getStudents();
        //System.out.println("hey " + students.size());
        //store the JavaStudents object in the session
        //if(session != null){
            session.setAttribute("phones", phones);
            request.getRequestDispatcher("/customerManagement.jsp").forward(request, response);
        //}

    }






    private edu.witc.Assignment03.model.States states = new States();
    private List<edu.witc.Assignment03.model.Customer> customers = new ArrayList<Customer>();



 private void addCustomer(HttpServletResponse response, HttpServletRequest request)//redirect to index
           throws IOException, ServletException {
    String url = "/receipt.jsp";
        request.setAttribute("customers", customers);
        request.getRequestDispatcher(url).forward(request,response);
    }

    private void editCustomer(HttpServletResponse response, HttpServletRequest request)//redirect to index
           throws IOException, ServletException {
        String url = "/customerManagement.jsp";
        request.setAttribute("customers", customers);
    request.getRequestDispatcher(url).forward(request,response);
    }

    private void sendCustomerInfo(HttpServletResponse response, HttpServletRequest request)//redirect to index
            throws IOException, ServletException {
        String url = "/receipt.jsp";
        request.setAttribute("customers", customers);
        request.getRequestDispatcher(url).forward(request,response);




    }

    private Customer getCustomer(int customerId) {
        for (Customer customer : customers) {
            if (customer.getCustomerId() == customerId) {
                return customer;
            }
        }
        return null;
    }



    private void sendEditCustomerForm(HttpServletRequest request, 
            HttpServletResponse response) throws IOException, ServletException {

        String url = "/customerManagement.jsp";
        request.setAttribute("customers", customers);
        request.getRequestDispatcher(url).forward(request,response);
    }






    public void doPost(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {

           processRequest(request, response);
        // update customer
        int customerId = 0;
        try {
            customerId = 
                    Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
        }
        Customer customer = getCustomer(customerId);
        if (customer != null) {
            customer.setFirstName(request.getParameter("firstName"));
            customer.setLastName(request.getParameter("lastName"));
            customer.setEmail(request.getParameter("email"));
            customer.setPhone(request.getParameter("phone"));
            customer.setAddress(request.getParameter("address"));
            customer.setCity(request.getParameter("city"));

            customer.setZip(request.getParameter("zip"));
        }
        sendCustomerInfo(response, request);
    }
}

Upvotes: 0

Views: 1843

Answers (2)

Gautam
Gautam

Reputation: 3384

In your doPost() method your are using below two method for sending response

processRequest(request, response);
sendCustomerInfo(response, request);

so while calling sendCustomerInfo(response, request) you will get this exception as response is already committed by processRequest method

Upvotes: 2

nos
nos

Reputation: 229128

It's kind of hard to tell you what to do without seeing any code. However what's going on is that here:

  edu.witc.Assignment03.controller.CustomerServlet.sendCustomerInfo(CustomerServlet.java:85)

you are forwarding the response to something else.

However you have previously sent something in a response. Which you can't do. Either you have to send a response, or forward the request to something else. You can't do both.

Upvotes: 0

Related Questions