gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:33:06 36 4
gpt4 key购买 nike

<分区>

我刚刚开始使用 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.

任何人都可以解释我应该把类(class)放在哪里才能正确调用它吗?谢谢。

更新:

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

}

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com