在我的项目中,我使用主页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-INF
浏览器

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

最佳答案

无论调用什么动作,您总是也调用browse()。如此有效地使您在调用动作(反过来已经执行了动作)时执行第二次向前。

也许您想将其放置在else块中?

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);
} else {
    browse(req, resp);
}

09-10 21:58