<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)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 成人av片无码免费天天看| 亚洲国产模特在线播放| 污污视频网站免费观看| 免费高清在线爱做视频| 亚洲中文字幕久久久一区| 人妻视频一区二区三区免费| 精品丝袜国产自在线拍亚洲| 最近中文字幕免费mv视频7| 亚洲综合激情五月丁香六月| 免费无遮挡无码视频网站| 国产精品亚洲lv粉色| 亚洲国产精品成人久久蜜臀| 一级毛片在线免费播放| 亚洲精品无码专区久久久| 亚洲a一级免费视频| 亚洲精品**中文毛片| 女人张腿给男人桶视频免费版 | 国产精品亚洲综合一区在线观看| 国产国产成年年人免费看片| 鲁啊鲁在线视频免费播放| 色噜噜亚洲精品中文字幕| 免费国产99久久久香蕉| 亚洲同性男gay网站在线观看| 青青久在线视频免费观看| 性色av极品无码专区亚洲| 中文字幕亚洲一区| 无码AV片在线观看免费| 亚洲综合国产成人丁香五月激情| 四虎影视永久免费视频观看| 99久久免费国产精品热| 亚洲白色白色在线播放| 日韩免费高清一级毛片在线| 久久久久免费视频| 亚洲av无码一区二区三区观看| 国产成人综合久久精品免费| 国产成人无码区免费网站| 国产.亚洲.欧洲在线| 亚洲无线码在线一区观看 | 国产人成免费视频| 日韩免费电影网址| 国产精品亚洲小说专区|