本文實例為大家分享了jsp實現簡單圖片驗證碼的具體代碼,供大家參考,具體內容如下
一、實現的功能分析
(1)在登陸頁面加驗證碼的功能,起到一定的安全性。在輸入正確的驗證碼,用戶名和密碼的情況下,才可以實現登錄。
(2)實現查詢數據庫的功能。在登陸后的頁面中,顯示用戶名和密碼,并且設置有一個超鏈接,實現查詢數據庫的功能。
(3)代碼核心是:隨機生成驗證碼,并且顯示在頁面上。同時要和輸入框中的輸入驗證碼進行校驗。
(4)主頁面使用img標簽的src屬性引入驗證頁面的jsp文件。
(5)驗證碼的實現頁面使用BufferedImage類的方法產生圖片。
(6)使用Graphics類的各種方法實現驗證碼的構成。
二、代碼實現
(1)登錄頁面:index.jsp文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >登錄頁面</ title > </ head > < body > < form action = "LoginServlet" method = "post" > 用戶名:< input name = "username" type = "text" value = "" />< br />< br /> 密碼:< input name = "password" type = "password" value = "" />< br />< br /> 驗證碼: < input type = "text" name = "checkCode" height = "20px " value = "" /> < img src = "CodeServlet" />< span >${error_code}</ span >< br /> < input type = "submit" value = "提交" > </ form > </ body > </ html > |
(2)登錄后的頁面:user.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
|
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import = "com.entity.Author"%> <!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >顯示登錄用戶的用戶名和密碼頁面</ title > </ head > < body > <% //內置對象 request.setCharacterEncoding("utf-8"); //獲取交互層放入session中的obj Author obj = (Author)session.getAttribute("authorInfo"); if(obj != null){ out.print("< p >用戶名:"+obj.getName()+"</ p >"); out.print("< p >密碼:"+obj.getId()+"</ p >"); } else{ response.sendRedirect("index.jsp"); } %> < br /> < a href = "AuthorServlet" >用戶信息查詢 </ a > </ body > </ html > |
(3)實現數據查詢頁面:ueslist.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
|
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >查詢信息顯示頁面</ title > </ head > < body > < table border = "1" > < tr > < td >編號</ td > < td >名稱</ td > < td >價格</ td > < td >數量</ td > < td >日期</ td > < td >風格</ td > </ tr > < c:forEach items = "${authorList}" var = "author" > < tr > < td >${author.id}</ td > < td >${author.name }</ td > < td >${author.price }</ td > < td >${author.num }</ td > < td >${author.dates}</ td > < td >${author.style}</ td > </ tr > </ c:forEach > </ table > </ body > </ html > |
(4)定義一個Author類,用于接收數據庫中的元素。
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
|
package com.entity; //用于獲取數據庫中的元素對象 public class Author { private int id; private String name; private int price ; private int num; private String dates; private String style; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getPrice() { return price; } public void setPrice( int price) { this .price = price; } public int getNum() { return num; } public void setNum( int num) { this .num = num; } public String getDates() { return dates; } public void setDates(String dates) { this .dates = dates; } public String getStyle() { return style; } public void setStyle(String style) { this .style = style; } } |
(5)登錄頁面的交互層:LoginServlet.java文件。用于登錄檢驗和驗證碼匹配。
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
|
//交互層(客戶端和服務器的交互) package com.servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.dao.AuthorDao; import com.entity.Author; /** * Servlet implementation class LoginServlet */ @WebServlet ( "/LoginServlet" ) public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super (); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //內置對象request,response request.setCharacterEncoding( "utf-8" ); HttpSession session = request.getSession(); //獲取用戶輸入驗證碼 String checkCode = request.getParameter( "checkCode" ); //獲取session中的驗證碼,也就是CodeServlet中生成的四個字符 String sessionCode = (String)session.getAttribute( "sCode" ); //驗證碼正確 if (checkCode.equals(sessionCode)) { //獲取表單數據 String username = request.getParameter( "username" ); int password = Integer.valueOf(request.getParameter( "password" )); //判斷用戶信息是否正確,查詢數據庫獲取用戶信息 AuthorDao ad = new AuthorDao(); Author obj = ad.check(username, password); //判斷 if (obj != null ) { //重新放入用戶信息 // HttpSession session = request.getSession(); session.setAttribute( "authorInfo" , obj); //設置session的有效期為10秒 session.setMaxInactiveInterval( 10 ); //頁面轉發 response.sendRedirect( "user.jsp" ); } else { //頁面重定向到登錄頁面 response.sendRedirect( "index.jsp" ); } } else { //驗證碼不正確 request.setAttribute( "error_code" , "驗證碼不匹配" ); request.getRequestDispatcher( "index.jsp" ).forward(request, response); }
|