JSP页面使用Servlet制作图形验证码

本文给大家分享的是JSP验证码的制作,通过Servlet生成图形验证码,调用非常方便,希望对大家有所帮助。

在《JS+CSS3制作图形验证码》一文中给大家分享过一个前端验证的验证码,利用JS和CSS3的transform属性里的rotate设置旋转进行制作。

今天给大家分享的是JSP验证码的制作,通过Servlet生成图形验证码。

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
/**
* 产生验证码图片的servlet
*/
public class ValidateCodeServlet extends HttpServlet {
private static final long serialVersionUID = -5051097528828603895L;
/**
* 验证码session的名称。
*/
private static final String SESSION_ATT_NAME = "validateCode";
/**
* 验证码图片的宽度。
*/
private int width = 100;
/**
* 验证码图片的高度。
*/
private int height = 30;
/**
* 验证码字符个数
*/
private int codeCount = 4;
/**
* 字体高度
*/
private int fontHeight;
/**
* 第一个字符的x轴值,因为后面的字符坐标依次递增,所以它们的x轴值是codeX的倍数
*/ 
private int codeX; 
/**
* codeY ,验证字符的y轴值,因为并行所以值一样
*/
private int codeY;
/**
* codeSequence 表示字符允许出现的序列值
*/
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
/** 
* 初始化验证图片属性 
*/
public void init() throws ServletException {
// 从web.xml中获取初始信息 
// 宽度
String strWidth = this.getInitParameter("width");
// 高度
String strHeight = this.getInitParameter("height");
// 字符个数
String strCodeCount = this.getInitParameter("codeCount");
// 将配置的信息转换成数值
try {
if (strWidth != null && strWidth.length() != 0) {
width = Integer.parseInt(strWidth);
}
if (strHeight != null && strHeight.length() != 0) {
height = Integer.parseInt(strHeight);
}
if (strCodeCount != null && strCodeCount.length() != 0) { 
codeCount = Integer.parseInt(strCodeCount);
}
} catch (NumberFormatException e) {
e.printStackTrace(); 
} 
//width-4 除去左右多余的位置,使验证码更加集中显示,减得越多越集中。
//codeCount+1 //等比分配显示的宽度,包括左右两边的空格 
codeX = (width-4) / (codeCount+1);
//height - 10 集中显示验证码 
fontHeight = height - 10; 
codeY = height - 7; 
} 

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
// 定义图像buffer 
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
Graphics2D gd = buffImg.createGraphics(); 
// 创建一个随机数生成器类
Random random = new Random(); 
// 将图像填充为白色 
gd.setColor(Color.LIGHT_GRAY); 
gd.fillRect(0, 0, width, height); 
// 创建字体,字体的大小应该根据图片的高度来定。 
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
// 设置字体。 
gd.setFont(font); 
// 画边框。 
gd.setColor(Color.BLACK); 
gd.drawRect(0, 0, width - 1, height - 1);
// 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。 
gd.setColor(Color.gray); 
for (int i = 0; i < 16; i++) { 
int x = random.nextInt(width); 
int y = random.nextInt(height); 
int xl = random.nextInt(12); 
int yl = random.nextInt(12); 
gd.drawLine(x, y, x + xl, y + yl); 
} 
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。 
StringBuffer randomCode = new StringBuffer(); 
int red = 0, green = 0, blue = 0; 
// 随机产生codeCount数字的验证码。 
for (int i = 0; i < codeCount; i++) { 
// 得到随机产生的验证码数字。 
String strRand = String.valueOf(codeSequence[random.nextInt(36)]); 
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。 
red = random.nextInt(255); 
green = random.nextInt(255); 
blue = random.nextInt(255); 
// 用随机产生的颜色将验证码绘制到图像中。 
gd.setColor(new Color(red,green,blue)); 
gd.drawString(strRand, (i + 1) * codeX, codeY); 
// 将产生的四个随机数组合在一起。 
randomCode.append(strRand); 
} 
// 将四位数字的验证码保存到Session中。 
HttpSession session = request.getSession(); 
session.setAttribute(SESSION_ATT_NAME, randomCode.toString()); 
// 禁止图像缓存。 
response.setHeader("Pragma", "no-cache"); 
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0); 
response.setContentType("image/jpeg"); 
// 将图像输出到Servlet输出流中。 
ServletOutputStream sos = response.getOutputStream(); 
ImageIO.write(buffImg, "jpeg", sos); 
sos.close();
} 

