1

我知道这已经在这里讨论了至少一百万次,但是每个 servlet 在调用 forward() 之前都会重定向或刷新输出。我有同样的问题,但我没有对输出做任何事情。我的 servlet 只接受请求参数并提交到数据库并在请求上设置一个属性。然后,它将请求转发给他们显示属性的 jsp。我在 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:

<%@ 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>

我确信我可能错过了一些非常微不足道的事情。我无法弄清楚它是什么,尤其是当我在转发请求之前没有向输出写入任何内容时。

PS:我也尝试return;在转发之后添加一个声明,但没有改变。

谢谢!

4

1 回答 1

4

您应该删除super.doPost(req, resp)方法调用。HTTPServlet 的 doPost 方法的默认实现返回 HTTP 405 状态代码,意思是“方法不支持”,这就是已提交的响应。因此,您不能将您的请求转发给其他 jsp。

这是RequestDispatcher.forward(ServletRequest req, ServletResponse resp)方法描述的一部分:“应该在响应提交给客户端之前(在刷新响应主体输出之前)调用 forward。如果响应已经提交,则此方法抛出 IllegalStateException。未提交的输出在响应缓冲区在转发之前自动清除”。

于 2012-05-01T23:35:21.980 回答