博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet之HttpSession验证码
阅读量:6860 次
发布时间:2019-06-26

本文共 4821 字,大约阅读时间需要 16 分钟。

hot3.png

index.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title here
<%=session.getAttribute("message")==null?"":session.getAttribute("message")%>
<%=request.getContextPath() %>/checkCodeServlet" method="post"> name:
checkCode:
<%=request.getContextPath() %>/validateColorServlet">
CheckCodeServletimport java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class CheckCodeServlet */@WebServlet("/checkCodeServlet")public class CheckCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L;           public CheckCodeServlet() {        super();    } protected void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException { String paramCode = request.getParameter("CHECK_CODE_PARAM_NAME"); String sessionCode = (String) request.getSession().getAttribute("CHECK_CODE_KEY"); System.out.println(paramCode); System.out.println(sessionCode); if(!(paramCode!=null && paramCode.equals(sessionCode))){ request.getSession().setAttribute("message", "验证码不一致"); response.sendRedirect(request.getContextPath()+"/check/index.jsp"); return; } System.out.println("456789"); }}ValidateColorServletimport java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class ValidateColorServlet */@WebServlet("/validateColorServlet")public class ValidateColorServlet extends HttpServlet {public static final String CHECK_CODE_KEY = "CHECK_CODE_KEY"; private static final long serialVersionUID = 1L; //设置验证图片的宽度, 高度, 验证码的个数 private int width = 152; private int height = 40; private int codeCount = 4; //验证码字体的高度 private int fontHeight = 4; //验证码中的单个字符基线. 即:验证码中的单个字符位于验证码图形左上角的 (codeX, codeY) 位置处 private int codeX = 0; private int codeY = 0; //验证码由哪些字符组成 char [] codeSequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz23456789".toCharArray(); //初始化验证码图形属性 public void init(){ fontHeight = height - 2; codeX = width / (codeCount + 2); codeY = height - 4; } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //定义一个类型为 BufferedImage.TYPE_INT_BGR 类型的图像缓存 BufferedImage buffImg = null; buffImg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); //在 buffImg 中创建一个 Graphics2D 图像 Graphics2D graphics = null; graphics = buffImg.createGraphics(); //设置一个颜色, 使 Graphics2D 对象的后续图形使用这个颜色 graphics.setColor(Color.WHITE); //填充一个指定的矩形: x - 要填充矩形的 x 坐标; y - 要填充矩形的 y 坐标; width - 要填充矩形的宽度; height - 要填充矩形的高度 graphics.fillRect(0, 0, width, height); //创建一个 Font 对象: name - 字体名称; style - Font 的样式常量; size - Font 的点大小 Font font = null; font = new Font("", Font.BOLD, fontHeight); //使 Graphics2D 对象的后续图形使用此字体 graphics.setFont(font); graphics.setColor(Color.BLACK); //绘制指定矩形的边框, 绘制出的矩形将比构件宽一个也高一个像素 graphics.drawRect(0, 0, width - 1, height - 1); //随机产生 15 条干扰线, 使图像中的认证码不易被其它程序探测到 Random random = null; random = new Random(); graphics.setColor(Color.GREEN); for(int i = 0; i < 55; i++){ int x = random.nextInt(width); int y = random.nextInt(height); int x1 = random.nextInt(20); int y1 = random.nextInt(20); graphics.drawLine(x, y, x + x1, y + y1); } //创建 randomCode 对象, 用于保存随机产生的验证码, 以便用户登录后进行验证 StringBuffer randomCode; randomCode = new StringBuffer(); for(int i = 0; i < codeCount; i++){ //得到随机产生的验证码数字 String strRand = null; strRand = String.valueOf(codeSequence[random.nextInt(36)]); //把正在产生的随机字符放入到 StringBuffer 中 randomCode.append(strRand); //用随机产生的颜色将验证码绘制到图像中 graphics.setColor(Color.BLUE); graphics.drawString(strRand, (i + 1)* codeX, codeY); } //再把存放有所有随机字符的 StringBuffer 对应的字符串放入到 HttpSession 中 request.getSession().setAttribute(CHECK_CODE_KEY, randomCode.toString()); //禁止图像缓存 response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); //将图像输出到输出流中 ServletOutputStream sos = null; sos = response.getOutputStream(); ImageIO.write(buffImg, "jpeg", sos);  sos.close(); }}

转载于:https://my.oschina.net/projerry/blog/525978

你可能感兴趣的文章
Gartner:云安全进入高速发展期
查看>>
云存储能否成为数据安全灵药?几个角度全方位剖析
查看>>
未来几年SDN将进一步提升云服务利润率
查看>>
手把手教你用 Python 和 Scikit-Learn 实现垃圾邮件过滤
查看>>
Hinton亲自讲解迄今未发表工作:胶囊理论的核心概念到底是什么?
查看>>
公开课总结发布《云数据库实现原理和海量运维方法》
查看>>
预告:如何完成从学术科研到产业创新的华丽转身?| 硬创公开课
查看>>
《C++语言入门经典》一2.3 数据的输入与输出
查看>>
阿里云ECS通过docker配置MySQL--MGR
查看>>
光伏业需要一次国内“双反”
查看>>
小微企业都在用的一体化管理解决方案
查看>>
Sql Server 2008 为开发带来的新特性
查看>>
Realm为Node.js发布对象数据库
查看>>
农民别再愁!人工智能帮你诊断作物疾病
查看>>
物联网行业将掀起新一轮并购潮 步入整合期
查看>>
夏日炎炎 构筑安防线 这些知识你Get到了吗?
查看>>
《C语言程序设计:问题与求解方法》——2.4节C语言源程序的次要组成成分:编译预处理命令、注释和声明...
查看>>
业绩不佳引裁员 雅虎2016有点烦
查看>>
《Hadoop实战第2版》——导读
查看>>
德国为新能源付出了哪些巨大的代价?
查看>>