0

我正在运行一个 JSP 调用 servlet1 的应用程序。这个 servlet1 在内部调用 servlet2 来获取一些数据。Servlet 1 自己计算一些数据并从 servlet2 获取一些数据并将其发送回 JSP。

但我收到以下错误:

HTTP 状态 500 -


类型异常报告

信息

描述 服务器遇到一个内部错误 () 阻止它完成这个请求。

例外

java.lang.IllegalStateException:提交响应后无法转发 RoutingParser.doPost(RoutingParser.java:389) javax.servlet.http.HttpServlet.service(HttpServlet.java:754) javax.servlet.http.HttpServlet.service(HttpServlet .java:847)

note Apache Tomcat/7.0.16 日志中提供了根本原因的完整堆栈跟踪。

我的代码是这样写的:

    import java.awt.List;
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.HashSet;
    import org.w3c.dom.Element;

    //import javax.lang.model.element.Element;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.swing.text.Document;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;

    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;

    import java.io.File;
    import java.util.ArrayList;


    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.HashSet;

    //import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

    public class RoutingParser extends HttpServlet {


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

     response.setContentType("text/html");
     PrintWriter writer = response.getWriter();

     System.out.println("Routing Parser is accessible");

     System.out.println("i reached servlet");
          try {

              ArrayList inputlist1= new ArrayList();

                                        inputlist1.add(request.getParameter("CardNetwork"));//CardNetwork
                inputlist1.add(request.getParameter("Currency"));//Currency
                inputlist1.add(request.getParameter("TxnCategory"));//TxnCategory
                inputlist1.add(request.getParameter("TxnType"));//TxnType


                ArrayList list1=new ArrayList();
                ArrayList returnedData= new ArrayList();

                System.out.println("i reached servlet");
                  try {

                    File fXmlFile = new File("c:\\eclipse\\RoutingTable.xml");
                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                    org.w3c.dom.Document doc = dBuilder.parse(fXmlFile);
                    doc.getDocumentElement().normalize();

                    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
                    NodeList nList = doc.getElementsByTagName("Entry");
                    System.out.println("-----------------------");

                    for (int temp = 0; temp < nList.getLength(); temp++)
                    {

                                //some piece of code

                              returnedData.add(data1);
                              returnedData.add(extra_info);
                              break;
                          }

                       }

                       /*it is important to remove the earlier node elements as the comparison starts from 0 all the time, we make a purely fresh arraylist for every node
                       * if we dont remove, the arraylist will keep on adding elements to the end and the comparison will not be made with latest node elements.
                       */ 
                       for (int remove = 0; remove < 26; remove++){
                           list1.remove(0);
                       }

                    }
                    //this will be returned to JSP page

                    ServletContext context = this.getServletContext();
                    RequestDispatcher dispatcher = context.getRequestDispatcher("/Mid");

                    // change your request and response accordingly

                    dispatcher.forward(request, response);


                    request.setAttribute("returndata",returnedData);
                    System.out.println("data1 set in arraylist for JSP page is: "+ returnedData.get(0));
                    System.out.println("extra_info set in arraylist for JSP page is: "+ returnedData.get(1));




                  } catch (Exception e) {
                    e.printStackTrace();
                  }

                  System.out.println("array list in end is: " + list1);
          } finally {

                RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/show_result_combination_2.jsp");
                requestDispatcher.forward(request,response);

          }
              }

              private static String getTagValue(String sTag, Element eElement) {
                NodeList nlList = (eElement).getElementsByTagName(sTag).item(0).getChildNodes();

                    Node nValue = (Node) nlList.item(0);

                return nValue.getNodeValue();
              }

}

有人可以帮我解决这个错误。

提前致谢。

4

2 回答 2

0

问题是您尝试在转发后写入响应。Servlet 2 可能正在提交响应。

尝试改用 RequestDispatcher.include。

于 2012-08-03T19:54:54.253 回答
0

你有一个逻辑问题。finally 块中的代码应该在 catch 块内移动几行(据我所知,如果一切正常,您希望将响应转发到 /Mid,如果没有,您希望将其重定向到 /show_result_combination_2.jsp)

如果不是这种情况,那么您应该修改您的代码,因为您有两个可能发生的前向条件。这就是导致错误的原因。如果第一个被执行,那么第二个转发产生异常

于 2012-08-03T23:12:47.470 回答