本文实例讲述了jsp实现简单验证码的方法。分享给大家供大家参考。具体如下:
这里只有一个文件,可以在<img />的src属性里直接调用显示,适用于各种项目的表单安全验证
jsp页面代码:
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %> <%! Color getRandColor(int fc,int bc){ Random random = new Random(); if(fc>255) fc=255; if(bc>255) bc=255; int r=fc+random.nextInt(bc-fc); int g=fc+random.nextInt(bc-fc); int b=fc+random.nextInt(bc-fc); return new Color(r,g,b); } %> <% response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); int width=60, height=20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); Random random = new Random(); g.setColor(getRandColor(200,250)); g.fillRect(0, 0, width, height); g.setFont(new Font("Times New Roman",Font.PLAIN,18)); g.setColor(getRandColor(160,200)); for (int i=0;i<155;i++){ int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x,y,x+xl,y+yl); } String sRand=""; for (int i=0;i<4;i++){ String rand=String.valueOf(random.nextInt(10)); sRand+=rand; g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); g.drawString(rand,13*i+6,16); } session.setAttribute("rand",sRand); g.dispose(); ImageIO.write(image, "JPEG", response.getOutputStream()); response.getOutputStream().flush(); response.getOutputStream().close(); out.clear(); out=pageContext.pushBody(); %>
希望本文所述对大家的JSP程序设计有所帮助。
Greediness(贪婪型):最大匹配 X、X*、X+、X{n,} 是最大匹配。例如你要用 “....
1 . 目标 演示下图的git reset 各选项的效果。 2. Git Reset操作说明 图中说明:...
Go原生就支持连接数据库,所以在使用 Golang 开发时,当需要数据库交互时,即可...
2月23日消息 据外媒 Windows Latest 今日报道,借助 Windows 10 Sun Valley 更新...
3月22日消息 外媒 Winfuture 报道,此前微软面向 Insider 预览用户公布了 Window...
本文实例讲述了正则表达式中的操作符及说明。分享给大家供大家参考,具体如下: ...
橡皮擦一个逗趣的互联网高级网虫。 观前提醒本篇文章涉及知识点巨大建议先收藏再...
ajax 实现三级联动,相当于写了一个小插件,用的时候直接拿过来用就可以了,这里...
从另一台机器上复制过来的项目,由于两台机器的库目录不一致,导致了stdio.h等很...
这些日子一直在简书上使用markdown写作,已经渐渐的痴迷于这种简洁纯粹的写作方...