session的典型案例三:实现一次性验证码
来源:互联网 发布:淘宝联通华盛通 编辑:程序博客网 时间:2024/05/16 17:03
生成验证码的工具类:
package com.sa.util;import 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.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;import javax.servlet.http.HttpSession;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;/** * 验证码图片生成Servlet类,直接调用该Servlet即可使用 * 取值的时候调用session.getAttribute("code")得到生成的值 * @author <a href="mailto:weijunqiang2010@gmail.com">Ajunboys</a> * */@WebServlet("/verifycode")public class VerifyCodeUtil extends HttpServlet { private static final long serialVersionUID = 1L; static final char[] dictionary = { '2','3','4','5','6','7','8','9' ,'a','b','c','d','e','f','g','h','i','j' ,'k','m','n','p','q','r','s','t' ,'u','v','w','x','y','z' ,'A','B','C','D','E','F','G','H','J' ,'K','L','M','N','P','Q','R','S','T' ,'U','V','W','X','Y','Z' /*'1','l','0','O','o','#','@','$','%','&','(',')','|','/','*'//暂时不用特殊字符(包括:数字1,0;字母:l,o,O) ,'^','!','~','\\'*/ }; static Random random = new Random(); /** * 产生n[4,4+]个随机数 * @param n * @return */ static String getRandomString(int n){ StringBuffer buffer = new StringBuffer(); if (n < 4) { n = 4; } for (int i = 0; i < n; i++) { buffer.append(dictionary[random.nextInt(dictionary.length)]); } return buffer.toString(); } /** * 随机颜色 * @return */ static Color getRandomColor(){ return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)); } /** * 颜色反色 * @param c * @return */ static Color getReverseColor(Color c){ return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue()); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); String randomString = getRandomString(6); request.getSession(true).setAttribute("code", randomString); int width = 100; //验证码图片宽度 int height = 30; //验证码图片高度 Color color = getRandomColor(); Color reverse = getReverseColor(color); //创建一个彩图 BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //绘图对象 Graphics2D g = bi.createGraphics(); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16)); g.setColor(color); g.fillRect(0, 0, width, height); g.setColor(reverse); g.drawString(randomString, 18, 20); //绘制最多100个噪音点 for (int i = 0, n = random.nextInt(100); i < n; i++) { g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1); } ServletOutputStream out = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(bi); out.flush(); out.close(); }}登录页面:
<%-- Created by IntelliJ IDEA. User: 123 Date: 2017/10/9 Time: 8:57 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>验证码</title> <script> function reload() { console.log(1); var img = document.getElementById("img"); img.src = "<%=request.getContextPath()%>/verifycode?num="+Math.random(); //img.src=img.src } </script></head><body> <form action="<%=request.getContextPath()%>/loginServlet" method="post"> name:<input type="text" name="name"/> password:<input type="password" name="password"/> verify:<input type="text" name="verify"/> <img src="<%=request.getContextPath()%>/verifycode" id="img"/> <a href="javascript:reload();">看不清,换一张?</a> <input type="submit" value="提交"> </form></body></html>逻辑判断:
package com.sa.servlet;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 java.io.IOException;//@WebServlet(name = "loginServlet")public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(1); String verifyCode=(String) request.getParameter("verify"); String code=(String) request.getSession().getAttribute("code"); if(code.equalsIgnoreCase(verifyCode)){ response.sendRedirect(request.getContextPath()+"/pages/success.jsp"); }else{ response.sendRedirect(request.getContextPath()+"/pages/failed.jsp"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}测试:
阅读全文
0 0
- session的典型案例三:实现一次性验证码
- Session实现一次性验证码
- Session案例一次性验证码登录
- 一个利用Session实现一次性验证码
- 利用session实现一次性验证码
- 使用session实现一次性验证码
- 使用session实现一次性验证码
- 使用session实现一次性验证码
- 使用session实现一次性验证码
- 利用Session实现一次性验证码
- Session实现一次性验证码(十)
- session的典型案例一:使用session实现购物车
- Session实例:一次性验证码
- url重写实现session跟踪和一次性验证码的实现
- url重写实现session跟踪和一次性验证码的实现
- 会话中session实现一次性验证码(源码)
- 2011-10-26 利用Session实现一次性验证码
- javaWEB总结(26):利用session实现一次性验证码
- spring boot maven 报错 sconnected from the target VM, address: '127.0.0.1:57472', transport: 'socket'
- 旅行必备的地图,三分钟看懂方位图~
- 关于Java中Byte类型的取值范围的理解
- arraylist linkedlist vector的区别
- iOS 设备是否越狱的判断代码
- session的典型案例三:实现一次性验证码
- php class类的用法详细总结
- Android开发中高效的数据结构
- JAVA中的反射机制和动态代理
- 深度分析-从源代码看Hcash(超级现金)的矿业指南和矿工规划,含挖矿收益预估
- 关于跨域访问的扯淡
- 使用console调试JavaScript
- MongoDB 基础
- 【机器学习实战】第5章 Logistic回归