Html和Servlet HTTP状态404错误

问题描述:

美好的一天!拜托,我是新来的servlet和有很难理解为什么我不断收到此错误 HTTP状态404 - 找不到 类型状态报告 消息未找到 描述所请求的资源不可用。 的GlassFish Server开源版4.1Html和Servlet HTTP状态404错误

servlet代码:

 /* 
    * To change this license header, choose License Headers in Project  Properties. 
    * To change this template file, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    package simpleRegistration; 

    import java.io.IOException; 
    import java.io.PrintWriter; 
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.PreparedStatement; 
    import java.sql.SQLException; 
    import javax.servlet.ServletException; 
    import javax.servlet.http.HttpServlet; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 

    /** 
    * 
    * @author chinwe 
    */ 
    public class SimpleRegistration extends HttpServlet { 

     /** 
     * 
     */ 
    private static final long serialVersionUID = 1L; 
    private PreparedStatement pstmt; 

    @Override 
    public void init() throws ServletException { 
     initializeJdbc(); 
    } 

    /** 
    * Handles the HTTP <code>POST</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
response.setContentType("text/html"); 
     try (PrintWriter out = response.getWriter()) { 
      /* TODO output your page here. You may use following sample code. */ 
      String lastName = request.getParameter("lastName"); 
      String firstName = request.getParameter("firstName"); 
      String mi = request.getParameter("mi"); 
      String phone = request.getParameter("telephone"); 
      String email = request.getParameter("email"); 
      String address = request.getParameter("street"); 
      String city = request.getParameter("city"); 
      String state = request.getParameter("state"); 
      String zip = request.getParameter("zip"); 

      try { 
       if (lastName.length() == 0 || firstName.length() == 0) { 
        out.println("Last Name and First Name are required"); 
       } else { 
        storeStudent(lastName, firstName, mi, phone, email, 
          address, city, state, zip); 
       } 
      } catch (Exception ex) { 
       out.println("Error: " + ex.getMessage()); 
      } finally { 
       out.close(); 
      } 
     } 

    } 

    private void initializeJdbc() { 
     //To change body of generated methods, choose Tools | Templates. 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      System.out.println("Driver loaded"); 

      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/javabook", "root", "9682244umanA"); 
      System.out.println("Database connected"); 

      pstmt = conn.prepareStatement("insert into Address (lastName, firstName, mi, telephone, email, street, city," 
        + " state, zip) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"); 
     } catch (ClassNotFoundException | SQLException ex) { 
      ex.printStackTrace(); 
     } 
    } 

    private void storeStudent(String lastName, String firstName, String mi, String phone, String email, String address, String city, String state, String zip) throws SQLException { 
     pstmt.setString(1, lastName); 
     pstmt.setString(2, firstName); 
     pstmt.setString(3, mi); 
     pstmt.setString(4, phone); 
     pstmt.setString(5, email); 
     pstmt.setString(6, address); 
     pstmt.setString(7, city); 
     pstmt.setString(8, state); 
     pstmt.setString(9, zip); 
     pstmt.executeUpdate(); 
    } 

} 

的HTML代码是:

<!DOCTYPE html> 
<!-- 
To change this license header, choose License Headers in Project Properties. 
To change this template file, choose Tools | Templates 
and open the template in the editor. 
--> 
<html> 
    <head> 
     <title>Simple Registration without Confirmation</title> 
     <meta charset="ISO-8859-1"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    </head> 
    <body> 
     Please register to your instructor's student address book. 


     <form method="post" action="SimpleRegistration"> 
      <p>Last Name<font color = "#FF0000">*</font> 
       <input type="text" name="lastName"> &nbsp; 
       First Name <font color ="#FF0000">*</font> 
       <input type="text" name="firstName"> &nbsp; 
       MI <input type="text" name="mi" size="3"> 
      </p> 
      <p>Telephone 
       <input type="text" name="telephone" size="20"> &nbsp; 
       Email 
       <input type="text" name="email" size="28"> &nbsp; 
      </p> 
      <p>Street <input type="text" name="street" size="50"> 
      </p> 
      <p>City <input type="text" name="city" size="23"> &nbsp; 
       Street 
       <select size="1" name="state"> 
        <option value="GA">Georgia-GA</option> 
        <option value="OK">Oklahoma-OK</option> 
        <option value="IN">Indiana_IN</option> 
       </select> &nbsp; 
       Zip <input type="text" name="zip" size="9"> 
      </p> 
      <p><input type="submit" name="Submit" value="Submit"> 
       <input type="reset" value="Reset"> 
      </p> 
     </form><p><font color="#FF0000"> required fields</font></p> 
    </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>SimpleRegsitration</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> 

我会建议你rem从您的帖子中提取您的用户名和密码。 404是未找到的状态码。我没有看到您尝试在您的代码中尝试点击的端点,但您收到的响应意味着无法在您要查找资源的位置找到该端点。

您还没有在web.xml中输入servlet。当web.xml中不存在任何条目时,服务器找不到该servlet。 入境将是这样的

<servlet> 
     <servlet-name>SimpleRegistration</servlet-name> 
     <servlet-class>simpleRegistration.SimpleRegistration</servlet-class> 
</servlet> 

<servlet-mapping> 
     <servlet-name>SimpleRegistration</servlet-name> 
     <url-pattern>/simpleregistration</url-pattern> 
</servlet-mapping>