JSP页面验证码源码,直接复制到页面使用
2013-09-09 17:48
489 查看
<%@ page contentType="image/jpeg" import="java.awt.*, java.awt.image.*,java.util.*,javax.imageio.*" %> <%@ page import="java.io.OutputStream" %> <%! 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); } try{ session.setAttribute("rand",sRand); g.dispose(); ImageIO.write(image, "JPEG", response.getOutputStream()); OutputStream os=response.getOutputStream(); os.flush(); os.close(); os=null; response.flushBuffer(); out.clear(); out = pageContext.pushBody(); } catch(IllegalStateException e) { System.out.println(e.getMessage()); e.printStackTrace(); }%>
相关文章推荐
- 百度地图jsp源码(直接复制使用)
- 关于jsp页面中的验证码以及使用
- 使用虚拟登录页面jsp,跳过登录页,直接访问主页面
- Java 使用JSP页面生成随机验证码
- struts2中Action的各项默认值及使用Action直接转发jsp页面
- struts2中Action的各项默认值及使用Action直接转发jsp页面
- JSP页面使用js直接打印页面信息加载水印
- 在jsp页面嵌入java代码和js并且在js中直接使用java代码中的变量
- 使用servlet,jdbc将mysql中数据显示在jsp页面中,且实现直接删除数据库数据
- vbs-计算机硬件信息查看器-源码-.exe程序-可直接使用复制使用
- jsp向页面显示图片,找不到文件时直接使用java画图
- 在jsp页面使用session来验证未经登录的访问 并完成直接跳转
- 使用jsp生成验证码使用简单在java或jsp页面调用简单
- JSP的九大内置对象在jsp页面中可不加定义直接使用
- 直接在jsp页面上使用flex标签
- 页面调用方法弹出企业QQ联系客服的方法,可以直接复制过去使用
- 使用iframe实现,点击JSP页面中的验证码刷新
- js调用打印接口打印web页面之——使用lodop组件打印jsp web页面
- 使用jsp生成验证码
- 使用struts2 由pojo类跳转到jsp页面再跳转到jsp页面使用后退时,报空指针异常