1、首先新建一各專門生成驗證碼圖片的類VerifyCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
public class VerifyCode { private int w= 70 ; private int h= 35 ; private Random r= new Random(); //宋體,華文楷書,黑體,華文新魏,華文隸書,微軟雅黑,楷體_GB2312,Times New Roman private String [] fontNames={ "宋體" , "華文楷書" , "黑體" , "華文新魏" , "微軟雅黑" , "華文隸書" , "楷體_GB2312" , "Times New Roman" }; //可選的字符 private String codes= "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ" ; //背景色 private Color bgColor= new Color( 255 , 255 , 255 ); //驗證碼上的文本 private String text; //生成隨機顏色 private Color randomColor(){ int red=r.nextInt( 150 ); int green=r.nextInt( 150 ); int blue=r.nextInt( 150 ); return new Color(red,green,blue); } //生成隨機字體 private Font randomFont(){ int index=r.nextInt(fontNames.length); //獲取下標 String fontName=fontNames[index]; //生成隨機的字體名稱 int style=r.nextInt( 4 ); //獲取隨機樣式,0表示無樣式,1表示粗體,2表示斜體,3表示粗體加斜體 int size=r.nextInt( 5 )+ 24 ; //生成隨機字號,24~28 return new Font(fontName,style,size); } //畫干擾線 private void drawLine(BufferedImage image){ int num= 3 ; //一共畫3條 Graphics2D g2=(Graphics2D) image.getGraphics(); for ( int i= 0 ;i<num;i++){ //隨機生成坐標,即4個值 int x1=r.nextInt(w); int y1=r.nextInt(h); int x2=r.nextInt(w); int y2=r.nextInt(h); g2.setStroke( new BasicStroke( 1 .5F)); //設置筆畫的寬度 g2.setColor(Color.blue); //干擾線顏色 g2.drawLine(x1, y1, x2, y2); //畫線 } } //隨機生成一個字符 private char randomChar(){ int index=r.nextInt(codes.length()); return codes.charAt(index); } //創建BufferedImage private BufferedImage createImage(){ BufferedImage image= new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphics2D g2=(Graphics2D) image.getGraphics(); g2.setColor( this .bgColor); g2.fillRect( 0 , 0 , w, h); return image; } //外界調用這個方法得到驗證碼 public BufferedImage getImage(){ BufferedImage image=createImage(); //創建圖片緩沖區 Graphics2D g2=(Graphics2D)image.getGraphics(); //得到繪制環境 StringBuilder sb= new StringBuilder(); //用來裝載生成的驗證碼文本 //向圖中畫4個字符 for ( int i= 0 ;i< 4 ;i++){ //循環4次 String s=randomChar()+ "" ; //隨機生成一個字符 sb.append(s); //把字母添加到sb中 double x=i* 1.0 *w/ 4 ; //設置當前字符的x軸坐標 g2.setFont(randomFont()); //設置隨機字體 g2.setColor(randomColor()); //設置隨機顏色 g2.drawString(s, ( int )x, h- 5 ); } this .text=sb.toString(); //把生成的字符串賦給了this.text drawLine(image); //添加干擾線 return image; } //返回驗證碼圖片上面的文本 public String getText(){ return text; } //保存圖片到指定的輸出流 public static void output(BufferedImage image,OutputStream out) throws IOException{ ImageIO.write(image, "JPEG" , out); } } |
2、在jsp頁面上應用:
其核心代碼如下:
1
2
3
4
5
6
7
8
|
< form action = "/test/LoginServlet" method = "post" > 用戶名:< input type = "text" name = "username" value="<%=uname %>">< br > 密 碼:< input type = "password" name = "password" >< br > 驗證碼:< input type = "text" name = "verifyCode" > < img id = "img" src = "/test/VerifyCodeServlet" > < a href = "JavaScript:_change()" rel = "external nofollow" >看不清換一張</ a >< br > < input type = "submit" value = "登錄" > </ form > |
看不清換一張使用javascript實現,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
|
<script type= "text/javascript" > function _change(){ /** *1.得到image圖片 *2.修改其src為/test/VerifyCodeServlet */ var imgEle=document.getElementById( "img" ); imgEle.src= "/test/VerifyCodeServlet?a=" + new Date().getTime(); } </script> |
+new Date().getTime()這條語句可以欺騙瀏覽器,以為請求是新的,實現驗證碼的改變。
3、在第2步中,VerifyCodeServlet的實現生成一張驗證碼,并且通過session域保存驗證碼圖片對應的文本內容,為后面的登錄驗證驗證碼是否正確做準備。核心代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class VerifyCodeServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1.生成圖片 * 2.保存圖片上的文本到session域中 * 3.把圖片響應給客戶端 */ VerifyCode vc= new VerifyCode(); BufferedImage image=vc.getImage(); //保存圖片上的文本到session域 request.getSession().setAttribute( "session_code" , vc.getText()); VerifyCode.output(image, response.getOutputStream()); } } |
4、最后一步,實現登錄驗證驗證碼是否正確,其核心代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class LoginServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 校驗驗證碼 * 1.從session中獲取正取的驗證碼 * 2.從表單中獲取用戶填寫的驗證碼 * 3.進行比較 * 4.如果相同,向下運行,否則保存錯誤信息到request域,轉發到login.jsp */ String sessionCode=(String) request.getSession().getAttribute( "session_code" ); //獲得的驗證碼里面對應的文本內容 String paramCode=request.getParameter( "verifyCode" ); //用戶輸入文本框里面的內容 if (!paramCode.equalsIgnoreCase(sessionCode)){ request.setAttribute( "msg" , "驗證碼錯誤!" ); request.getRequestDispatcher( "/login.jsp" ).forward(request, response); //打回到登錄界面。 return ; //返回,不繼續執行下面的語句。 } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。