本文实例为大家分享了JavaWeb实现简单的自动登录功能的具体代码,供大家参考,具体内容如下
用最近所学的知识点实现自动登录,主要有:
1、Filter过滤器
2、session & cookie
一、需求分析
二、准备工作
1. 项目目录
2. 导入相应的jar包
三、代码实现
1. 搭建环境
1.1 搭建数据库、数据库表
数据库【user】,数据库表【t_user】
1.2 搭建页面
登录页面【login.jsp】
1
2
3
4
5
6
7
8
9
10
|
< body > < form action = "LoginServlet" method = "post" > 账号:< input type = "text" name = "username" >< br > 密码:< input type = "password" name = "password" >< br > < input type = "checkbox" name = "auto_login" >自动登录< br > < input type = "submit" value = "登录" > </ form > </ body > |
首页【index.jsp】
注意:导入<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
1
2
3
4
5
6
7
8
9
10
|
< body > 这是首页, < c:if test = "${not empty userBean }" > 欢迎你,${userBean.username } </ c:if > < c:if test = "${empty userBean }" > 你好,请登录! </ c:if > </ body > |
2. 登录servlet代码【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
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String userName = request.getParameter( "username" ); String password = request.getParameter( "password" ); String autoLogin = request.getParameter( "auto_login" ); UserBean user = new UserBean(); user.setUsername(userName); user.setPassword(password); UserDao dao = new UserDaoImpl(); UserBean userBean = dao.login(user); if (userBean != null ) { //成功了,进入首页 request.getSession().setAttribute( "userBean" , userBean); response.sendRedirect( "index.jsp" ); } else { //不成功 request.getRequestDispatcher( "login.jsp" ).forward(request, response); } } catch (SQLException e) { e.printStackTrace(); } |
3. 过滤器filter代码【AutoLoginFilter.java】
实现思路:
1、先判断session是否有效,如果有效,就不用取cookie了,直接放行;
2、如果session失效了,那么就取cookie。
a. 取出cookie的值,然后完成登录;
b. 把这个用户的值存储到session中;
c. 放行。
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
|
public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { HttpServletRequest request = (HttpServletRequest) req; //先判断,session中还有没有userBean UserBean userBean = (UserBean) request.getSession().getAttribute( "userBean" ); //有---session有效 if (userBean != null ) { chain.doFilter(request, response); } else { //session失效了----看cookie //1.来请求的时候,先从请求里面取出cookie,但是cookie里有很多的key-value Cookie[] cookies = request.getCookies(); //2.从一堆的cookie里面找出以前给浏览器发的那个cookie Cookie cookie = CookieUtil.findCookie(cookies, "auto_login" ); //第一次登录 if (cookie == null ) { chain.doFilter(request, response); } else { //不是第一次登录 String value = cookie.getValue(); String username = value.split( "#" )[ 0 ]; String password = value.split( "#" )[ 1 ]; //完成登录 UserBean user = new UserBean(); user.setUsername(username); user.setPassword(password); UserDao dao = new UserDaoImpl(); userBean = dao.login(user); //将session值存到域中,方便下一次未过期前还可以用 request.getSession().setAttribute( "userBean" , userBean); chain.doFilter(request, response); } } } catch (Exception e) { e.printStackTrace(); chain.doFilter(req, response); } } |
4. 其他代码
4.1 方法findCookie()
作用:从一堆的cookie里面找出以前给浏览器发的那个cookie
【CookieUtil.java】
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class CookieUtil { public static Cookie findCookie(Cookie[] cookies,String name) { if (cookies != null ) { for (Cookie cookie: cookies) { if (name.equals(cookie.getName())) { return cookie; } } } return null ; } } |
4.2 Bean类
【UserBean.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
|
public class UserBean { private int id; private String username; private String password; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } } |
4.3 UserDao & UserDaoImpl
UserDao.java
1
2
3
4
5
6
7
8
|
public interface UserDao { /** * 执行登录,并且返回该用户所有的信息 * @param user 执行登录的用户信息 * @return */ UserBean login(UserBean user) throws SQLException; } |
UserDaoImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
|
public class UserDaoImpl implements UserDao { @Override public UserBean login(UserBean user) throws SQLException { QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); String sql = "select * from t_user where username = ? and password = ?" ; return runner.query(sql, new BeanHandler<UserBean>(UserBean. class ),user.getUsername(),user.getPassword()); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_44270855/article/details/104313785