私のプロジェクトでは、ユーザーを追加/編集/削除できるメインページBrowse.jspを使用しています。そして、「追加」、「編集」、「削除」の3つのボタンがあります。しかし、ボタンを押すとエラーが発生します。
これは私のBrowseServletです
public class BrowseServlet extends HttpServlet {
protected void service (HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
if (req.getParameter("addButton") !=null){
add (req, resp);
} else if (req.getParameter("editButton") !=null){
edit (req, resp);
} else if (req.getParameter("deleteButton") !=null){
delete (req, resp);
} else if (req.getParameter("detailsButton") !=null){
details (req, resp);
}
browse(req, resp);
}
private void details(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
}
private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
// TODO Auto-generated method stub
}
private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String idStr = req.getParameter("id");
if (idStr == null || idStr.trim().length() == 0) {
req.setAttribute("error", "You must select the user");
req.getRequestDispatcher("/browse.jsp").forward(req, resp);
return;
}
try{
User user = DaoFactory.getInstance().getUserDao().find(new Long(idStr));
req.getSession().setAttribute("user", user);
} catch (Exception e) {
req.setAttribute("error", "ERROR:" + e.toString());
req.getRequestDispatcher("/browse.jsp").forward(req, resp);
return;
}
req.getRequestDispatcher("/edit").forward(req, resp);
}
private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/add").forward(req, resp);
}
private void browse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Collection users;
try {
users = DaoFactory.getInstance().getUserDao().findAll();
req.getSession().setAttribute("users", users);
req.getRequestDispatcher("/browse.jsp").forward(req, resp);
} catch (DatabaseException e) {
throw new ServletException(e);
}
}
}
これは私のAddServletです
public class AddServlet extends EditServlet {
protected void processUser(User user) throws DatabaseException {
DaoFactory.getInstance().getUserDao().create(user);
}
protected void showPage(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.getRequestDispatcher("/add.jsp").forward(req, resp);
}
}
そしてこれは私のWEB-INFBROWSE.jspです
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head><title>User management </title></head>
<body>
<form action="<%=request.getContextPath()%>/browse" method="post">
<table id="UserTable" border="1">
<tr>
<th></th>
<th>First Name</th>
<th>Last Name</th>
<th>Date of birth</th>
</tr>
<c:forEach var="user" items="${session.Scope.users}">
<tr>
<td><input type="radio" name="id" id="id" value="${user.id}"</td>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.dateOfBirth}</td>
</tr>
</c:forEach>
</table>
<input type="submit" name="addButton" value="Add">
<input type="submit" name="editButton" value="Edit">
<input type="submit" name="deleteButton" value="Delete">
<input type="submit" name="detailsButton" value="Details">
</form>
<c:if test="${requestScope.error != null}">
<script>
alert('${requestScope.error}');
</script>
</c:if>
</body>
</html>
ADD.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head><title>User management. Add </title></head>
<body>
<form action="<%=request.getContextPath()%>/add" method="post">
First name <input type="text" name="firstName" value=""><br>
Last name <input type="text" name="lastName" value=""><br>
Date of birth <input type="text" name="date" value=""><br>
<input type="submit" name="okButton" value="Ok">
<input type="submit" name="cancelButton" value="Cancel">
</form>
<c:if test="${requestScope.error != null}">
<script>
alert('${requestScope.error}');
</script>
</c:if>
</body>
</html>
しかし、「addButton」をプッシュしようとすると、この間違いがあります
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.IllegalStateException: Cannot forward after response has been committed
com.rudniev.usermanagment.web.BrowseServlet.browse(Unknown Source)
com.rudniev.usermanagment.web.BrowseServlet.service(Unknown Source)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.12 logs.