码迷,mamicode.com
首页 > Web开发 > 详细

利用jsp生成验证码

时间:2015-08-06 10:56:45      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
<%
            response.addHeader("P3P","CP=CAO PSA OUR");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            
            //set backgroud color
            Color backColor = new Color(90,90,90);
            
            //set font color
            Color fontColor = new Color(200,255,200);
            
            int maxlength = 0;
            int width = 0;
            int height = 20;
            if (request.getParameter("width") != null) {
                maxlength = Integer.parseInt(request.getParameter("width"));
            } else {
                maxlength = 4;
            }
            width = maxlength * 15;
            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = image.getGraphics();
            Random random = new Random();
            g.setColor(backColor);
            g.fillRect(0, 0, width, height);
            g.setFont(new Font("å®?ä½?", Font.PLAIN, 18));
            g.setColor(backColor);
            for (int i = 0; i < 255; 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 < maxlength; i++) {
                String rand = String.valueOf(random.nextInt(10));
                sRand += rand;
                g.setColor(fontColor);   
                g.drawString(rand, 13 * i + 6, 16);
            }
            //session.setAttribute("RAND_IMAGE_VALIDATE_", sRand);         
            session.setAttribute("RAND_IMAGE_VALIDATE_", sRand);
            g.dispose();
            out.clear();
            out = pageContext.pushBody();        
            ImageIO.write(image, "JPEG", response.getOutputStream());
%>

 

利用jsp生成验证码

标签:

原文地址:http://www.cnblogs.com/bobodeboke/p/4707133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!