验证码升级版
loginServlet
点击查看代码
package servlet;
import javax.servlet.ServletContext;
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("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置req编码
req.setCharacterEncoding("utf-8");
//获取参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String checkCode = req.getParameter("checkCode");
HttpSession session = req.getSession();
//每次登陆前就清除session缓存
session.removeAttribute("cc_error");
session.removeAttribute("up_error");
//从session中获取正确验证码
String check_code = (String) session.getAttribute("check_code");
// System.out.println(checkCode);
// System.out.println(check_code);
session.removeAttribute("check_code");
//判断
if(check_code!=null && check_code.equalsIgnoreCase(checkCode)){
//验证码正确
//判断用户名和密码
if(("lfqyj".equals(username))&&"123456".equals(password)){
//登录成功
//存储登录用户的信息
session.setAttribute("success","恭喜你登陆成功!");
session.setAttribute("s_username",username);
//跳转到success.jsp,重定向
String contextPath = req.getContextPath();
resp.sendRedirect(contextPath+"/success.jsp");
}else {
//登陆失败
//将提示信息存到session中
session.setAttribute("up_error","用户名或密码错误");
//转发到登录页
req.getRequestDispatcher("/login.jsp").forward(req,resp);
}
}else{
session.removeAttribute("check_code");
//将提示信息存到session中
session.setAttribute("cc_error","验证码错误");
//转发到登录页
req.getRequestDispatcher("/login.jsp").forward(req,resp);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<form action="/Spring_MVC/loginServlet" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="checkCode"></td>
</tr>
<tr>
<td colspan="2"><img id="img" src="/Spring_MVC/CheckCodeServlet"></td>
</tr>
<tr>
<td><input type="submit" name="登录"></td>
</tr>
</table>
</form>
<div style="color: red"><%=request.getSession().getAttribute("cc_error")==null? "":request.getSession().getAttribute("cc_error")%></div>
<div style="color: red"><%=request.getSession().getAttribute("up_error")==null? "":request.getSession().getAttribute("up_error")%></div>
</body>
<script>
window.onload= function () {
var img = document.getElementById("img");
img.onclick = function () {
//加时间戳
var date = new Date().getTime();
img.src = "/Spring_MVC/CheckCodeServlet?" + date;
// 清除错误信息显示
var errorDivs = document.querySelectorAll("div[style='color: red']");
errorDivs.forEach(function (div) {
div.innerHTML = "";
});
};
// 当用户开始输入时清除对应错误信息
document.querySelector("input[name='checkCode']").oninput = function () {
document.querySelector("div[style='color: red']").innerHTML = "";
};
};
</script>
</html>
点击查看代码
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2025/5/29
Time: 22:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>success</title>
</head>
<body>
<h1><%=request.getSession().getAttribute("s_username")%>,欢迎!</h1>
</body>
</html>