【问题标题】:Html file cannot find Servlet with link givenHtml 文件找不到带有给定链接的 Servlet
【发布时间】:2020-02-28 17:17:55
【问题描述】:

我无法让我的 index.html 文件调用我的 getEmployees.java servlet,而且我不知道为什么它不起作用,因为我使用的是 servlets url 模式。 然后 getEmployees 方法应该将控制权转发给 listEmployees.jsp 文件。

点击html页面中的超链接时出现错误;

HTTP 状态 404 - /getEmployees

输入状态报告

消息/getEmployees

说明请求的资源不可用。

Apache Tomcat/8.0.3

还有代码;

<html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div></div>


        <a  href="/getEmployees"> View Employees </a>


    </body>
</html>
@WebServlet(urlPatterns = {"/getEmployees"}) 

public class getEmployees extends HttpServlet {

      /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {


           Employee E1 = new Employee("Rick Astley", 1234);
           Address A1 = new Address("63 Elm Street");
          Position P1   = new Position("Gentleman",20000);
           E1.setAddress(A1);
           E1.setPosition(P1);

             Employee E2 = new Employee("Jesus", 5555);
           Address A2 = new Address("desert");
          Position P2   = new Position("Full-time son of God",0);
           E1.setAddress(A2);
           E1.setPosition(P2);


            List<Employee> storage = new ArrayList<Employee>();
            storage.add(E1);
            storage.add(E2);

            HttpSession session = request.getSession();
             session.setAttribute("List", storage);

             RequestDispatcher dis=request.getRequestDispatcher("listEmployees.jsp");  
                dis.forward(request,response);


            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet listEmployees</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet listEmployees at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

【问题讨论】:

    标签: html servlets hyperlink


    【解决方案1】:

    出于某种原因,删除了 servlet 并将其重命名为 NewServlet 并重写了修复此问题的代码。我不知道为什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 2022-09-27
      • 2011-08-02
      • 2022-10-15
      相关资源
      最近更新 更多