session 注册简单验证码的实现
来源:互联网 发布:js windows调用firefox 编辑:程序博客网 时间:2024/06/05 09:33
一:简单实现界面:
二:代码实现
index.jsp原码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> <font color="red"> <%=session.getAttribute("message")==null ? "":session.getAttribute("message") %> </font> <form action="<%=request.getContextPath() %>/checkCodeservlet" method="post"> name:<input type="text" name="name"/> checkcode:<input type="text" name="CHECK_CODE_PARAM_NAME" /> <img alt="" src="<%=request.getContextPath() %>/ValidateColorServlet"> <input type="submit" value="submit"/> </form> </body></html>CheckCodeservlet原码:
package com.hp.yanzhengma;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class CheckCodeservlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//1、获取请求参数CHECK_CODE_PARAM_NAMEString parameCode=request.getParameter("CHECK_CODE_PARAM_NAME");//2、获取session属性值CHECK_CODE_KEYString sessionCode=(String)request.getSession().getAttribute("CHECK_CODE_KEY");//3、比对,看是否一致,若一致,验证码正确,否则错误System.out.println(parameCode+"\n"+sessionCode);if( !(parameCode != null && parameCode.equals(sessionCode))){request.getSession().setAttribute("message", "验证码不一致!");response.sendRedirect(request.getContextPath()+"/check/index.jsp");return;}System.out.println("受理请求!");}}ValidateColorServlet原码:
package com.hp.yanzhengma; 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.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;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 = 6;//验证码字体的高度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();}}web.xml配置原码:
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>1011sessionyanzhengma</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ValidateColorServlet</servlet-name> <servlet-class>com.hp.yanzhengma.ValidateColorServlet</servlet-class> </servlet> <servlet> <servlet-name>CheckCodeservlet</servlet-name> <servlet-class>com.hp.yanzhengma.CheckCodeservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ValidateColorServlet</servlet-name> <url-pattern>/ValidateColorServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CheckCodeservlet</servlet-name> <url-pattern>/checkCodeservlet</url-pattern> </servlet-mapping></web-app></span>
三:体会
提示信息重定向于另一个jsp并打印出:
表单中链接servlet:
0 0
- session 注册简单验证码的实现
- web实现验证码和简单的登录注册
- 使用js实现简单的注册验证
- 简单实现手机号验证码注册功能
- 简单的登陆注册的实现+验证码和表单验证
- 实现session验证的lservlet验证码
- ajax实现简单注册验证
- PHP 用session与gd库实现简单验证码生成与验证的类
- Ajax实现简单的登录验证与帐号注册
- 简单好玩的struts2+hibernate实现用户注册邮箱验证
- webpy实现简单的登录注册和对应验证
- jsp注册验证码的实现
- jsp注册验证码的实现
- 登陆注册时的验证码实现
- java实现注册的短信验证码
- boodstrap 简单的 注册验证
- Session技术案例-用户注册验证码
- cookie+session+登录+注册+验证码
- Ldom can't boot from vdisk ( Can't open disk label package )
- int 类型的自增长ID生成类,支持多线程。
- MySQL- SHOW TABLE STATUS命令
- Eclipse上配置MonkeyRunner(Windows)
- 解读ASP.NET 5 & MVC6系列(2):初识项目
- session 注册简单验证码的实现
- ibatis resultMap使用的问题
- iOS 获取Wi-Fi的SSID
- 算法:两种对拼音进行智能切分的方法
- cxf发布webservice
- Linux基础(七)内核线程和用户线程
- iOS开发——TTS文本发音——(附)单词、中文发音示例和工具类
- Web Api 自动生成帮助文档
- long 类型的自增长ID生成类,支持多线程。