ここでは少なくとも100万回は議論されていますが、すべてのサーブレットがforward()を呼び出す前に出力をリダイレクトまたはフラッシュしていたことが分かります。私は同じ問題を抱えていますが、出力に関して何もしていません。私のサーブレットはリクエストパラメータを受け取り、データベースにコミットし、リクエストに属性を設定します。次に、要求をjspに転送して、属性を表示します。 IここでのTomcat 7にサーブレット3.0を使用しています私のサーブレットのdoPostメソッドは、これがに転送されていることJSP BYT続いている:java.lang.IllegalStateException:応答がコミットされた後に転送できません
@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;
ステートメントを転送した後に変更を加えてみました。
ありがとうございます!
super.doPost()が実行しているコードは何ですか? – Marvo
問題を引き起こしていたのはsuper.doPost()でした。ヒントをありがとう。 – motiver