预期的效果:
以百度为例:一: 登陆界面,显示下次自动登录---选中
![\](/uploadfile/Collfiles/20140103/2014010309114045.jpg)
二:点击登录,完成登录实现跳转。。。
<喎�"/kf/ware/vc/" target="_blank" class="keylink">vcD4KPC9oMj4KPGJyPgoKPGltZyBzcmM9"/uploadfile/Collfiles/20140103/2014010309114146.jpg" alt="\">
三:关闭页面:重新打开百度。。。
![\](/uploadfile/Collfiles/20140103/2014010309114147.jpg)
四:实现自动登录。。。完成跳转。。。
![\](/uploadfile/Collfiles/20140103/2014010309114146.jpg)
自我分析后,使用session,cookie filter 等技术实现基本功能。
代码区:
一:实体bean:
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
|
package
www.csdn.net.day56.bean;
public
class
Admin {
private
Integer id;
private
String name;
private
String pass;
public
Admin() {
super
();
// TODO Auto-generated constructor stub
}
public
Admin(Integer id, String name, String pass) {
super
();
this
.id = id;
this
.name = name;
this
.pass = pass;
}
public
Integer getId() {
return
id;
}
public
void
setId(Integer id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getPass() {
return
pass;
}
public
void
setPass(String pass) {
this
.pass = pass;
}
@Override
public
String toString() {
return
"Admin [id="
+ id +
", name="
+ name +
", pass="
+ pass +
"]"
;
}
}
|
二:AdminDao 接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package
www.csdn.net.day56.dao;
import
www.csdn.net.day56.bean.Admin;
public
interface
AdminDao
extends
BaseDao {
/**
* 用户登录验证的 操作
* @param name
* @param pass
* @return
*/
public
Admin checkLogin(String name,String pass);
/**
*
* @param name
* @return
*/
public
Admin checkLogin(String name);
}
</admin,>
|
三:Admin dao Impl 类 实现 AdminDao 的接口方法
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
|
package
www.csdn.net.day56.dao.impl;
import
java.sql.Connection;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
www.csdn.net.day56.bean.Admin;
import
www.csdn.net.day56.dao.AdminDao;
import
www.csdn.net.day56.util.DBConn;
public
class
AdminDaoImpl
implements
AdminDao{
private
Connection conn;
private
PreparedStatement pstmt;
private
ResultSet rs;
@Override
public
Admin checkLogin(String name, String pass) {
Admin entity=
null
;
conn=DBConn.getConn();
String sql=
"select id, name,pass from admin where name=? and pass=?"
;
try
{
pstmt=conn.prepareStatement(sql);
int
index=
1
;
pstmt.setString(index++, name);
pstmt.setString(index++, pass);
rs=pstmt.executeQuery();
if
(rs.next()){
entity=
new
Admin(rs.getInt(
"id"
), rs.getString(
"name"
), rs.getString(
"pass"
));
}
}
catch
(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
DBConn.release(rs, pstmt);
}
return
entity;
}
@Override
public
Admin checkLogin(String name) {
Admin entity=
null
;
conn=DBConn.getConn();
String sql=
"select id, name,pass from admin where name=?"
;
try
{
pstmt=conn.prepareStatement(sql);
int
index=
1
;
pstmt.setString(index++, name);
rs=pstmt.executeQuery();
if
(rs.next()){
entity=
new
Admin(rs.getInt(
"id"
), rs.getString(
"name"
), rs.getString(
"pass"
));
}
}
catch
(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
DBConn.release(rs, pstmt);
}
return
entity;
}
}
|
四:Admin Service 用来处理事务 或日志的接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package
www.csdn.net.day56.service;
import
www.csdn.net.day56.bean.Admin;
public
interface
AdminService
extends
BaseService{
/**
* 用户登录验证的操作
* @param name
* @param pass
* @return
*/
public
Admin checkLogin(String name,String pass);
/**
*
* @param name
* @return
*/
public
Admin checkLogin(String name);
}
</admin,>
|
五:Admin Service Impl 实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package
www.csdn.net.day56.service;
import
www.csdn.net.day56.bean.Admin;
import
www.csdn.net.day56.dao.AdminDao;
import
www.csdn.net.day56.dao.impl.AdminDaoImpl;
public
class
AdminServiceImpl
implements
AdminService {
private
AdminDao adminDao =
new
AdminDaoImpl();
@Override
public
Admin checkLogin(String name, String pass) {
System.out.println(
"。。。。记录日志操作。。。。。。。。"
);
Admin entity=adminDao.checkLogin(name, pass);
return
entity;
}
@Override
public
Admin checkLogin(String name) {
return
adminDao.checkLogin(name);
}
}
|
六: index.jsp 一个登录链接,跳转到loginServlet---- 经过filter过滤器
1
|
登录
|
七:autoLoginFilter 过滤器,对session会话,及cookie 等的检测
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
package
www.csdn.net.day56.filter;
import
java.io.IOException;
import
java.security.MessageDigest;
import
java.security.NoSuchAlgorithmException;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.http.Cookie;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
sun.misc.BASE64Encoder;
import
www.csdn.net.day56.bean.Admin;
import
www.csdn.net.day56.service.AdminService;
import
www.csdn.net.day56.service.AdminServiceImpl;
public
class
AutoLoginFilter
implements
Filter {
@Override
public
void
destroy() {
// TODO Auto-generated method stub
}
@Override
public
void
doFilter(ServletRequest req, ServletResponse res,
FilterChain chain)
throws
IOException, ServletException {
// 第一步造型
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) res;
// 1. 首先判断session 中 有没有 admin
Object object=request.getSession().getAttribute(
"admin"
);
if
(object!=
null
){
request.getRequestDispatcher(
"./sc.jsp"
).forward(request, response);
return
;
}
// 第二步 判断cookie 中是否存在
Cookie autoCookie=
null
;
Cookie cookies[]=request.getCookies();
if
(cookies!=
null
){
// 遍历cookie
for
(Cookie cookie : cookies) {
// 判断cookie 中是否有autologin 标示符的cookie
if
(
"autologin"
.equals(cookie.getName())){
autoCookie=cookie;
// 赋值
}
}
System.out.println(
"----------"
+autoCookie+
"------"
);
//判断cookie 中是否有
if
(autoCookie ==
null
){
chain.doFilter(request,response);
return
;
}
System.out.println(
"+++++++++++"
+autoCookie);
// 判断 cookie 的值
// 获取cookie 的值
String value=autoCookie.getValue();
// 拆分
String temp[]=value.split(
":"
);
System.out.println(
"length::::::::;;"
+temp.length);
// 判断长度 对否等于自己拼凑的长度
if
(temp.length!=
3
){
chain.doFilter(request, response);
return
;
}
String name=temp[
0
];
String time=temp[
1
];
String service_md5Value=temp[
2
];
System.out.println(Long.valueOf(time)+
"----- 时间------"
+System.currentTimeMillis());
// 判断是否失效
if
(Long.valueOf(time)<=System.currentTimeMillis()){
chain.doFilter(request, response);
return
;
}
// 根据用户名去查询永华
AdminService adminService=
new
AdminServiceImpl();
// 查询用户
Admin entity=adminService.checkLogin(name);
System.out.println(
"-------- 重新查询"
+entity);
if
(entity==
null
){
chain.doFilter(request, response);
return
;
}
// 拼接的字符
String md5Temp=entity.getName()+
":"
+entity.getPass()+
":"
+time;
System.out.println(md5Temp+
"-----"
+service_md5Value);
if
(!(md5Value(md5Temp).equals(service_md5Value))){
chain.doFilter(request, response);
return
;
}
request.getSession().setAttribute(
"admin"
, entity);
request.getRequestDispatcher(
"./sc.jsp"
).forward(request, response);
}
else
{
chain.doFilter(request, response);
return
;
}
}
@Override
public
void
init(FilterConfig arg0)
throws
ServletException {
// TODO Auto-generated method stub
}
public
String md5Value(String value) {
try
{
MessageDigest digest = MessageDigest.getInstance(
"md5"
);
byte
result[] = digest.digest(value.getBytes());
BASE64Encoder encoder =
new
BASE64Encoder();
return
encoder.encode(result);
}
catch
(NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
""
;
}
<p>} </p><h3>filter 在xml 文件中的描述信息</h3>
|
1
2
3
4
5
6
7
8
|
<filter>
<filter-name>AutoLoginFilter</filter-name>
<filter-
class
>www.csdn.net.day56.filter.AutoLoginFilter</filter-
class
>
</filter>
<filter-mapping>
<filter-name>AutoLoginFilter</filter-name>
<url-pattern>/login.
do
</url-pattern>
</filter-mapping>
|
八:loginServlet ---实现转发----login.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
|
package
www.csdn.net.day56.servlet;
import
java.io.IOException;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public
class
LoginServlet
extends
HttpServlet {
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
request.getRequestDispatcher(
"./login.jsp"
).forward(request, response);
}
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
this
.doGet(request, response);
}
<p>}</p>
|
loginServlet 在xml 中的 描述信息:
1
2
3
4
5
6
7
8
9
|
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-
class
>www.csdn.net.day56.servlet.LoginServlet</servlet-
class
>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.
do
</url-pattern>
</servlet-mapping>
|
九:登陆界面,表单提交到AdminServlet
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
|
<%@ page language=
"java"
import
=
"java.util.*"
pageEncoding=
"UTF-8"
%>
<%
@include
file=
"/common/tag.jsp"
%>
<%
@include
file=
"/common/jquery.jsp"
%>
<title>My JSP
'index.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"
>
-->
${requestScope.msg}<br>
<form action=
"${pageContext.request.contextPath }/AdminOper.do"
method=
"get"
>
<table>
<tbody><tr>
<td>用户名:</td>
<td><input type=
"text"
name=
"name"
></td>
</tr>
<tr>
<td>密码</td>
<td><input type=
"password"
name=
"pass"
></td>
</tr>
<tr>
<td colspan=
"2"
>
<input type=
"radio"
name=
"day"
value=
"1"
> 一天
<input type=
"radio"
name=
"day"
value=
"7"
> 一周
<input type=
"radio"
name=
"day"
value=
"15"
> 半月
<input type=
"radio"
name=
"day"
value=
"30"
> 一月
<input type=
"radio"
name=
"day"
value=
"180"
> 半年
<input type=
"radio"
name=
"day"
value=
"360"
> 一年
</td>
</tr>
<tr>
<td><input type=
"checkbox"
name=
"mark"
value=
"mark"
></td>
<td>下次自动登录</td>
</tr>
<tr>
<td><input type=
"submit"
value=
"登录"
></td>
<td><input type=
"reset"
value=
"重置"
></td>
</tr>
</tbody></table>
<input type=
"hidden"
name=
"oper"
value=
"login"
>
</form>
|
十:AdminServlet---实现对用户的验证及cookie,session 的存储调用
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
92
93
94
95
96
97
98
99
100
101
|
package
www.csdn.net.day56.servlet;
import
java.io.IOException;
import
java.security.MessageDigest;
import
java.security.NoSuchAlgorithmException;
import
javax.servlet.ServletException;
import
javax.servlet.http.Cookie;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
sun.misc.BASE64Encoder;
import
com.sun.mail.util.BASE64EncoderStream;
import
www.csdn.net.day56.bean.Admin;
import
www.csdn.net.day56.service.AdminService;
import
www.csdn.net.day56.service.AdminServiceImpl;
public
class
AdminServlet
extends
HttpServlet {
private
AdminService adminService =
new
AdminServiceImpl();
private
int
expires =
7
*
24
*
60
*
60
;
// 7 就是天数
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
// 获取 请求的 方法
String method = request.getMethod();
String oper = request.getParameter(
"oper"
);
if
(
"login"
.equals(oper)) {
String name = request.getParameter(
"name"
);
String pass = request.getParameter(
"pass"
);
String mark = request.getParameter(
"mark"
);
System.out.println(name + pass + mark +
"==========="
);
// 验证用户名和密码是否正确
Admin entity = adminService.checkLogin(name, pass);
if
(entity !=
null
) {
if
(
"mark"
.equals(mark)) {
// 声明 cookie
Cookie autoCookie =
null
;
// 获取所有的 cookie
Cookie cookies[] = request.getCookies();
// 遍历cookie
for
(Cookie cookie : cookies) {
// 判断是否存在自动登录记录
if
(
"autologin"
.equals(cookie.getName())) {
autoCookie = cookie;
// 赋值
// 当cookie 存在的时候 我需要重新设置值
long
time = System.currentTimeMillis() + expires
*
1000
;
String newValue = name +
":"
+ time +
":"
+ md5Value(name +
":"
+ pass +
":"
+ time);
cookie.setValue(newValue);
}
else
{
// 不在创建
long
time = System.currentTimeMillis() + expires
*
1000
;
String cookieValue = name +
":"
+ time +
":"
+ md5Value(name +
":"
+ pass +
":"
+ time);
autoCookie =
new
Cookie(
"autologin"
, cookieValue);
}
}
autoCookie.setMaxAge(expires);
// 10 分钟
response.addCookie(autoCookie);
// 添加里边去了
}
// admin 存入到 session
request.getSession().setAttribute(
"admin"
, entity);
request.getRequestDispatcher(
"./sc.jsp"
).forward(request,
response);
}
else
{
request.setAttribute(
"msg"
,
"用户名 或密码错误"
);
request.getRequestDispatcher(
",/index.jsp"
).forward(request,
response);
}
}
}
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
this
.doGet(request, response);
}
public
String md5Value(String value) {
MessageDigest digest;
try
{
digest = MessageDigest.getInstance(
"md5"
);
byte
result[] = digest.digest(value.getBytes());
BASE64Encoder encoder =
new
BASE64Encoder();
return
encoder.encode(result);
}
catch
(NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
""
;
}
}
|
AdminServlet 在web.xml 的描述信息:
1
2
3
4
5
6
7
8
9
|
<servlet>
<servlet-name>AdminServlet</servlet-name>
<servlet-
class
>www.csdn.net.day56.servlet.AdminServlet</servlet-
class
>
</servlet>
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/AdminOper.
do
</url-pattern>
</servlet-mapping>
|
十一:检测成功:跳转到指定的界面--显示登录人的信息
1
|
欢迎${sessionScope.admin.name}登录<br>
|
效果展示:
一:输入指定的地址:点击登录:
![\](/uploadfile/Collfiles/20140103/2014010309114148.jpg)
二:提交表单: 跳转到指定的jsp 页面:
![\](/uploadfile/Collfiles/20140103/2014010309114249.jpg)
三:跳转到指定页面--显示登录信息![\](/uploadfile/Collfiles/20140103/2014010309114250.jpg)
四:重新打开index 界面--点击登录将直接跳转到登录成功页面---这是session会话的作用
![\](/uploadfile/Collfiles/20140103/2014010309114148.jpg)
![\](/uploadfile/Collfiles/20140103/2014010309114250.jpg)
五:关闭Tomact--session会话结束--用来检测cookie
![\](/uploadfile/Collfiles/20140103/2014010309114148.jpg)
![](/uploadfile/Collfiles/20140103/2014010309114351.jpg)
以上是使用filter,cookie,session等技术是小的效果。。。仅供参考。。。希望能给大家解除疑惑