0

Good day! Please, I am new to Servlet and having hard time understanding why I keep getting this error HTTP Status 404 - Not Found type Status report message Not Found description The requested resource is not available. GlassFish Server Open Source Edition 4.1

The Servlet code:

       /*
     * 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();
    }

}

The html code is :

<!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>

The 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>
Benjamin C
  • 11
  • 1
  • 2

2 Answers2

0

I would advise you to remove your username and password from your post. A 404 is a Not Found status code. I don't see the endpoint you are trying to hit in your code but the response you are getting means that it cannot be found at the location you are looking for the resource at.

Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19
0

You haven't made entry of servlet in web.xml. Server can not find the servlet when no entry of it is present in web.xml. Entry will be something like this

<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>
Sumit Gupta
  • 437
  • 4
  • 12