Java HTTP 状态 404 - 请求的资源不可用。一个简单的servlet认证系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20443449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:57:54  来源:igfitidea点击:

HTTP Status 404 - The requested resource is not available. A simple servlet authentication system

javaeclipseauthenticationservlets

提问by Dozent

I just started to play around with servlets and trying to implement a simple java-based authentication system. I have a login form through which send the login data and if its correct redirect to home page. The login form is like in below code:

我刚开始玩 servlets 并尝试实现一个简单的基于 java 的身份验证系统。我有一个登录表单,通过它发送登录数据以及是否正确重定向到主页。登录表单类似于以下代码:

<form name="frmLogin" action="/LogonServlet" method="POST">
    <table border="1">
        <tr>
            <td colspan="2"><c:out value="${errorMsg}"/> </td></tr>
        <tr>
            <td>User Name: </td>
            <td><input type="text" name="username" /></td></tr>
        <tr>
            <td>Password: </td>
            <td><input type="password" name="password" /></td></tr>
        <tr>
            <td><input type="submit" name="Submit" value="Submit"/></td></tr>
    </table>
</form>

The directory structure:

目录结构:

enter image description here

enter image description here

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>TestProject</display-name>
    <servlet>
        <display-name>LogonServlet</display-name>
        <servlet-name>LogonServlet</servlet-name>
        <servlet-class>test.cc.project.LogonServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LogonServlet</servlet-name>
        <url-pattern>/LogonServlet</url-pattern>
    </servlet-mapping>
</web-app>

The result is:

结果是:

HTTP Status 404 - /LogonServlet

type Status report
message /LogonServlet
description The requested resource is not available.

Can anyone explain me where I should put the class in order to call it correctly? Thanks.

谁能解释一下我应该把课程放在哪里才能正确调用它?谢谢。

UPDATE:

更新:

LogonServlet.java

登录Servlet.java

@WebServlet("/LogonServlet")
public class LogonServlet extends HttpServlet {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
private static final String DB_USERNAME = "test";
private static final String DB_PASSWORD = "test";
private static final String LOGIN_QUERY = "SELECT * FROM `accounts` WHERE uname=? AND passwd=?";

private static final String HOME_PAGE = "../Home.jsp";
private static final String LOGIN_PAGE = "../Login.jsp";

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String strUserName = request.getParameter("username");
    String strPassword = request.getParameter("password");
    String strErrMsg = null;
    HttpSession session = request.getSession();
    boolean isValidLogon = false;

    try {
        isValidLogon = authenticateLogin(strUserName, strPassword);
        if(isValidLogon) {
            session.setAttribute("username", strUserName);
        } else {
            strErrMsg = "Username or Password is invalid. Please try again.";
        }
    } catch(Exception e) {
        strErrMsg = "Unable to validate user/password in database";
    }

    if(isValidLogon) {
        response.sendRedirect(HOME_PAGE);
    } else {
        session.setAttribute("errorMsg", strErrMsg);
        response.sendRedirect(LOGIN_PAGE);
    }

}

private boolean authenticateLogin(String strUserName, String strPassword) throws Exception {
    boolean isValid = false;
    Connection conn = null;

    try {
        conn = getConnection();
        PreparedStatement prepStmt = conn.prepareStatement(LOGIN_QUERY);
        prepStmt.setString(1, strUserName);
        prepStmt.setString(2, strPassword);
        ResultSet rs = prepStmt.executeQuery();
        if(rs.next()) {
            System.out.println("User login is valid in DB");
            isValid = true;
        }
    } catch(Exception e) {
        System.out.println("validateLogon: Error while validating password: " +e.getMessage());
        throw e;
    } finally {
        closeConnection(conn);
    }

    return isValid;
}

private Connection getConnection() throws Exception {
    Connection conn = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
        if (conn != null) {
            System.out.println("Connected to the database");
        }
    } catch(SQLException sqle) {
        System.out.println("SQLException: Unable to open connection to DB: " +sqle.getMessage());
        throw sqle;
    } catch(Exception e) {
        System.out.println("Exception: Unable to open connection to DB: " +e.getMessage());
        throw e;
    }

    return conn;
}

private void closeConnection(Connection conn) {
    try {
        if(conn!=null && !conn.isClosed()) {
            conn.close();
        }
    } catch(SQLException sqle) {
        System.out.println("Error while closing connection.");
    }
}

}

采纳答案by venergiac

  1. you are calling /LogonServlet then you redirect to ../Home.jsp and ../Login.jsp
  2. remove the "/" from web.xml
  3. remove the line "@WebServlet("/LogonServlet")" which is a duplicate definition in respect of web.xml
  1. 您正在调用 /LogonServlet 然后重定向到 ../Home.jsp 和 ../Login.jsp
  2. 从 web.xml 中删除“/”
  3. 删除 "@WebServlet("/LogonServlet")" 行,它是关于 web.xml 的重复定义

finally let me suggest to NOT use custom login, use Realm and JEE authentication module it is easier and more portable

最后让我建议不要使用自定义登录,使用 Realm 和 JEE 身份验证模块它更容易和更便携