丄諦啲仇魜ヤ
          如 果 敵 人 讓 你 生 氣 , 那 說 明 你 沒 有 勝 他 的 把 握!
          posts - 6,comments - 56,trackbacks - 1

          <%@ page language="java" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
               contentType="image/jpeg" pageEncoding="UTF-8"%>


          <%  //設(shè)置頁面不緩存
             response.setHeader("Pragma","No-cache");
             response.setHeader("Cahce-Control","no-cache");
             response.setDateHeader("Expires",0);
             //在內(nèi)存中創(chuàng)建圖片
             int width=60,height=20;
             BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
             //獲取圖形上下文
             Graphics g= image.getGraphics();
             //生成隨機(jī)類
             Random random= new Random();
             //設(shè)置背景顏色
             g.setColor(new Color(160,200,100));
             g.fillRect(0,0,width,height);
             //設(shè)置字體
             g.setFont(new Font("Times New Roman",Font.PLAIN,18));
             //隨機(jī)產(chǎn)生50條干擾線,使圖形中的驗(yàn)證碼不易被其他的程序探測到
              g.setColor(new Color(160,200,200));
             for(int i=0;i<50;i++)
             {
               int x=random.nextInt(width);
               int y=random.nextInt(height);
               int x1=random.nextInt(width);
               int y1=random.nextInt(height);
               g.drawLine(x,y,x+x1,y+y1);
             }
             //隨機(jī)產(chǎn)生驗(yàn)證碼(4為數(shù)字)
             String sRand="";
             for(int i=0;i<4;i++)
             {
               String rand=String.valueOf(random.nextInt(10));
               sRand+=rand;
               //將驗(yàn)證碼顯示到圖象
               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);  //////將產(chǎn)生的驗(yàn)證碼存儲到sesson中
             g.dispose();
             ImageIO.write(image,"JPEG",response.getOutputStream());
             out.clear(); //***********
             out=pageContext.pushBody();//**********
           %>
          *********************************************************************
          <img src="yanzhengma.jsp" id="CheckCodeimg" title="看不清請點(diǎn)擊圖片!"/><html:errors property="yanzhengma"/>&nbsp;
          <a href="#" onclick="document.getElementById('CheckCodeimg').src='yanzhengma.jsp';">看不清?</a><br/>

          posted on 2007-11-29 16:31 Crying 閱讀(255) 評論(0)  編輯  收藏 所屬分類: Jsp+Struts
          主站蜘蛛池模板: 丁青县| 永德县| 奇台县| 岱山县| 汤阴县| 汉沽区| 汝城县| 南皮县| 阿克陶县| 晋宁县| 昔阳县| 福州市| 金川县| 兴安县| 宁乡县| 齐河县| 枣庄市| 保康县| 长垣县| 上杭县| 方山县| 微博| 富宁县| 渑池县| 太保市| 巴南区| 舞钢市| 通州区| 临桂县| 金华市| 清水县| 会泽县| 赤城县| 来宾市| 富川| 商城县| 汽车| 广灵县| 金乡县| 武鸣县| 遵义市|