国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

java驗證碼

2024-09-05 00:23:38
字體:
來源:轉載
供稿:網友
<一>、Java生成驗證碼圖片
1.Servlet生成驗證碼圖片
Java代碼 復制代碼
  1. package com.logcd.servlet;   
  2.   
  3. import java.awt.Color;   
  4. import java.awt.Font;   
  5. import java.awt.Graphics2D;   
  6. import java.awt.image.BufferedImage;   
  7. import java.util.Random;   
  8. import javax.imageio.ImageIO;   
  9. import javax.servlet.*;   
  10. import java.io.*;   
  11. import javax.servlet.http.*;   
  12. import javax.servlet.ServletException;   
  13. import javax.servlet.http.HttpServlet;   
  14. import javax.servlet.http.HttpServletRequest;   
  15. import javax.servlet.http.HttpServletResponse;   
  16.   
  17. @SuppressWarnings("serial")   
  18. public class RandomCode extends HttpServlet {   
  19.   
  20.     public void doGet(HttpServletRequest request, HttpServletResponse response)   
  21.             throws ServletException, IOException {   
  22.   
  23.         this.doPost(request, response);   
  24.     }   
  25.   
  26.     public void doPost(HttpServletRequest request, HttpServletResponse response)   
  27.             throws ServletException, IOException {   
  28.   
  29.         // 驗證碼圖片的寬度。   
  30.         int width = 70;   
  31.         // 驗證碼圖片的高度。   
  32.         int height = 30;   
  33.         BufferedImage buffImg = new BufferedImage(width, height,   
  34.                 BufferedImage.TYPE_INT_RGB);   
  35.         Graphics2D g = buffImg.createGraphics();   
  36.   
  37.         // 創建一個隨機數生成器類。   
  38.         Random random = new Random();   
  39.   
  40.         // 設定圖像背景色(因為是做背景,所以偏淡)   
  41.         g.setColor(getRandColor(200250));   
  42.         g.fillRect(00, width, height);   
  43.         // 創建字體,字體的大小應該根據圖片的高度來定。   
  44.         Font font = new Font("Times New Roman", Font.HANGING_BASELINE, 28);   
  45.         // 設置字體。   
  46.         g.setFont(font);   
  47.   
  48.         // 畫邊框。   
  49.         g.setColor(Color.BLACK);   
  50.         g.drawRect(00, width - 1, height - 1);   
  51.         // 隨機產生155條干擾線,使圖象中的認證碼不易被其它程序探測到。   
  52.         //g.setColor(Color.GRAY);   
  53.         g.setColor(getRandColor(160,200));   
  54.         for (int i = 0; i < 155; i++) {   
  55.             int x = random.nextInt(width);   
  56.             int y = random.nextInt(height);   
  57.             int xl = random.nextInt(12);   
  58.             int yl = random.nextInt(12);   
  59.             g.drawLine(x, y, x + xl, y + yl);   
  60.         }   
  61.   
  62.         // randomCode用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證。   
  63.         StringBuffer randomCode = new StringBuffer();   
  64.   
  65.         // 設置默認生成4個驗證碼   
  66.         int length = 4;   
  67.         // 設置備選驗證碼:包括"a-z"和數字"0-9"   
  68.         String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";   
  69.   
  70.         int size = base.length();   
  71.   
  72.         // 隨機產生4位數字的驗證碼。   
  73.         for (int i = 0; i < length; i++) {   
  74.             // 得到隨機產生的驗證碼數字。   
  75.             int start = random.nextInt(size);   
  76.             String strRand = base.substring(start, start + 1);   
  77.   
  78.             // 用隨機產生的顏色將驗證碼繪制到圖像中。   
  79.             // 生成隨機顏色(因為是做前景,所以偏深)   
  80.             //g.setColor(getRandColor(1, 100));   
  81.                
  82.             //調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接生成   
  83.             g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));   
  84.   
  85.             g.drawString(strRand, 15 * i + 624);   
  86.   
  87.             // 將產生的四個隨機數組合在一起。   
  88.             randomCode.append(strRand);   
  89.         }   
  90.         // 將四位數字的驗證碼保存到Session中。   
  91.         HttpSession session = request.getSession();   
  92.         session.setAttribute("rand", randomCode.toString());   
  93.   
  94.         //圖象生效   
  95.         g.dispose();   
  96.   
  97.         // 禁止圖像緩存。   
  98.         response.setHeader("Pragma""no-cache");   
  99.         response.setHeader("Cache-Control""no-cache");   
  100.         response.setDateHeader("Expires"0);   
  101.   
  102.         response.setContentType("image/jpeg");   
  103.   
  104.         // 將圖像輸出到Servlet輸出流中。   
  105.         ServletOutputStream sos = response.getOutputStream();   
  106.         ImageIO.write(buffImg, "jpeg", sos);   
  107.         sos.flush();   
  108.         sos.close();   
  109.   
  110.     }   
  111.   
  112.     Color getRandColor(int fc, int bc) {// 給定范圍獲得隨機顏色   
  113.         Random random = new Random();   
  114.         if (fc > 255)   
  115.             fc = 255;   
  116.         if (bc > 255)   
  117.             bc = 255;   
  118.         int r = fc + random.nextInt(bc - fc);   
  119.         int g = fc + random.nextInt(bc - fc);   
  120.         int b = fc + random.nextInt(bc - fc);   
  121.         return new Color(r, g, b);   
  122.     }   
  123.   
  124. }  

