JavaWeb實現(xiàn)簡單的自動登錄功能
本文實例為大家分享了JavaWeb實現(xiàn)簡單的自動登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
用最近所學的知識點實現(xiàn)自動登錄,主要有:
1、Filter過濾器
2、session & cookie
一、需求分析


二、準備工作
1. 項目目錄

2. 導入相應的jar包

三、代碼實現(xiàn)
1. 搭建環(huán)境
1.1 搭建數(shù)據(jù)庫、數(shù)據(jù)庫表
數(shù)據(jù)庫【user】,數(shù)據(jù)庫表【t_user】

1.2 搭建頁面
登錄頁面【login.jsp】
<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"%>
<body>
這是首頁,
<c:if test="${not empty userBean }">
歡迎你,${userBean.username }
</c:if>
<c:if test="${empty userBean }">
你好,請登錄!
</c:if>
</body>
2. 登錄servlet代碼【LoginServlet.java】
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】
實現(xiàn)思路:
1、先判斷session是否有效,如果有效,就不用取cookie了,直接放行;
2、如果session失效了,那么就取cookie。
a. 取出cookie的值,然后完成登錄;
b. 把這個用戶的值存儲到session中;
c. 放行。
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里面找出以前給瀏覽器發(fā)的那個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里面找出以前給瀏覽器發(fā)的那個cookie
【CookieUtil.java】
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】
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
public interface UserDao {
/**
* 執(zhí)行登錄,并且返回該用戶所有的信息
* @param user 執(zhí)行登錄的用戶信息
* @return
*/
UserBean login(UserBean user) throws SQLException;
}
UserDaoImpl.java
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());
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC實現(xiàn)RESTful風格:@PathVariable注解的使用方式
這篇文章主要介紹了SpringMVC實現(xiàn)RESTful風格:@PathVariable注解的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot自定義定時任務(wù)的實現(xiàn)示例
本文主要介紹了SpringBoot自定義定時任務(wù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-05-05
SpringBoot 如何添加容器啟動的初始化邏輯的操作方法
這篇文章主要介紹了SpringBoot 如何添加容器啟動的初始化邏輯,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
IntelliJ?IDEA運行SpringBoot項目的詳細步驟
這篇文章主要介紹了IntelliJ?IDEA如何運行SpringBoot項目,步驟一配置maven,步驟二配置JDK環(huán)境,緊接著通過步驟三檢查數(shù)據(jù)庫的配置,最后一步數(shù)據(jù)庫連接,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-08-08
jdk中動態(tài)代理異常處理分析:UndeclaredThrowableException
最近在工作中遇到了報UndeclaredThrowableException的錯誤,通過查找相關(guān)的資料,終于解決了,所以這篇文章主要給大家介紹了關(guān)于jdk中動態(tài)代理異常處理分析:UndeclaredThrowableException的相關(guān)資料,需要的朋友可以參考下2018-04-04
tk.Mybatis 插入數(shù)據(jù)獲取Id問題
本文主要介紹了tk.Mybatis 插入數(shù)據(jù)獲取Id問題,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
詳解Mybatis核心類SqlSessionFactory的構(gòu)建
這篇文章主要為大家詳細介紹了Mybatis核心類SqlSessionFactory的構(gòu)建過程,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2022-12-12
Schedule定時任務(wù)在分布式產(chǎn)生的問題詳解
這篇文章主要介紹了Schedule定時任務(wù)在分布式產(chǎn)生的問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