public static String getValidateCode(HttpServletRequest request){ 
HttpSession session = request.getSession(); 
Object obj = session.getAttribute(SESSION_ATT_NAME); 
if(obj!=null){ 
return obj.toString(); 
}
return "";
} 

public static void removeValidateCode(HttpServletRequest request){
HttpSession session = request.getSession(); 
session.removeAttribute(SESSION_ATT_NAME); 
    }
}

图形验证码servlet方法功能描述:

JSP页面使用Servlet制作图形验证码

web.xml配置:

JSP页面使用Servlet制作图形验证码

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/11180.html

(0)
江山如画的头像江山如画管理团队
上一篇 2022年12月1日 下午1:53
下一篇 2022年12月1日 下午2:18

99%的人还看了以下文章

  • MyEclipse中JSP的页面编码-中文乱码快速解决

    MyEclipse中页面默认编码为”ISO8859-1″,如果输入了中文会显示乱码: 保存时会弹出如下图的提示对话框。 在myeclipse中如何更改jsp的默认编码 或 jsp页面代码: <%@ page language=”java” import=”java.util.*” contentType=”text/html; …

    2018年12月24日 编程开发
    6.9K0
  • 表单method方式为post或get中文乱码的解决方法

    表单method方式为post中文乱码解决方法 ■设置请求和响应的编码方式 request.setCharacterEncoding(“utf-8”); response.setCharacterEncoding(“utf-8”); 表单method方式为get中文乱码解决方法 ■治标的方法: new String(s.getBytes(”iso-8859-…

    2019年12月17日
    6.5K0
  • aspx文件编码不统一导致乱码

    今天发现了一个很奇怪的现象,页面在VS2008下面,有些页面乱码,而有些則不会。 上网查找了很多方法均不能解决,根据以前开发asp,php的经验,怀疑这些页面的编码可能不一样。 在VS2008下面还真的不知道在哪里看文件的编码,用Dreamweaver打开一个乱码跟一个不乱码的页面对比了一下,发现一个是gb2312(不乱码),而别外一个则是utf-8(乱码)…

    2019年2月14日
    3.0K0
  • 动态网站开发技术asp、asp.net、php、jsp比较

    asp、asp.net、php、jsp技术简介 ASP 全称为Active Server Pages(中文译名为活动服务器页面),是微软公司推出的用于Web应用服务的一种编程技术.采用的脚本语言: VBScript 和JavaScript。 ASP.NET 微软公司很快公布了其宏伟的“Windows.NET”计划,发布了成为下一代网络服务框架的NGWS,同时…

    2018年3月15日
    3.2K0
  • 上机实战七:EL和JSTL的使用

    建议学时:2 一、使用EL表达式简化javaBean的开发 编写一个用户登录的JavaBean,用户信息包括用户名和密码。 编写user.jsp,使用setProperty设置用户名为125jz,密码为123。 使用EL获取用户名和密码并显示。 二、使用EL实现问卷调查 用户输入昵称、所在城市,并且以多选的方式让用户选择所使用的开发语言,然后使用EL表达式显…

    2018年12月4日
    7.5K0
  • input file获取文件路径时无法获取正确的路径

    页面有一个input file服务器控件,一个div,div是image标签的容器,当点击input file的值改变,我们往div里追加image标签;但当通过js的onchange事件动态获取input file 的路径的时候,发现console.log(path)打印出的路径是被浏览器屏蔽的, 例如:C:\fakepath\file.jpg 这是由于浏…

    2019年11月13日
    7.9K0

发表回复

登录后才能评论