Java Web 应用程序编辑不工作

Java Web Application Edit is Not Working

我正在学习Java CRUD 操作。我正在尝试从 sql database.The 插入、更新和删除记录]它抛出 http 404 未找到异常

这是我的 HTML 显示所有记录的代码。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>All Posts</title>
    </head>
    <body>
        <div style="width: 1200px; margin-left: auto; margin-right: auto;">
            <table cellpadding="10">
                <tr>
                    <th>Id</th>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Detail</th>
                    <th>Category</th>
                    <th>Date</th>
                    <th>Image</th>
                    <th></th>
                </tr>
                <c:forEach items="${AllPost}" var="p">
                    <tr>
                        <td>${p.id}</td>
                        <td>${p.title}...</td>
                        <td>${p.description}...</td>
                        <td>${p.detail}...</td>
                        <td>${p.category}</td>
                        <td>${p.date}...</td>
                        <td>${p.image}...</td>
                        <td>
                            <a href="EditPost?id=${p.id}">Edit</a>
                            <a href="DeletePost?id=${p.id}">Delete</a>
                        </td>
                    </tr>
                </c:forEach>
            </table>
        </div>
    </body>
</html>

这里是 HTML EidtPost.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Edit</title>
    </head>
    <body>
        <h1>Edit News</h1>
        <div style="width: 900px; margin-left: auto; margin-right: auto">
            <c:forEach items="${getNewsById}" var="p">
                <form action="JSP/ManagerEditPost.jsp" method="post">
                    <input type="hidden" name="id" value="${p.id}">
                    Title:<br>
                    <input type="text" value="${p.title}" name="title" style="width: 200px"><br>
                    Description:<br>
                    <input type="text" value="${p.description}" name="description" style="width: 200px"><br>
                    Detail:<br>
                    <textarea name="detail" style="width: 400px; height: 200px">${p.detail}</textarea><br>
                    Category: 
                    <select name="category">
                        <option value="${p.category}">${p.category}</option>
                        <option value="World">World</option>
                        <option value="Tech">Tech</option>
                        <option value="Sport">Sport</option>
                    </select><br>
                    Image:<br>
                    <input type="text" value="${p.image}" name="image" style="width: 200px"><br>
                    <input type="submit" value="Submit">
                </form>
            </c:forEach>

        </div>
    </body>
</html>

这里是 CRUD 操作的数据访问代码。

    public void edit(int id, String title, String description, String detail, String category, String image){
        try {
            String sql = "update News SET title = ?, description = ?, detail = ?, category = ?, image = ?" + " where id = ?";
            PreparedStatement ps= DBUtils.getPreparedStatement(sql);
            ps.setString(1, title);
            ps.setString(2, description);
            ps.setString(3, detail);
            ps.setString(4, category);
            ps.setString(5, image);
            ps.setInt(6, id);
            ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


}

这是 servlet 代码。

@WebServlet(name = "EditPost", urlPatterns = {"/EditPost"})
public class EditPost extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
             {
        String idTemp = request.getParameter("id");
        int id = Integer.parseInt(idTemp);
        request.setAttribute("getNewsById", DataAccess.getNewById(id));
        RequestDispatcher rd = request.getRequestDispatcher("CRUD/EditPost.jsp");
        try {
            rd.forward(request, response);
        } catch (ServletException | IOException ex) {
            Logger.getLogger(EditPost.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

这是代码web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>EditPost</servlet-name>
        <servlet-class>servlet.EditPost</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>DeletePost</servlet-name>
        <servlet-class>servlet.DeletePost</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>AllPost</servlet-name>
        <servlet-class>servlet.AllPost</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>EditPost</servlet-name>
        <url-pattern>/EditPost</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>DeletePost</servlet-name>
        <url-pattern>/DeletePost</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AllPost</servlet-name>
        <url-pattern>/AllPost</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

这是我点击编辑和删除时的错误截图link。

以下显示了创建所需功能所需的最低限度。显然需要添加有关真正实现的所有内容。

最终,.jsp 中的路径需要与 @WebServlet 路径匹配。虽然具体转发有点依赖absolute vs. relative URLs.

这适用于 tomcat 9.0,但可能适用于其他此类服务器,例如 glassfish 等。

web.xml

这提供了基本信息。

<web-app>
  <welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
  </welcome-file-list>
</web-app>

welcome.jsp

这只是一个示例。jsp 提供了 href

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
  </head>
  <body>

    <h1>Hello World!</h1>
    <!-- note this can also be ./EditPost -->
    <!-- also note that not passing any query here -->
    <a href="EditPost">Edit Post</a>
  </body>
</html>

EditPost.java

这是带注释的 servlet 的快速示例。

@WebServlet("/EditPost")
public class EditPost extends HttpServlet {
  private static final long serialVersionUID = 1L;

  /**
   * Default constructor. 
   */
  public EditPost() {
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    doGet(request, response);
  }

}