Html和Servlet HTTP状态404错误

时间:2015-07-21 16:28:20

标签: html http servlets http-status-code-404 status

美好的一天!请,我是Servlet的新手,并且很难理解为什么我一直收到这个错误 HTTP状态404 - 未找到 输入状态报告 消息未找到 说明请求的资源不可用。 GlassFish Server开源4.1版

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>

2 个答案:

答案 0 :(得分:0)

我建议您从帖子中删除您的用户名和密码。 404是未找到状态代码。我没有在您的代码中看到您尝试访问的端点,但您获得的响应意味着无法在您正在寻找资源的位置找到它。

答案 1 :(得分:0)

您尚未在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>
相关问题