简体   繁体   English

java.lang.IllegalStateException:在提交响应后无法转发

[英]java.lang.IllegalStateException: Cannot forward after response has been committed

I know this has been discusses at least a million times here but every servlet was redirecting or flushing the output before calling forward(). 我知道这已经讨论了至少一百万次,但每个servlet在调用forward()之前重定向或刷新输出。 I have the same problem but I am not doing anything with the output. 我有同样的问题,但我没有做任何输出。 My servlet just takes the request parameters and commits to the database and sets an attribute on the request. 我的servlet只接受请求参数并提交到数据库并在请求上设置属性。 Then, it forwards the request to a jsp which them displays the attribute. 然后,它将请求转发给它们显示属性的jsp。 I am using Servlet 3.0 on Tomcat 7. Here is my servlet doPost method followed byt the jsp that this is forwarding to: 我在Tomcat 7上使用Servlet 3.0。这是我的servlet doPost方法,然后是转发到的jsp:

@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        super.doPost(req, resp);

        DAOFactory daoFactory = DAOFactory.getFactory();
        daoFactory.getCompanyDAO().beginTransaction();

        Company company = new Company();
        company.setName(req.getParameter("companyName"));
        company.setContactEmail(req.getParameter("companyEmail"));
        company.setContactPhone(new Long(req.getParameter("companyMobile")));
        company.setAddressLine1(req.getParameter("companyAddressLine1"));
        company.setAddressLine2(req.getParameter("companyAddressLine2"));
        company.setCity(req.getParameter("companyCity"));
        company.setZipcode(Integer.parseInt(req.getParameter("companyZip")));
        company.setState(req.getParameter("companyState"));     
        company = daoFactory.getCompanyDAO().save(company);
        daoFactory.getCompanyDAO().commitTransaction();

        Employee owner = new Employee();
        owner.setFirstname(req.getParameter("ownerFirstName"));
        owner.setLastname(req.getParameter("ownerLastName"));
        owner.setEmail(req.getParameter("ownerEmail"));
        owner.setMobileNum(new Long(req.getParameter("ownerCellPhone")));
        owner.setZipcode(Integer.parseInt(req.getParameter("ownerZip")));
        owner.setRole("Employer");
        owner.setCompany(company);
        daoFactory.getEmployeeDAO().beginTransaction();
        owner = daoFactory.getEmployeeDAO().save(owner);
        daoFactory.getEmployeeDAO().commitTransaction();

        company.addEmployee(owner);
        company.setOwnerId(owner.getId());

        daoFactory.getCompanyDAO().beginTransaction();
        company = daoFactory.getCompanyDAO().save(company);
        daoFactory.getCompanyDAO().commitTransaction();

        req.setAttribute("company", company);
        RequestDispatcher rd = getServletConfig().getServletContext().getRequestDispatcher("/jsp/companyConfirmation.jsp");
        rd.forward(req, resp);

    }

JSP: JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>The Company was saved successfully</h3>
<p>
Company name: ${company.name}
</p>
</body>
</html>

I am sure I may have missed something very trivial. 我相信我可能错过了一些非常微不足道的事情。 I cannot figure out what it is especially when I am not writing anything to the output before forwarding the request. 我无法弄清楚它是什么,特别是当我在转发请求之前没有向输出写任何东西时。

PS: I also tried putting a return; PS:我也尝试过return; statement after the forward but no change. 前进后的声明但没有变化。

Thanks! 谢谢!

You should remove the super.doPost(req, resp) method call. 您应该删除super.doPost(req, resp)方法调用。 The default implementation of doPost method from HTTPServlet returns HTTP 405 status code meaning "Method not supported" and that's the response which has been committed. HTTPServlet的doPost方法的默认实现返回HTTP 405状态代码,意思是“不支持方法”,这是已提交的响应。 Therefore, you can not forward your request to other jsp. 因此,您无法将您的请求转发给其他jsp。

This is part of the RequestDispatcher.forward(ServletRequest req, ServletResponse resp) method description: "forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward". 这是RequestDispatcher.forward(ServletRequest req, ServletResponse resp)方法描述的一部分:“应该在响应提交给客户端之前调用forward(在刷新响应体输出之前)。如果响应已经提交,此方法抛出IllegalStateException。响应缓冲区中的未提交输出在转发前自动清除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:添加`return`后提交响应后无法转发 - java.lang.IllegalStateException: Cannot forward after response has been committed after add `return` java.lang.IllegalStateException:在servletfilter中提交响应后无法转发 - java.lang.IllegalStateException: Cannot forward after response has been committed in servletfilter java.lang.IllegalStateException:PWC1227:提交响应后无法转发……为什么要发送? - java.lang.IllegalStateException: PWC1227: Cannot forward after response has been committed…why it was comming? java.lang.IllegalStateException:在404上提交响应后无法转发 - java.lang.IllegalStateException: Cannot forward after response has been committed on 404 随机java.lang.IllegalStateException:提交响应后无法转发 - Random java.lang.IllegalStateException: Cannot forward after response has been committed java.lang.IllegalStateException:提交响应后无法转发,位于com.java.QTD.QuestionOfTheDay.doGet() - java.lang.IllegalStateException: Cannot forward after response has been committed, at at com.java.QTD.QuestionOfTheDay.doGet() Spring boot:java.lang.IllegalStateException:响应提交后无法调用sendError() - Spring boot : java.lang.IllegalStateException: Cannot call sendError() after the response has been committed JSF 2.2 java.lang.IllegalStateException:提交响应后无法创建会话 - JSF 2.2 java.lang.IllegalStateException: Cannot create a session after the response has been committed Spring java.lang.IllegalStateException:在提交响应后无法创建会话 - Spring java.lang.IllegalStateException: Cannot create a session after the response has been committed java.lang.IllegalStateException - 在提交响应后无法创建会话 - java.lang.IllegalStateException - Cannot create a session after the response has been committed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM