本文實(shí)例為大家分享了JavaWeb實(shí)現(xiàn)簡(jiǎn)單的自動(dòng)登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
用最近所學(xué)的知識(shí)點(diǎn)實(shí)現(xiàn)自動(dòng)登錄,主要有:
1、Filter過(guò)濾器
2、session & cookie
一、需求分析
二、準(zhǔn)備工作
1. 項(xiàng)目目錄
2. 導(dǎo)入相應(yīng)的jar包
三、代碼實(shí)現(xiàn)
1. 搭建環(huán)境
1.1 搭建數(shù)據(jù)庫(kù)、數(shù)據(jù)庫(kù)表
數(shù)據(jù)庫(kù)【user】,數(shù)據(jù)庫(kù)表【t_user】
1.2 搭建頁(yè)面
登錄頁(yè)面【login.jsp】
1
2
3
4
5
6
7
8
9
10
|
< body > < form action = "LoginServlet" method = "post" > 賬號(hào):< input type = "text" name = "username" >< br > 密碼:< input type = "password" name = "password" >< br > < input type = "checkbox" name = "auto_login" >自動(dòng)登錄< br > < input type = "submit" value = "登錄" > </ form > </ body > |
首頁(yè)【index.jsp】
注意:導(dǎo)入<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
1
2
3
4
5
6
7
8
9
10
|
< body > 這是首頁(yè), < c:if test = "${not empty userBean }" > 歡迎你,${userBean.username } </ c:if > < c:if test = "${empty userBean }" > 你好,請(qǐng)登錄! </ 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 ) { //成功了,進(jìn)入首頁(yè) request.getSession().setAttribute( "userBean" , userBean); response.sendRedirect( "index.jsp" ); } else { //不成功 request.getRequestDispatcher( "login.jsp" ).forward(request, response); } } catch (SQLException e) { e.printStackTrace(); } |
3. 過(guò)濾器filter代碼【AutoLoginFilter.java】
實(shí)現(xiàn)思路:
1、先判斷session是否有效,如果有效,就不用取cookie了,直接放行;
2、如果session失效了,那么就取cookie。
a. 取出cookie的值,然后完成登錄;
b. 把這個(gè)用戶的值存儲(chǔ)到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中還有沒(méi)有userBean UserBean userBean = (UserBean) request.getSession().getAttribute( "userBean" ); //有---session有效 if (userBean != null ) { chain.doFilter(request, response); } else { //session失效了----看cookie //1.來(lái)請(qǐng)求的時(shí)候,先從請(qǐng)求里面取出cookie,但是cookie里有很多的key-value Cookie[] cookies = request.getCookies(); //2.從一堆的cookie里面找出以前給瀏覽器發(fā)的那個(gè)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值存到域中,方便下一次未過(guò)期前還可以用 request.getSession().setAttribute( "userBean" , userBean); chain.doFilter(request, response); } } } catch (Exception e) { e.printStackTrace(); chain.doFilter(req, response); } } |
4. 其他代碼
4.1 方法findCookie()
作用:從一堆的cookie里面找出以前給瀏覽器發(fā)的那個(gè)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 { /** * 執(zhí)行登錄,并且返回該用戶所有的信息 * @param user 執(zhí)行登錄的用戶信息 * @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()); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/weixin_44270855/article/details/104313785