【问题标题】:Servlet 404 error on get request upon submitting the form提交表单时获取请求时出现 Servlet 404 错误
【发布时间】:2018-02-03 19:58:54
【问题描述】:

单击表单上的提交按钮时出现 404 错误。 URL 映射http://localhost:8080/HelloWorld/HelloServlet 出现错误(请求的资源不可用。)。我也尝试了这个参考,Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available",但这似乎也不起作用。

1、索引、html

   <!DOCTYPE html>
   <html>
   <head>
   <meta charset="ISO-8859-1">
   <title>Insert title here</title>
   </head>
   <body>
      <form action="HelloServlet">
        <input type="submit" value="HIT">
      </form>
   </body>
   </html>

2。 HelloServlet

        package com.example.aman;

        import java.io.IOException;
        import javax.servlet.ServletConfig;
        import javax.servlet.ServletException;
        import javax.servlet.annotation.WebServlet;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;

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

        /**
        * Default constructor. 
        */
        public HelloServlet() {
        // TODO Auto-generated constructor stub
        }

        /**
         * @see Servlet#init(ServletConfig)
        */
        public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("DIVESH");
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, 
        HttpServletResponse response)
        */
        protected void doGet(HttpServletRequest request, HttpServletResponse 
        response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: 
        ").append(request.getContextPath());
        response.getWriter().println("DIVESH");
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, 
         HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

3. web.xml

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HelloWorld</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

【问题讨论】:

    标签: forms servlets get action


    【解决方案1】:

    http://localhost:8080/HelloWorld/HelloServlet.

    直接告诉我问题出在哪里。您正在尝试从同一文件夹目录中访问 servlet。如果您对 relative URLS 不小心,就会发生这种情况

    在您的表单中,将 URL 映射更改为:

       <form action="/HelloServlet">
            <input type="submit" value="HIT">
          </form>
    

    如果这不起作用,请执行以下操作:

       <form action="../HelloServlet">
            <input type="submit" value="HIT">
          </form>
    

    在路径中添加 / 使其成为absolute

    添加 ../ 使其转到当前目录的父目录。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-06
      • 2017-08-24
      • 2014-06-06
      • 1970-01-01
      • 2021-01-12
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多