編寫CheckImgServlet實現類
1 import java.awt.Color; 2 import java.awt.Font; 3 import java.awt.Graphics; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.BufferedReader; 7 import java.io.FileReader; 8 import java.io.IOException; 9 import java.util.ArrayList; 10 import java.util.List; 11 import java.util.Random; 12 import javax.imageio.ImageIO; 13 import javax.servlet.ServletException; 14 import javax.servlet.http.HttpServlet; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 /** 18 * 驗證碼生成程序 19 * 20 * 21 * 22 */ 23 public class CheckImgServlet extends HttpServlet { 24 // 集合中保存所有成語 25 private List<String> words = new ArrayList<String>(); 26 @Override 27 public void init() throws ServletException { 28 // 初始化階段,讀取new_words.txt 29 // web工程中讀取 文件,必須使用絕對磁盤路徑 30 String path = getServletContext().getRealPath("/WEB-INF/new_words.txt"); 31 try { 32 BufferedReader reader = new BufferedReader(new FileReader(path)); 33 String line; 34 while ((line = reader.readLine()) != null) { 35 words.add(line); 36 } 37 reader.close(); 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 } 42 public void doGet(HttpServletRequest request, HttpServletResponse response) 43 throws ServletException, IOException { 44 // 禁止緩存 45 // response.setHeader("Cache-Control", "no-cache"); 46 // response.setHeader("Pragma", "no-cache"); 47 // response.setDateHeader("Expires", -1); 48 int width = 120; 49 int height = 30; 50 // 步驟一 繪制一張內存中圖片 51 BufferedImage bufferedImage = new BufferedImage(width, height, 52 BufferedImage.TYPE_INT_RGB); 53 // 步驟二 圖片繪制背景顏色 ---通過繪圖對象 54 Graphics graphics = bufferedImage.getGraphics();// 得到畫圖對象 --- 畫筆 55 // 繪制任何圖形之前 都必須指定一個顏色 56 graphics.setColor(getRandColor(200, 250)); 57 graphics.fillRect(0, 0, width, height); 58 // 步驟三 繪制邊框 59 graphics.setColor(Color.WHITE); 60 graphics.drawRect(0, 0, width - 1, height - 1); 61 // 步驟四 四個隨機數字 62 Graphics2D graphics2d = (Graphics2D) graphics; 63 // 設置輸出字體 64 graphics2d.setFont(new Font("宋體", Font.BOLD, 18)); 65 Random random = new Random();// 生成隨機數 66 int index = random.nextInt(words.size()); 67 String word = words.get(index);// 獲得成語 68 // 定義x坐標 69 int x = 10; 70 for (int i = 0; i < word.length(); i++) { 71 // 隨機顏色 72 graphics2d.setColor(new Color(20 + random.nextInt(110), 20 + random 73 .nextInt(110), 20 + random.nextInt(110))); 74 // 旋轉 -30 --- 30度 75 int jiaodu = random.nextInt(60) - 30; 76 // 換算弧度 77 double theta = jiaodu * Math.PI / 180; 78 // 獲得字母數字 79 char c = word.charAt(i); 80 // 將c 輸出到圖片 81 graphics2d.rotate(theta, x, 20); 82 graphics2d.drawString(String.valueOf(c), x, 20); 83 graphics2d.rotate(-theta, x, 20); 84 x += 30; 85 } 86 // 將驗證碼內容保存session 87 request.getSession().setAttribute("checkcode_session", word); 88 // 步驟五 繪制干擾線 89 graphics.setColor(getRandColor(160, 200)); 90 int x1; 91 int x2; 92 int y1; 93 int y2; 94 for (int i = 0; i < 30; i++) { 95 x1 = random.nextInt(width); 96 x2 = random.nextInt(12); 97 y1 = random.nextInt(height); 98 y2 = random.nextInt(12); 99 graphics.drawLine(x1, y1, x1 + x2, x2 + y2); 100 } 101 // 將上面圖片輸出到瀏覽器 ImageIO 102 graphics.dispose();// 釋放資源 103 104 //將圖片寫到response.getOutputStream()中 105 ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); 106 } 107 public void doPost(HttpServletRequest request, HttpServletResponse response) 108 throws ServletException, IOException { 109 doGet(request, response); 110 } 111 /** 112 * 取其某一范圍的color 113 * 114 * @param fc 115 * int 范圍參數1 116 * @param bc 117 * int 范圍參數2 118 * @return Color 119 */ 120 private Color getRandColor(int fc, int bc) { 121 // 取其隨機顏色 122 Random random = new Random(); 123 if (fc > 255) { 124 fc = 255; 125 } 126 if (bc > 255) { 127 bc = 255; 128 } 129 int r = fc + random.nextInt(bc - fc); 130 int g = fc + random.nextInt(bc - fc); 131 int b = fc + random.nextInt(bc - fc); 132 return new Color(r, g, b); 133 } 134 }
用於生成驗證碼圖片上的文字如下下面四字成語,太多只打出一部分,你們可以自己添加,將其保存為txt文檔,保存web項目的WEB-INF下
一唱一和
一呼百應
一干二凈
一舉兩得
一落千丈
一模一樣
一暴十寒
一日千里
一五一十
一心一意
兩面三刀
三長兩短
三番五次
三三兩兩
三頭六臂
三心二意
三言兩語
一呼百應
一干二凈
一舉兩得
一落千丈
一模一樣
一暴十寒
一日千里
一五一十
一心一意
兩面三刀
三長兩短
三番五次
三三兩兩
三頭六臂
三心二意
三言兩語
html.login文件,確定驗證碼顯示的位置,用於登陸頁面
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 <script type="text/javascript"> 7 window.onload = function(){ 8 9 } 10 function changeImg(obj){ 11 obj.src="/WEB3/checkImg?time="+new Date().getTime(); 12 } 13 </script> 14 </head> 15 <body> 16 <form action="/WEB2/login" method="post"> 17 用戶名:<input type="text" name="username"><br/> 18 密碼:<input type="password" name="password"><br/> 19 驗證碼:<input type="text" name="username"><img onclick="changeImg(this)" src="/WEB3/checkImg"><br> 20 <input type="submit" value="登錄"><br/> 21 </form> 22 </body> 23 </html>