驗證碼的實現原理:
在一個Servlet中生成驗證,并把驗證碼上的數據保存在Session,用戶提交驗證碼之后,會提交給另外一個Servlet程序。在獲取用戶提交數據的Servlet中的從Session中把驗證碼取出,在取出的同時從Session中把驗證碼刪除。
1.注冊頁面:register.jsp
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
|
<%@ 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%>" rel= "external nofollow" > <title>My JSP 'login.jsp' starting page</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" > <!-- <link rel= "stylesheet" type= "text/css" href= "styles.css" rel= "external nofollow" > --> <script type= "text/javascript" > function _changImg(){ document.getElementById( "myimg" ).src = "/day11/checkImg?" + new Date().getMilliseconds(); } </script> </head> <body> <center> <form action= "/day11/regist" method= "post" > <table> <tr> <td>用戶名<font color= "red" >*</font></td> <td><input type= "text" name= "name" /></td> </tr> <tr> <td>密碼</td> <td><input type= "password" name= "password" /></td> </tr> <tr> <td>輸入驗證碼</td> <td> <input type= "text" name= "form_checkcode" /> <img style= "cursor: pointer;" alt= "" src= "/day11/checkImg" id= "myimg" onclick= "_changImg();" /> <a href= "javascript:void(0);" rel= "external nofollow" onclick= "_changImg();" >看不清,換一張</a> <font color= "red" >${imgError }</font> </td> </tr> <tr> <td><input type= "submit" value= "注冊" /></td> </tr> </table> </form> </center> </body> </html> |
2.生成驗證碼參考:cn.itcast.session.CheckImgServlet
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
|
public class CheckImgServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width = 120 ; int height = 40 ; // 先生成一張紙 BufferedImage bufi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 畫筆 Graphics g = bufi.getGraphics(); // 設置背景顏色 // 修改畫筆顏色 g.setColor(Color.white); // 填充 g.fillRect( 0 , 0 , width, height); // 繪制邊框 // 設置邊框顏色 g.setColor(Color.red); // 畫邊框 g.drawRect( 0 , 0 , width - 1 , height - 1 ); // 準備一些數據 String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz" ; // 生成隨機對象 Random r = new Random(); String checkcode = "" ; // 生成4個隨機的數字 for ( int i = 0 ; i < 4 ; i++) { // 生成隨機顏色 g.setColor( new Color(r.nextInt( 255 ), r.nextInt( 255 ), r.nextInt( 255 ))); // 設置字體 g.setFont( new Font( "宋體" , Font.ITALIC, 25 )); String c = data.charAt(r.nextInt(data.length())) + "" ; g.drawString(c, 10 + ( 20 * i), 30 ); checkcode += c; } // 將生成的驗證碼放到 session中 request.getSession().setAttribute( "session_checkcode" , checkcode); // 畫干擾線 for ( int i = 0 ; i < 8 ; i++) { // 設置隨機顏色 g.setColor( new Color(r.nextInt( 255 ), r.nextInt( 255 ), r.nextInt( 255 ))); // 畫線 兩點確定一線 g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height)); } // 將圖片輸出到瀏覽器 ImageIO.write(bufi, "jpg" , response.getOutputStream()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } |
3.驗證碼的驗證參考:cn.itcast.session.RegistServlet
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
|
public class RegistServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 校驗驗證碼的有效性: 服務端生成的驗證碼 和 表單提交的驗證碼一致才算有效 // 服務端生成的驗證碼 保存在 session容器中 // 獲得session中的驗證碼 HttpSession session = request.getSession(); String session_checkcode = (String) session.getAttribute( "session_checkcode" ); // 使用完后,迅速清除session中驗證碼的屬性 session.removeAttribute( "session_checkcode" ); // 獲得表單提交的驗證碼 String form_checkcode = request.getParameter( "form_checkcode" ); // 判斷什么是非法 如果非法,終止 if (session_checkcode == null || !session_checkcode.equals(form_checkcode)) { // 說明驗證碼錯誤 request.setAttribute( "imgError" , "驗證碼錯誤!" ); // 將request對象轉發給 login.jsp request.getRequestDispatcher( "/login.jsp" ).forward(request, response); // 結束當前方法 return ; } // 如果合法, 繼續校驗用戶名和密碼的有效 String username = request.getParameter( "username" ); String password = request.getParameter( "password" ); } } |
以上所述是小編給大家介紹的通過Session案例分析一次性驗證碼登錄問題,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
原文鏈接:http://blog.csdn.net/wearetheworld1/article/details/60464062