001 | import java.io.IOException; |
002 | import javax.imageio.ImageIO; |
003 | import javax.servlet.ServletException; |
004 | import javax.servlet.ServletOutputStream; |
005 | import javax.servlet.http.HttpServlet; |
006 | import javax.servlet.http.HttpServletRequest; |
007 | import javax.servlet.http.HttpServletResponse; |
008 | import javax.servlet.http.HttpSession; |
009 | import java.awt.*; |
010 | import java.awt.image.BufferedImage; |
011 | import java.util.*; |
012 |
013 | /** |
014 | * 登录验证码servlet实例 |
015 | */ |
016 | public class LoginCaptcha extends HttpServlet { |
017 | private static final long serialVersionUID = 1L; |
018 | |
019 | protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
020 | response.setContentType( "image/png" ); |
021 | VerCodeCreate(response.getOutputStream(),request,response); |
022 | } |
023 | public void VerCodeCreate(ServletOutputStream out,HttpServletRequest request, |
024 | HttpServletResponse response) |
025 | { |
026 | out.clear(); //设置页面不缓存 |
027 | response.setHeader( "Pragma" , "No-cache" ); |
028 | response.setHeader( "Cache-Control" , "no-cache" ); |
029 | response.setDateHeader( "Expires" , 0 ); |
030 | |
031 | // 在内存中创建图象 |
032 | int width = 60 , height = 20 ; |
033 | BufferedImage image = new BufferedImage(width, height, |
034 | BufferedImage.TYPE_INT_RGB); |
035 | // 获取图形上下文 |
036 | Graphics g = image.getGraphics(); |
037 | // 生成随机类 |
038 | Random random = new Random(); |
039 | // 设定背景色 |
040 | // g.setColor(getRandColor(200,250)); |
041 | // g.fillRect(0, 0, width, height); |
042 | g.setColor( new Color( 255 , 255 , 255 )); |
043 | g.fillRect( 0 , 0 , width, height); |
044 | // 设定字体 |
045 | g.setFont( new Font( "Arial" , Font.PLAIN, 18 )); |
046 | // 画边框 |
047 | // g.setColor(getRandColor(160,200)); |
048 | // g.drawRect(0,0,width-1,height-1); |
049 | // 随机产生64条干扰线,使图象中的认证码不易被其它程序探测到 |
050 | g.setColor(getRandColor( 160 , 200 )); |
051 | for ( int i = 0 ; i < 64 ; i++) |
052 | { |
053 | int x = random.nextInt(width); |
054 | int y = random.nextInt(height); |
055 | int xl = random.nextInt( 12 ); |
056 | int yl = random.nextInt( 12 ); |
057 | g.drawLine(x, y, x + xl, y + yl); |
058 | } |
059 |
060 | // 取随机产生的认证码(4位数字) |
061 | String sRand = "" ; |
062 | for ( int i = 0 ; i < 4 ; i++) |
063 | { |
064 | String rand = String.valueOf(random.nextInt( 10 )); |
065 | sRand += rand; |
066 | // 将认证码显示到图象中 |
067 | g.setColor( new Color( 20 + random.nextInt( 110 ), 20 + random |
068 | .nextInt( 110 ), 20 + random.nextInt( 110 ))); |
069 | |
070 | g.drawString(rand, 13 * i + 6 , 16 ); |
071 | } |
072 |
073 | HttpSession session=request.getSession( true ); //如果没有该session,则自动创建一个新的 |
074 | // 将认证码存入SESSION |
075 | session.setAttribute( "yanzhengma" , sRand); |
076 | // 图象生效 |
077 | g.dispose(); |
078 | // 输出图象到页面 |
079 | try |
080 | { |
081 | ImageIO.write(image, "PNG" , response.getOutputStream()); |
082 | } catch (IOException e) |
083 | { |
084 | // TODO Auto-generated catch block |
085 | e.printStackTrace(); |
086 | } |
087 | } |
088 |
089 | Color getRandColor( int fc, int bc) |
090 | { |
091 | // 给定范围获得随机颜色 |
092 | Random random = new Random(); |
093 | if (fc > 255 ) |
094 | fc = 255 ; |
095 | if (bc > 255 ) |
096 | bc = 255 ; |
097 | int r = fc + random.nextInt(bc - fc); |
098 | int g = fc + random.nextInt(bc - fc); |
099 | int b = fc + random.nextInt(bc - fc); |
100 | return new Color(r, g, b); |
101 | } |
102 | } |