JSP / JDBC:HTTP状态404 - 未找到

时间:2016-05-14 07:16:11

标签: java jsp jdbc netbeans glassfish

我有HTTP状态404 - 未找到。

我使用Netbeans IDE和Glassfish服务器以及MySql作为数据库。 我无法理解为什么我有这个错误。 这是我的代码。我是初学者。

谢谢!

这是我的JSP文件:

<%@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>

这是我的Servlet文件:

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

}

这是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>

NetBeans中的目录结构:

enter image description here

我的输出:

enter image description here

1 个答案:

答案 0 :(得分:3)

您的Web应用程序中没有任何内容处理对上下文根http://localhost:8080/UserApplication的请求,因此servlet容器返回404 not found错误:

  • 您的servlet仅侦听http://localhost:8080/UserApplication/ServletRegister
  • 由于您已将JSP页面放入WEB-INF
  • ,因此您的JSP页面不可见

您可以将JSP页面移动到目录Web Pages,更正名称(register.jsp),然后调用http://localhost:8080/UserApplication/register.jsp