利用servlet 编写登录页面,页面中有userID和password,前端校验是userID不能出现字母,密码必须6位;校验通过 # 跟数据库数据比较,如果存在该人将所有user数据展示到表格回到浏览器;否则弹框登录失败跳转回原来的登录页面
时间: 2024-02-28 17:56:11 浏览: 112
servlet页面验证用户名密码
好的,下面是使用 Servlet 编写的登录页面示例:
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<style type="text/css">
#login-form {
margin: 50px auto;
width: 300px;
border: 1px solid #ccc;
padding: 20px;
}
input[type="text"], input[type="password"] {
margin-bottom: 10px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="login-form">
<h2>Login</h2>
<form id="login" method="post" action="login">
<label for="userID">User ID:</label><br>
<input type="text" id="userID" name="userID"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
```
Java 代码:
```java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的信息
String userID = request.getParameter("userID");
String password = request.getParameter("password");
// 前端校验
if (!userID.matches("\\d+")) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<script type=\"text/javascript\">");
out.println("alert('User ID should only contain numbers!');");
out.println("window.location.href = 'login.html';");
out.println("</script>");
return;
}
if (password.length() != 6) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<script type=\"text/javascript\">");
out.println("alert('Password should be 6 characters long!');");
out.println("window.location.href = 'login.html';");
out.println("</script>");
return;
}
// 向数据库查询用户信息
// TODO: 在这里写查询数据库的代码,如果存在该用户,则将其数据返回
if (/* 比较成功 */) {
// 将用户数据传递给 JSP 页面进行展示
request.setAttribute("userData", /* 用户数据 */);
request.getRequestDispatcher("user.jsp").forward(request, response);
} else {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<script type=\"text/javascript\">");
out.println("alert('Login failed!');");
out.println("window.location.href = 'login.html';");
out.println("</script>");
}
}
}
```
JSP 代码(user.jsp):
```jsp
<!DOCTYPE html>
<html>
<head>
<title>User Info</title>
</head>
<body>
<table>
<tr>
<th>User ID</th>
<th>Name</th>
<th>Age</th>
<!-- TODO: 在这里添加其他用户数据的表头 -->
</tr>
<tr>
<td><%= request.getAttribute("userData").getUserID() %></td>
<td><%= request.getAttribute("userData").getName() %></td>
<td><%= request.getAttribute("userData").getAge() %></td>
<!-- TODO: 在这里添加其他用户数据的内容 -->
</tr>
</table>
</body>
</html>
```
注意,上述代码仅为示例,实际情况中需要根据具体需求进行修改。
阅读全文