如何解决:java.lang.IllegalStateException:提交响应后无法转发

时间:2019-05-15 14:51:29

标签: java jsp servlets

我正在尝试从Servlet发送一些数据到Jsp,这些数据是从数据库中检索到的,我得到了预期的结果,但也遇到了这个错误。

我知道这个“ java.lang.IllegalStateException:在提交响应后无法转发”问题已在该论坛中问了百万遍。,我阅读了所有内容,但没有与我匹配的..,

Servlets


 private void selectPatient(HttpServletRequest request, HttpServletResponse response) throws Exception {

            String patientId=request.getParameter("pNic");

            //Retrieve data from database and save in Patient object
            Patient p1=pdb.selectPatientDb(patientId);
            //add object to attributes
            request.setAttribute("PatientDb", p1);

            //send to the Jsp page (view)
            RequestDispatcher fly3=request.getRequestDispatcher("/assignMed.jsp");
            fly3.forward(request, response);
    }

数据库类

public Patient selectPatientDb(String patientId) throws Exception {
        Patient patientDb=null;
        Connection myConn=null;
        java.sql.PreparedStatement myStmt=null;
        ResultSet myRs=null;
        //get connection
        try {
            myConn=datasource.getConnection();
            String sql="select * from patient where nic=?";
            //prepare statement
            myStmt=myConn.prepareStatement(sql);
            myStmt.setString(1, patientId);

            //execute Statement
            myRs=myStmt.executeQuery();
            //Reading 
            if(myRs.next()) {
                String pid=myRs.getString("nic");
                String firstName=myRs.getString("first_name");
                String lastName=myRs.getString("last_name");
                String address=myRs.getString("address");
                String description=myRs.getString("description");

                //Assign to the object
                patientDb=new Patient(pid, firstName, lastName, address, description);
            }else {
                throw new Exception("Cannot find the given id "+patientId);
            }


        } catch (SQLException e) {

            e.printStackTrace();
        }finally {
            close(myConn, myStmt, myRs);
        }

        return patientDb;
    }

JSP

<!--Form-->
    <form action="PharmaceuticalControllerServlet" class="form-horizontal" method="POST" >
        <fieldset>

            <!-- Form Name -->
            <div class="content-title">
                <h2>Patient Name : ${PatientDb.firstName} </h2>
            </div>

            <!-- Input for send info of request-->
            <input type="hidden" name="infoPost" value="assignMed">


            <!-- Input Pharmaceutical ID-->
            <div class="form-group row">
                <label for="phId" class="col-lg-2 col-form-label-sm">Pharmaceutical Id</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control form-control-sm" id="pMedId" name="pMedId" placeholder="NIC No">
                </div>
            </div>

            <div class="form-group row">
                <label for="phId" class="col-lg-2 col-form-label-sm">Qty</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control form-control-sm" id="pMedQty" name="pMedQty" placeholder="NIC No">
                </div>
            </div>

            <button name="btnSubmit" type="submit" class="btn btn-outline-success btn-sm">Assign</button>

        </fieldset>
    </form>
Error:
java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:326)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:316)
    at com.hospital.Pharmaceutical.PharmaceuticalControllerServlet.listMedicine(PharmaceuticalControllerServlet.java:214)
    at com.hospital.Pharmaceutical.PharmaceuticalControllerServlet.doPost(PharmaceuticalControllerServlet.java:139)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案