簡易代碼之家

            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 閱讀(192) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 平陆县| 绍兴县| 裕民县| 沁水县| 长岛县| 遂宁市| 雅安市| 大冶市| 子洲县| 甘孜县| 崇义县| 漳浦县| 秦安县| 年辖:市辖区| 隆化县| 镇雄县| 霞浦县| 泰来县| 江永县| 玛纳斯县| 金乡县| 旬阳县| 鹿泉市| 通州市| 德州市| 衡山县| 玉龙| 塔河县| 射洪县| 宝鸡市| 剑河县| 玉田县| 区。| 乳源| 鄯善县| 岳阳县| 望江县| 友谊县| 安西县| 浦县| 五原县|