案例需求 :
1、访问带有验证码的登录页面login.jsp
2、用户输入用户名,密码以及验证码。
如果用户名和密码输入有误,跳转登录页面,提示:用户名或密码错误。
如果验证码输入有误,跳转登录页面,提示 :验证码错误。
如果全部输入正确,则跳转到主页success.jsp,显示 : 用户名,欢迎您。
1、在 login.jsp 页面中,添加用户名、密码和验证码的输入框和提交按钮,并将验证码图片显示在页面中。使用以下代码可以生成验证码并将其存储到 Session 中:
// 生成验证码
String captcha = CaptchaUtil.generateCaptcha();
// 将验证码存储到 Session 中
HttpSession session = request.getSession();
session.setAttribute("captcha", captcha);
2、编写登录验证的 Servlet,处理用户提交的表单数据并进行验证操作。可以使用以下代码从请求参数中获取用户名、密码和验证码,并将其与 Session 中存储的验证码进行比对:
// 获取请求参数
String username = request.getParameter("username");
String password = request.getParameter("password");
String inputCaptcha = request.getParameter("captcha");
// 从 Session 中获取验证码
HttpSession session = request.getSession();
String captcha = (String) session.getAttribute("captcha");
// 验证用户名和密码是否正确
if ("admin".equals(username) && "123456".equals(password)) {
if (captcha.equalsIgnoreCase(inputCaptcha)) {
// 验证码正确,登录成功
session.setAttribute("username", username);
response.sendRedirect("success.jsp");
} else {
// 验证码错误,重新跳转到登录页面
request.setAttribute("errorMsg", "验证码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} else {
// 用户名或密码错误,重新跳转到登录页面
request.setAttribute("errorMsg", "用户名或密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
3、在 success.jsp 页面中,通过 Session 获取当前登录用户的用户名,并将其显示在页面上。可以使用以下代码获取当前登录用户的用户名:
// 从 Session 中获取用户名
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
完整部分代码:
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>登录页面</h1>
<form action="login" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
验证码:<input type="text" name="captcha"><br>
<img src="captcha" /><br>
<input type="submit" value="登录">
</form>
<c:if test="${not empty errorMsg}">
<p style="color:red;">${errorMsg}</p>
</c:if>
</body>
</html>
CaptchaUtil.java:
import java.util.Random;
public class CaptchaUtil {
public static String generateCaptcha() {
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int num = random.nextInt(10);
sb.append(num);
}
return sb.toString();
}
}
LoginServlet.java:
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取请求参数
String username = request.getParameter("username");
String password = request.getParameter("password");
String inputCaptcha = request.getParameter("captcha");
// 从 Session 中获取验证码
HttpSession session = request.getSession();
String captcha = (String) session.getAttribute("captcha");
// 验证用户名和密码是否正确
if ("admin".equals(username) && "123456".equals(password)) {
if (captcha.equalsIgnoreCase(inputCaptcha)) {
// 验证码正确,登录成功
session.setAttribute("username", username);
response.sendRedirect("success.jsp");
} else {
// 验证码错误,重新跳转到登录页面
request.setAttribute("errorMsg", "验证码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} else {
// 用户名或密码错误,重新跳转到登录页面
request.setAttribute("errorMsg", "用户名或密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Success</title>
</head>
<body>
<h1>主页</h1>
<p>${username},欢迎您!</p>
</body>
</html>