session+验证码 学习

分析
LoginServlet类
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置request编码
request.setCharacterEncoding("utf-8");
//获取参数
String username = request.getParameter("username");
String password = request.getParameter("password");
String checkcode = request.getParameter("checkcode");
//判断验证码是否正确
HttpSession session = request.getSession();
//获取验证码的值
String checkCode_session = (String) session.getAttribute("checkCode");
//一获取完,就立马删除,以保证验证码的一次性
session.removeAttribute("checkCode");
if (checkCode_session!=null&&checkCode_session.equalsIgnoreCase(checkcode)){//忽略大小写比较
//验证码正确
//1.判断验证码和密码是否一致
if ("zhangsan".equals(username)&&"123".equals(password)){//完善是需要调用userDao查询数据库的
//登陆成功
//1.存储用户信息
session.setAttribute("user",username);
//2.重定向success.jsp
response.sendRedirect(request.getContextPath()+"/success.jsp");
}else {
//登陆失败
//1.存储信息到request域中
request.setAttribute("login_error","用户名或密码 不正确");
//2.转发
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}else {
//验证码不正确
//1.存储信息到request域中
request.setAttribute("cc_error","验证码不正确");
//2.转发
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
验证码类Servlet
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width=100;
int height=50;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.green);
g.drawRect(0,0,width,height);
g.setColor(Color.yellow);
g.fillRect(0,0,width-1,height-1);
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new Random();
g.setColor(Color.magenta);
StringBuilder sb = new StringBuilder();
for (int i=1;i<=4;i++){
int index = random.nextInt(str.length());
char ch=str.charAt(index);
g.drawString(ch+"",i*width/5,height/2);
sb.append(ch);
}
String checkCode_session = sb.toString();
HttpSession session=request.getSession();
session.setAttribute("checkCode",checkCode_session);
System.out.println("验证码是 : " + checkCode_session);
//画干扰线
g.setColor(Color.green);
for (int i = 0; i < 16; i++) {
int x1=random.nextInt(width);
int x2=random.nextInt(width);
int y1=random.nextInt(height);
int y2=random.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
<script>
window.onload=function () {
var img = document.getElementById('img');
img.onclick=function () {
var date=new Date().getTime();
// img.src="/session/checkCodeServlet?"+date
this.src="/session/checkCodeServlet?time="+date
}
}
</script>
<style>
div{
color: red;
}
</style>
</head>
<body>
<form action="/session/loginServlet">
<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="/session/checkCodeServlet"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"></td>
</tr>
</table>
</form> <div><%= request.getAttribute("login_error")==null?"":request.getAttribute("login_error")%></div>
<div><%= request.getAttribute("cc_error")==null?"":request.getAttribute("cc_error")%></div> </body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body> 欢迎登录,<h1><%=request.getSession().getAttribute("user") %></h1>
</body>
</html>
session+验证码 学习的更多相关文章
- 网站启动SSL, http变为https后,session验证码错误解决方法
网站启动SSL, http变为https后,session验证码错误解决方法 最近公司需要后台启动安全证书,证书安装完毕后,后台老提示 验证码错误,经过几天的研究,此问题已经得到有效解决,现把方法 ...
- django学习之- 动态验证码学习
实例:通过前台和后台,实现用户登录页面动态图片验证码校验,图片验证码部分使用Pillow模块实现,作为单独学习部分记录. 前端: <!DOCTYPE html> <html lang ...
- ASP.NET图片验证码学习!
1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...
- .Net Core Session验证码
1.验证码帮助类 namespace IdeaCore.Services.Common { public class ValidateCodeService : IValidateCodeServic ...
- node(4)express 框架 EJS模板,cookie, session的学习
一.EJS 概述:前端咱们使用过的一个模板套路,是underscore的套路.接下来EJS它属于后台工程师人的模板. https://www.npmjs.com/package/ejs 官网地址 特点 ...
- think php 登录 (session+验证码)
..........表单页面 <!DOCTYPE html> {__NOLAYOUT__} <html lang="en"> <head> &l ...
- php,session验证码不一致慢半拍
这种问题遇到过一次,后来忘了怎么解决了,所以做下笔记 输出的$_SESSION['code']之所以比图片慢了‘一帧’,这也纯属正常情况因为输出的图片是一个连接一次调用,而echo $_SESSION ...
- 会话控制:cookie和session基础学习笔记
在多次HTTP连接间维护用户与同一用户发出的不同请求之间关联的情况称为维护一个会话(session) 我们可以简单理解为浏览器的开关. 其实对cookie和session也是主要为curd操作 coo ...
- 一个简单的session传值学习
a.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
随机推荐
- 阿里云服务器yum源更新问题
阿里云官网也给出了yum卸载重装以及修改源为阿里云内网的文档.步骤这里就不说了,可点击下面的链接进行参考 https://help.aliyun.com/knowledge_detail/670864 ...
- 在.slurm文件中激活Anaconda环境
超算中心使用slurm作为集群调度.原始slurm脚本如下: source activate tensorflow-gpu python neural_style.py --content conte ...
- SpringSecurity——默认过滤器链
介绍Spring Security默认的过滤器链,介绍顺序按照过滤器在过滤器链中的顺序排序 1.WebAsyncManagerIntegrationFilter 将Security上下文与Spring ...
- jenkins安装以及自由风格的项目搭建(jenkins+tomcat+svn)
jenkins提供了直接通过war包启动以及通过tomcat容器启动的启动方法,这里使用tomcat来启动jenkins,这也是我觉得更稳定的方法. 提前搭建好tomcat环境,这里我使用的版本是:A ...
- 一、基础篇--1.2Java集合-HashMap死循环问题
为什么HashMap会出现死循环 首先,我们知道java的HashMap不是线程安全的.多线程下应该使用ConcurrentHashMap. HashMap底层是基于动态数组和单向链表(JDK1.7, ...
- android canvas drawtext 字高
Paint pFont = new Paint(); Rect rect = new Rect(); pFont.getTextBounds("豆", 0, 1, rect); L ...
- json-lib json反序列化——日期转换
将json格式的字符串转为对象,其中key-value有将String的日期转为Date类型,怪现象就是,转出来的Date类型的值是当前的系统时间. 网上有许多答案,在反序列化之前需要注册Date解析 ...
- TensorFlow 学习(3)——MNIST机器学习入门
通过对MNIST的学习,对TensorFlow和机器学习快速上手. MNIST:手写数字识别数据集 MNIST数据集 60000行的训练数据集 和 10000行测试集 每张图片是一个28*28的像素图 ...
- SQL Server批量备份数据库
--批量备份数据库201911 --开启文件夹权限 GO SP_CONFIGURE RECONFIGURE GO SP_CONFIGURE RECONFIGURE GO DECLARE ), ), ) ...
- python导入csv文件出现SyntaxError问题分析
python导入csv文件出现SyntaxError问题分析 先简单描述下碰到的题目,要求是写出2个print的结果 可以看到,a指向了一个列表list对象,在Python中,这样的赋值语句,其实内部 ...