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

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

//validationCode类。

package com.validationCode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
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;
import javax.servlet.http.HttpSession;
public class ValidateCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static int WIDTH = 65;//设置图片的宽度
private static int HEIGHT = 22;//设置图片的高度
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
char[] rands = generateCheckCode();
drawBackground(g);
drawRands(g, rands);
g.dispose();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", bos);
byte[] buf = bos.toByteArray();
response.setContentLength(buf.length);
sos.write(buf);
bos.close();
sos.close();
session.setAttribute("randomCode", new String(rands));
}
private void drawBackground(Graphics g) {
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < 120; i++) {
int x = (int) (Math.random() * WIDTH);
int y = (int) (Math.random() * HEIGHT);
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
g.setColor(new Color(red, green, blue));
g.drawOval(x, y, 1, 0);
}
}
private void drawRands(Graphics g, char[] rands) {
// g.setColor(Color.BLUE);
Random random = new Random();
int red = random.nextInt(110);
int green = random.nextInt(50);
int blue = random.nextInt(50);
g.setColor(new Color(red, green, blue));
g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
g.drawString("" + rands[0], 1, 17);
g.drawString("" + rands[1], 16, 15);
g.drawString("" + rands[2], 31, 18);
g.drawString("" + rands[3], 46, 16);
}
private char[] generateCheckCode() {
String chars = "13456789ABCDEFGHIJKLMNPQRSTUVWXY";
char[] rands = new char[4];
for (int i = 0; i < 4; i++) {
int rand = (int) (Math.random() * 32);
rands[i] = chars.charAt(rand);
}
return rands;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}

//在servlet中判断输入验证码是否正确

 

validate=request.getParameter("random");

HttpSession session = request.getSession();
String randomCode = (String)session.getAttribute("randomCode");

if(validate.equalsIgnoreCase(randomCode)){

//如果正确就。。。。。
}
else{
request.getSession().setAttribute("errorMessage",
"验证码不匹配,请重新输入验证码!");
}

//jsp页面

<script  language="javascript">

function changeCode()

{
document.getElementById('rc').src="imgcode?t="+new Date().getTime();

}

</script>

<div>

<img src="imgcode" id="rc"/>
<input type="text" name="random" maxlength="4"/>
<a href="javascript:changeCode();">看不清?单击换一张图片</a>
</div>

//web.xml

 

<servlet>

<servlet-name>ValidateCodeServlet</servlet-name>
<servlet-class>com.validationCode.ValidateCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidateCodeServlet</servlet-name>
<url-pattern>/imgcode</url-pattern>
</servlet-mapping>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/java727/archive/2013/05/08/3066456.html

你可能感兴趣的文章
Linux下更换jdk和配置环境变量
查看>>
【shell】shell编程(二)-运算符
查看>>
DNS_PROBE_FINISHED_NXDOMAIN
查看>>
BZOJ5104 : Fib数列
查看>>
MySQL Replication 主从复制全方位解决方案
查看>>
Nginx+upstream针对后端服务器容错的运维笔记
查看>>
使用SQL_TRACE进行数据库诊断
查看>>
风控8-收码平台
查看>>
SQL Server 中心订阅模型(多发布单订阅)
查看>>
Vue父组件接收不到子组件$emit事件的原因分析
查看>>
工作总结的字体和格式要求
查看>>
CentOS 6.9永久设置静态路由表以及路由表常用设置
查看>>
解决Docker时区与主机时区不一致的问题
查看>>
思考与知识
查看>>
访问日志不记录静态文件 访问日志切割 静态元素过期时间
查看>>
idea中复制module和module中的蓝色tag出现的方法
查看>>
python中的面相对象
查看>>
Spring缓存注解@Cache使用
查看>>
基于Three.js的360度全景--photo-sphere-viewer--简介
查看>>
去除wordpress的category各方法对比
查看>>