Session实战保存验证码
程序员文章站
2022-10-03 20:21:58
创建验证码导入ValidateCode.jar()创建生成验证码的ServletCreateCodeController.javapackage com.newer.servletProject.servlet.controller;import cn.dsna.util.images.ValidateCode;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;i...
创建验证码
- 导入ValidateCode.jar()
- 创建生成验证码的Servlet
CreateCodeController.java
package com.newer.servletProject.servlet.controller;
import cn.dsna.util.images.ValidateCode;
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(name = "CreateCodeController",value = "/createCode")
public class CreateCodeController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建验证码图片
ValidateCode code=new ValidateCode(200,30,4,20);
String codes=code.getCode();
//将codes存进session中
HttpSession session=request.getSession();
session.setAttribute("codes",codes);
//验证码图片响应给客户端
code.write(response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
loginMgr.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理员登录</title>
</head>
<body>
<form action="/day01web1_war_exploded/loginMgr" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
验证码:<input type="text" name="inputVcode"/><img src="/day01web1_war_exploded/createCode"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
LoginMgrController.java
package com.newer.servletProject.servlet.controller;
import com.newer.servletProject.entity.Manager;
import com.newer.servletProject.service.ManagerService;
import com.newer.servletProject.service.impl.ManagerServiceImpl;
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(name = "LoginMgrController",value = "/loginMgr")
public class LoginMgrController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.处理乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//2.收参
String username=request.getParameter("username");
String password=request.getParameter("password");
String inputVcode=request.getParameter("inputVcode");
//从request对象中获取codes
String codes=(String)request.getSession().getAttribute("codes");
//判断验证码
if(!inputVcode.isEmpty()&&inputVcode.equalsIgnoreCase(codes)){
//3.调用业务方法
ManagerService managerService=new ManagerServiceImpl();
Manager mgr=managerService.login(username,password);
//4.处理结果,流程跳转
if(mgr!=null){
//登录成功
//将管理员信息存储在session中
HttpSession session=request.getSession();
session.setAttribute("mgr",mgr);
//跳转,目标,方式
response.sendRedirect("/day01web1_war_exploded/showallController");
}else {
//登录失败
response.sendRedirect("/day01web1_war_exploded/loginMgr.html");
}
}else {
response.sendRedirect("/day01web1_war_exploded/loginMgr.html");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
本文地址:https://blog.csdn.net/weixin_44364444/article/details/109555726
上一篇: 名士贾谊的才能怎么样?他为何得不到重用?
下一篇: cdr怎么绘制一个灯箱广告海报?