【问题标题】:“HTTP Status 404 The requested resource is not available”“HTTP 状态 404 请求的资源不可用”
【发布时间】:2017-12-21 05:25:15
【问题描述】:

我是 servlet 编程的新手。我检查了有关 Http 404 错误的各种其他链接,但没有任何帮助。所以我在这里发布我的代码。

我在WebContent/文件夹form1.htmlform2.htmlform3.html中有三个html表单,所有这些表单都有相同的url模式,因为在同一个Http会话中访问三个不同的表单。

form1.html

<html>
<head>
<title>Adhar Registration Form</title>
</head>
<body style="background-color:orange">
<h1>FORM 1</h1>
<form action="./reg" method="get">

<table>
<tr><td>NAME:</td><td><input type="text" name="id"/></td></tr>
<tr><td>F_NAME:</td><td><input type="text" name="name"/></td></tr>
<tr><td>M_NAME:</td><td><input type="text" name="email"/></td></tr>

<tr><td><input type="submit" name= "NEXT"> </td></tr>
</table>
<input type="hidden" name="fno" value="1">

</form>

</body>
</html>

form2.html

<html>
<head>
<title>Adhar Registration Form</title>
</head>
<body style="background-color:orange">
<h1>FORM 2</h1>

<form action="./reg" method="get">

<table>
<tr><td>CONTACT:</td><td><input type="text" name="id"/></td></tr>
<tr><td>EMAIL:</td><td><input type="text" name="name"/></td></tr>
<tr><td>ADDRESS:</td><td><textarea rows ="10" cols="5" name="address"></textarea></td></tr>

<tr><td><input type="submit" name= "NEXT"> </td></tr>
</table>
<input type="hidden" name="fno" value="2">

</form>

</body>
</html>

<html>
<head>

<title>Adhar Registration Form</title>
</head>
<body style="background-color:orange">
<h1>FORM 3</h1>
<form action="./reg" method="get">

<table>
<tr><td>QUALIFICATION:</td><td><input type="text" name="id"/></td></tr>
<tr><td>PAN NO:</td><td><input type="text" name="name"/></td></tr>

<tr><td><input type="submit" name= "Register"> </td></tr>
</table>
<input type="hidden" name="fno" value="3">

</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Adhar</display-name>
  <welcome-file-list>
    <welcome-file>form1.html</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>container.RegistrationServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/reg</url-pattern>
</servlet-mapping>
</web-app>

RegistrationServlet.java

package container;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

    public static final String URL = "jdbc:mysql://localhost/db";
    public static final String USER = "root";
    public static final String PASSWORD = "12345";
    public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();

        HttpSession hs = request.getSession();
        String fno = request.getParameter("fno");

        if(fno.equals("1")){
            String name = request.getParameter("name");
            String f_name = request.getParameter("f_name");
            String m_name = request.getParameter("m_name");

            hs.setAttribute("name", name);
            hs.setAttribute("f_name", f_name);
            hs.setAttribute("m_name", m_name);

            response.sendRedirect("./Form2.html");
        }
        if(fno.equals("2")){

            String contact = request.getParameter("contact");
            String email = request.getParameter("email");
            String address = request.getParameter("address");

            hs.setAttribute("contact", contact);
            hs.setAttribute("email", email);
            hs.setAttribute("address", address);

            response.sendRedirect("./Form3.html");
        }
        if(fno.equals("3")){

            String qual = request.getParameter("qual");
            String pan = request.getParameter("pan");

            String name = (String)hs.getAttribute("name");
            String f_name = (String)hs.getAttribute("f_name");
            String m_name = (String)hs.getAttribute("m_name");

            String contact = (String)hs.getAttribute("contact");
            String email = (String)hs.getAttribute("email");
            String address = (String)hs.getAttribute("address");

            try {
                Class.forName(DRIVER_CLASS);
                System.out.println("Loaded the driver");

                Connection con = DriverManager.getConnection(URL,USER,PASSWORD);
                PreparedStatement ps = con.prepareStatement("INSERT into adharReg values(?,?,?,?,?,?,?,?)");

                ps.setString(1, name);
                ps.setString(2, f_name);
                ps.setString(3, m_name);
                ps.setString(4, contact);
                ps.setString(5, email);
                ps.setString(6, address);
                ps.setString(7, qual);
                ps.setString(8, pan);

                int i = ps.executeUpdate();

                if(i!=0){
                    out.println("<h1>REGISTRATION SUCCESS</h1>");
                }
                else{
                    out.println("<h1>REGISTRATION FAILED</h1>");
                }

            } catch (Exception e) {
                // TODO: handle exception
                out.println("<h1>REGISTRATION FAILED" + e.getMessage() + " </h1>");
            }

        }
    }

}

我使用的是 Tomcat 服务器 8.0。我检查了这个Link 并且我的tomcat 服务器具有完全相同的设置。并关注this link 任何可能的错误,但我不知道我收到 Http 404 错误的确切原因。帮我看看为什么会出现这个错误。

谢谢。

【问题讨论】:

    标签: java servlets


    【解决方案1】:

    该问题是由 web.xml 配置和 WebServlet 注解之间的冲突引起的。 web.xml 定义了一个名为 login 的 servlet,该 servlet 以 /reg url 为目标,但在 RegistrationServlet 中也有一个声明通过 @WebServlet 注释引用相同的 url /reg。

    一种可能的解决方案是从 web.xml 中删除 servlet 声明,这意味着 web.xml 内容应该是这样的。

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>Adhar</display-name>
      <welcome-file-list>
        <welcome-file>form1.html</welcome-file>
      </welcome-file-list>
    
    </web-app>
    

    只让通过注解声明的Servlet。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      检查web.xml 是否存在于 WEB-INF 文件夹中。

      并使用 servlet 映射到 web.xml

      然后像这样在 web.xml 中注册 servlet:

      <servlet>
      <servlet-name>yourServlet</servlet-name>
      <servlet-class>com.example.YourServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
      <servlet-name>yourServlet</servlet-name>
      <url-pattern>/servlet</url-pattern>  
      </servlet-mapping>
      

      更多请参考以下链接:-

      http://www.beingjavaguys.com/2013/08/jsp-servlet-hello-world-example.html

      https://www.javatpoint.com/servlet-with-annotation

      【讨论】:

      • 您好,web.xml 仅在 WEB-INF 文件夹中创建。并且你还可以看到上面的 web.xml 文件具有相同的 servlet 注册格式。
      • reg 之前删除./。试试这个:&lt;form action="reg" method="get"&gt;
      • 在 web.xml 中使用:w3.org/2001/XMLSchema-instance" xmlns="@987654324 @" xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      • 试过没用
      • 尝试使用这个:@WebServlet(name = "reg Servlet", description = "Registration ", urlPatterns = "/reg")
      猜你喜欢
      • 2017-03-23
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 2018-04-13
      • 2011-12-09
      • 1970-01-01
      相关资源
      最近更新 更多