【问题标题】:Java Web Application Edit is Not WorkingJava Web 应用程序编辑不起作用
【发布时间】:2018-05-05 01:00:18
【问题描述】:

我正在学习 Java CRUD 操作。我正在尝试从 sql 数据库中插入、更新和删除记录。插入和显示所有记录方法正在工作,但问题是当我单击编辑和删除链接时,它会抛出 http 404 not found 异常

这是我的 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>

这是 EidtPost.jsp 的 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>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>

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

【问题讨论】:

  • 在.html页面中,引用是deleteedit,但是URL模式是/DeletePost。我可能错过了某处的映射。但是 404 是在指定路径中找不到页面/servlet。
  • 这里发布的内容太多了——您需要将帖子缩减为只发布基本内容(例如,DataAccess 不太可能涉及 404 错误)。同样,在所有这些中可能存在映射。但是,乍一看,.html 文件&lt;a href="edit?id=${p.id}"&gt;Edit&lt;/a&gt; 中的这段代码需要一个名为edit 的页面/servlet,但您的servlet 路径声明为/EditPost。这些路径不同。
  • 尝试更改为&lt;a href="./EditPost?id=${p.id}"&gt; 并查看路径是否解析。另外,请添加出现 404 错误时浏览器显示的完整路径。
  • 也就是说,从href中删除绝对引用。见some discussion here(或者你可以做href="EditPost"
  • 谢谢你,凯文。你能发表你的答案吗?我只是删除了所有 servlet 并再次添加它。现在它根据您的建议工作

标签: java servlets netbeans-8 glassfish-4


【解决方案1】:

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

最终,.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

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

<%@ 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);
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多