2.配置
Xml代碼 復制代碼
  1. <servlet>  
  2.     <servlet-name>RandomCode</servlet-name>  
  3.     <servlet-class>com.logcd.servlet.RandomCode</servlet-class>  
  4. </servlet>  
  5. <servlet-mapping>  
  6.     <servlet-name>RandomCode</servlet-name>  
  7.     <url-pattern>/randomCode</url-pattern>  
  8. </servlet-mapping>  

3.調用
Html代碼 復制代碼
  1. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  2. <meta http-equiv="pragma" content="no-cache"/>  
  3. <meta http-equiv="cache-control" content="no-cache"/>  
  4. <meta http-equiv="expires" content="0"/>  
  5.   
  6. <iframe src="http://127.0.0.1/js_test/randomCode" id="codeFrame" name="codeFrame" frameborder="no" border="0" marginwidth="0"  
  7.      marginheight="0" scrolling="no" allowtransparency="yes" height="35" width="102"></iframe>  
  8.      <a href="javascript:void(0);" onclick="refreshCode();">看不清,換一張</a>  
  9.     <br>    
  10.     <span id="codeImg"><img border=0 src="randomCode"></span>  
  11.     <a href="javascript:void(0);" onclick="reloadCode()">看不清,再換一張</a>  

Js代碼 復制代碼
  1. function $(id){   
  2.     return document.getElementById(id);   
  3. }   
  4.   
  5. /**刷新iframe**/  
  6. function refreshCode(){   
  7.     window.frames["codeFrame"].location.reload();   
  8. }   
  9.   
  10. /**替換圖片**/  
  11. function reloadCode(){   
  12.     $("codeImg").innerHTML = "<img border=0 src='randomCode'>";   
  13. }  

<二>、Checked vs UnChecked Exception
       任何的異常都是Throwable類,并且在它之下包含兩個字類Error / Exception,而Error僅在當在Java虛擬機中發生動態連接失敗或其它的定位失敗的時候,Java虛擬機拋出一個Error對象。典型的簡易程序不捕捉或拋出Errors對象。
        Exception中比較重要的就是RuntimeException(運行時異常)-可能在執行方法期間拋出但未被捕捉的 RuntimeException 的任何子類都無需在 throws 子句中進行聲明。
        除了Error與RuntimeException,其他剩下的異常都是你需要關心的,而這些異常類統稱為Checked Exception,至于Error與RuntimeException則被統稱為Unchecked Exception。
  1. Checked exception: 這類異常都是Exception的子類 。異常的向上拋出機制進行處理,假如子類可能產生A異常,那么在父類中也必須throws A異常。可能導致的問題:代碼效率低,耦合度過高。C#中就沒有使用這種異常機制。
  2. Unchecked exception: 這類異常都是RuntimeException的子類,雖然RuntimeException同樣也是Exception的子類,但是它們是非凡的,它們不能通過client code來試圖解決,所以稱為Unchecked exception。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高唐县| 临桂县| 新津县| 祁门县| 淮滨县| 浠水县| 神池县| 上栗县| 翁牛特旗| 台北县| 吉木萨尔县| 台山市| 金秀| 峨眉山市| 喀喇沁旗| 哈巴河县| 什邡市| 卢湾区| 边坝县| 卢湾区| 个旧市| 淅川县| 苍南县| 神木县| 新和县| 无锡市| 沈阳市| 大安市| 张家界市| 蛟河市| 青岛市| 西乌珠穆沁旗| 香格里拉县| 永泰县| 丰台区| 焦作市| 驻马店市| 修武县| 新巴尔虎右旗| 福泉市| 尼木县|