Servlet生成验证码
public class CaptchaServlet extends HttpServlet {
private static final long serialVersionUID = -5051097528828603895L;
private int width = 100;
private int height = 30;
private int codeCount = 4;
private int fontHeight;
private int codeX;
private int codeY;
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 {
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();
}
codeX = (width - 4) / (codeCount + 1);
fontHeight = height - 10;
codeY = height - 7;
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
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);
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);
}
StringBuffer randomCode = new StringBuffer();
int red = 0;
int green = 0;
int blue = 0;
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);
}
HttpSession session = request.getSession();
session.setAttribute("captcha", randomCode.toString());
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
}
Web.xml中的配置
<servlet>
<servlet-name>CaptchaServlet</servlet-name>
<servlet-class>com.quidsi.emm.track.util.CaptchaServlet</servlet-class>
<init-param>
<param-name>width</param-name>
<param-value>120</param-value>
</init-param>
<init-param>
<param-name>height</param-name>
<param-value>32</param-value>
</init-param>
<init-param>
<param-name>codeCount</param-name>
<param-value>4</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CaptchaServlet</servlet-name>
<url-pattern>/CaptchaServlet</url-pattern>
</servlet-mapping>
页面中的使用:
<div>
Captcha:<input id="inputCode" name="inputCode" />
<img src="CaptchaServlet" align="middle" title="Look not clear, please click me" onclick="javascript:refresh(this);" onmouseover="mouseover(this)"/>
</div>
相关问题
1、浏览器后退,可以不断重复提交。js禁用后退:<script language="JavaScript">javascript:window.history.forward(1);</script>
2、前端页面验证码输入验证时,从后台取到的验证码与页面显示的验证码慢一拍。
解决方案:(1)、在提交的时候通过ajax请求取得最新的验证码。
(2)、直接后台验证一次,验证失败回写前台
3、跨域请求,改用相对路径
4、javax.imageio.IIOException: Can't create output stream!
原因:ImageIO依赖于Tomcat的temp目录,如temp目录遗失就会报上述错误
解决方案:(1)、(不推荐)在tomcat目录下加上temp目录
(2)、(不推荐)把ImageIO.write(image, "jpeg", response.getOutputStream());修改为
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(image);
(3)、禁用缓存ImageIO.setUseCache(false);
分享到:
相关推荐
验证码安全总结
多种编写验证码的方法源码,多重比较,下载即可使用!
验证码图像分割 图像去噪 图像识别 个人总结的
目前,不少网站在用户登录、用户提交信息等登录和输入的页面上使用了验证码技术。验证码技术可以有效防止恶意用户对网站的滥用,使得网站可以有效避免用户信息失窃、广告SPAM等问题。但与此同时,验证码技术的使用却...
1.使用自己手机号收取验证码 2.自己的验证码和对方的手机号填上,下一步城管设置新密码 1.在服务器进行有效验证,手机号和验证码在服务器进行唯一性绑定验证 2.
1.在实验四基础上使用AJAX技术实现异步登录功能。(40分) Ajax异步的特点为:多个事件并行发生,事件互不影响,请求之后,不刷新整张页面,页面不动,只是刷新页面的局部。异步数据获取技术 XMLHttpRequest对象方法...
html5点击刷新验证码特效
关于Java验证码的总结,在网上找到的,然后给整理了的,希望对你们有用
主要为大家详细介绍了java生成验证码的步骤总结,需要的朋友可以参考下
验证码设计 以及源代码,帮您设计验证码是您的设计跟容易
处理验证码时遇到了个问题就是验证码页面产生的图片与从session里面去的不一样,找了好久,现在总结了处理验证码的一些方法,同大家交流一下。
验证码及它的作用 验证码为全自动区分计算机和人类的图灵测试的缩写,是一种区分用户是计算机的公共全自动程序,这个问题可以由计算机生成并评判,但是必须只有人类才能解答.可以防止恶意破解密码、刷票、论坛灌水、...
对于WEB项目的Struts框架开发如何实现前端注册验证码的编写
个人总结验证码破解方法,用tensorflow在gpu上实现验证码破解,破解了一家网站的具有像素化数字大小写字母字符、斑点背景噪声验证码,初次准确率达50%。
本文实例总结了PHP生成各种随机验证码的方法。分享给大家供大家参考,具体如下: 验证码在WEB应用中非常重要,通常用来防止用户恶意提交表单,如恶意注册和登录、论坛恶意灌水等。本文将通过实例讲解使用PHP生成各种...
在Maven中实现验证码的功能比较困难。我在这总结了这复杂的操作步骤,写的很详细,并且有代码,希望能帮的到你。
kaptcha验证码配置的3种方式 参照本演示项目
《A Survey on Breaking Technique of Text-Based CAPTCHA》 《Selective Learning Confusion Class for Text-Based CAPTCHA Recognition》 。。。...论文总结请查看作者博客:验证码识别---外文篇
验证码在很多地方都会使用到,本分享是自己在做验证码功能的时候总结的一些经验,是基于谷歌kaptcha来做的,验证码的效果是,可以自定义字符范围,可以自定义背景效果,自定义字体等。总之,根据参数,都可以实现...
自己正在学习ajax,一手总结出来的,都是自己一个一个打出来的有很多我之前从未学到过的知识点,希望可以给你们带来帮助