<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    簡易代碼之家

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      157 Posts :: 2 Stories :: 57 Comments :: 0 Trackbacks
    validateCode.jsp:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      
    <head>
        
    <base href="<%=basePath%>">
        
    <title>兩組驗證碼</title>
        
    <meta http-equiv="pragma" content="no-cache">
        
    <meta http-equiv="cache-control" content="no-cache">
        
    <meta http-equiv="expires" content="0">    
        
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        
    <meta http-equiv="description" content="This is my page">
      
    </head>
      
      
    <body>
        Two Validate Code: 
    <br>
        
    <img src="vc_1.jsp" />
        
    <img src="vc_2.jsp" />
      
    </body>
    </html>

    vc_1.jsp:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import="com.ysx.system.actions.*"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        
    <head>
            
    <title>驗證碼測試</title>
        
    </head>

        
    <body>
            
    <form name="f1" method="post" action="login.jsp">
                
    <div>
                    
    <%
                        Yzm a 
    = new Yzm();
                        a.service(request, response);
                        out.clear();
                        out 
    = pageContext.pushBody();
                    
    %>
                
    </div>
            
    </form>
        
    </body>
    </html>
    vc_2.jsp:
    <%@ page
        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);
        }
    %>
    <%
        try {
            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);
            OutputStream os 
    = response.getOutputStream();
            Graphics g 
    = image.getGraphics();
            Random random 
    = new Random();
            g.setColor(getRandColor(
    200250));
            g.fillRect(
    00, width, height);

            g.setFont(
    new Font("Times New Roman", Font.PLAIN, 18));
            g.setColor(getRandColor(
    160200));
            
    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 + 616);
            }
            session.setAttribute(
    "rand", sRand);
            g.dispose();

            ImageIO.write(image, 
    "JPEG", os);
            os.flush();
            os.close();
            os 
    = null;
            response.flushBuffer();
            out.clear();
            out 
    = pageContext.pushBody();
        } catch (IllegalStateException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    %>

    Yzm.java:

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import javax.imageio.ImageIO;

    import javax.servlet.*;
    import javax.servlet.http.*;

    public class Yzm extends HttpServlet {
        
        
    public static final long serialVersionUID = 1L;
        
        
    // 驗證碼圖片的寬度。
        private int width = 60;
        
    // 驗證碼圖片的高度。
        private int height = 20;

        
    protected void service(HttpServletRequest req, HttpServletResponse resp)
                
    throws ServletException, java.io.IOException {
            BufferedImage buffImg 
    = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g 
    = buffImg.createGraphics();

            
    // 創建一個隨機數生成器類。
            Random random = new Random();
            g.setColor(Color.WHITE);
            g.fillRect(
    00, width, height);

            
    // 創建字體,字體的大小應該根據圖片的高度來定。
            Font font = new Font("Times New Roman", Font.PLAIN, 18);
            
    // 設置字體。
            g.setFont(font);

            
    // 畫邊框。
            g.setColor(Color.BLACK);
            g.drawRect(
    00, width - 1, height - 1);

            
    // 隨機產生160條干擾線,使圖象中的認證碼不易被其它程序探測到。
            g.setColor(Color.GRAY);
            
    for (int i = 0; i < 10; 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);
            }


            
    // randomCode用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證。
            StringBuffer randomCode = new StringBuffer();
            
    int red = 0, green = 0, blue = 0;

            
    // 隨機產生4位數字的驗證碼。
            for (int i = 0; i < 4; i++{
                
    // 得到隨機產生的驗證碼數字。
                String strRand = String.valueOf(random.nextInt(10));

                
    // 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
                red = random.nextInt(200);
                green 
    = random.nextInt(200);
                blue 
    = random.nextInt(200);

                
    // 產生隨機高度 13至height之間
                float imght = 0;
                
    while (imght <= 12{
                    imght 
    = Float
                            .parseFloat(String.valueOf(random.nextInt(height)));
                }

                
    // 用隨機產生的顏色將驗證碼繪制到圖像中。
                g.setColor(new Color(red, green, blue));
                g.drawString(strRand, 
    13 * i + 6, imght);

                
    // 將產生的四個隨機數組合在一起。
                randomCode.append(strRand);
            }

            
    // 將四位數字的驗證碼保存到Session中。
            HttpSession session = req.getSession();
            session.setAttribute(
    "randomCode", randomCode.toString());

            
    // 禁止圖像緩存。
            resp.setHeader("Pragma""no-cache");
            resp.setHeader(
    "Cache-Control""no-cache");
            resp.setDateHeader(
    "Expires"0);

            resp.setContentType(
    "image/jpeg");

            
    // 將圖像輸出到Servlet輸出流中。
            ServletOutputStream sos = resp.getOutputStream();
            ImageIO.write(buffImg, 
    "jpeg", sos);
            sos.close();
        }

    }
    posted on 2009-09-22 19:45 Jakin.zhou 閱讀(197) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 爱情岛论坛亚洲品质自拍视频网站 | 国产无遮挡吃胸膜奶免费看视频| 国产免费爽爽视频免费可以看| 亚洲人成影院在线观看| 亚洲小视频在线观看| 亚洲a无码综合a国产av中文| 女人18一级毛片免费观看| 久久久久久亚洲av成人无码国产| 亚洲国产高清国产拍精品| 99精品免费观看| 久久久久亚洲精品天堂久久久久久 | 久九九精品免费视频| 久久久久亚洲AV无码专区网站| 一级毛片a免费播放王色电影| 免免费国产AAAAA片| 亚洲中文字幕AV每天更新| 免费视频一区二区| 亚洲男人av香蕉爽爽爽爽| 亚洲色大成网站www| 在线观看成人免费| 国产成人综合久久精品亚洲| 成人免费午夜无码视频| 亚洲国产第一页www| 2021在线永久免费视频| 亚洲av激情无码专区在线播放| 999zyz**站免费毛片| 亚洲国产人成精品| 亚洲乱亚洲乱妇24p| 国产香蕉九九久久精品免费 | 我的小后妈韩剧在线看免费高清版 | 免费中文字幕一级毛片| 日韩亚洲国产综合高清| 2021在线永久免费视频| 亚洲JLZZJLZZ少妇| 国产亚洲婷婷香蕉久久精品| 精选影视免费在线 | 亚洲永久无码3D动漫一区| baoyu116.永久免费视频| 亚洲欧洲国产精品久久| 免费国产成人高清在线观看网站| 美景之屋4在线未删减免费|