本文实例为大家分享了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); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|