Session实战保存验证码
程序员文章站
2022-05-04 14:24:01
创建验证码导入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
上一篇: 北魏早期为什么会流行贪污之风?有什么影响
下一篇: python实现飞机大战微信小游戏
推荐阅读
-
Android项目实战(五十八):Android 保存图片文件到本地,相册/图库查看不到的处理
-
Session保存到数据库的php类分享
-
PHP保存session到memcache服务器的方法
-
PHP 图像处理与SESSION制作超简单验证码的方法示例
-
php自定文件保存session的方法
-
PHP自定session保存路径及删除、注销与写入的方法
-
PHP利用MySQL保存session的实现思路及示例代码
-
php中将一个对象保存到Session中的方法
-
安全校验Session验证码并避免绕开验证码攻击
-
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例