简体   繁体   中英

JSP/JDBC: HTTP Status 404 - Not Found

I am having HTTP Status 404 - Not Found.

I am using Netbeans IDE and Glassfish sever and MySql as database. I am unable to understand why I am having the error. Here are my codes.I am a beginner.

Thanks!

This is my JSP file:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>User Application Registration</title>
</head>
<body>
    <h1>Registration Page</h1>
    <form action="ServletRegister" method="post">
        Name: <input type="text" name="name"><br/>
        Password: <input type="password" name="password"><br/>
        Email: <input type="text" name="email"><br/>
        Language: <select name="language">
            <option>Hindi</option>
            <option>English</option>
            <option>Bengali</option>
        </select><br/>
        <input type="submit" value="Submit"/>
    </form>
</body>

This is my Servlet file:

package RegisterPackage;
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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletRegister extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {



    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String n = request.getParameter("name");
    String p = request.getParameter("password");
    String e  = request.getParameter("email");
    String l = request.getParameter("language");

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/demo", "root", "root");
        PreparedStatement ps = con.prepareStatement("insert into userdetails values(?,?,?,?)");

        ps.setString(1, n);
        ps.setString(2, p);
        ps.setString(3, e);
        ps.setString(4, l);

        int i = ps.executeUpdate();
        if (i > 0)
            out.print("You are successfully registered!");
    } 
    catch (Exception e2) {
        System.out.println(e2);
    }
    out.close();  
}

}

This is web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>ServletRegister</servlet-name>
        <servlet-class>RegisterPackage.ServletRegister</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletRegister</servlet-name>
        <url-pattern>/ServletRegister</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Directory structure in NetBeans:

在此处输入图片说明

My Output:

在此处输入图片说明

There is nothing in your web app which handles a request to the context root http://localhost:8080/UserApplication therefore the servlet container returns a 404 not found error:

  • Your servlet listens only to http://localhost:8080/UserApplication/ServletRegister
  • Your JSP page isn't visible since you have put it into WEB-INF

You could move the JSP page to directory Web Pages , correct the name ( register.jsp ) and then call http://localhost:8080/UserApplication/register.jsp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM