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

时间:2013-12-07 16:07:01

标签: java eclipse authentication servlets

我刚刚开始使用servlet并尝试实现一个简单的基于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>

目录结构:

enter image description here

的web.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>

结果是:

HTTP Status 404 - /LogonServlet

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

任何人都可以解释一下我应该把课程放在哪里才能正确调用它吗?感谢。

更新

LogonServlet.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.");
    }
}

}

1 个答案:

答案 0 :(得分:3)

  1. 您正在调用/ LogonServlet然后重定向到../Home.jsp和../Login.jsp
  2. 从web.xml中删除“/”
  3. 删除“@WebServlet(”/ LogonServlet“)”这一行,这是关于web.xml的重复定义
  4. 最后让我建议不要使用自定义登录,使用Realm和JEE身份验证模块它更容易,更便携