java web實(shí)現(xiàn)自動(dòng)登錄功能
本文實(shí)例為大家分享了java web實(shí)現(xiàn)自動(dòng)登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
主要思路就是:當(dāng)用戶訪問網(wǎng)站的首頁時(shí),瀏覽器端會(huì)先檢擦瀏覽器中存在的cookie中是否又登錄的用戶的用戶名,如果有,則直接跳轉(zhuǎn)至用戶登錄好的界面,如果沒有,則重定向至登錄界面,在服務(wù)器端創(chuàng)建該用戶登錄的cookie,響應(yīng)時(shí),將創(chuàng)建的cookie返回至瀏覽器端保存。
一、用戶訪問首頁時(shí)檢查cookie是否存在。
package ahpudong.com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/index.do") public class index extends HttpServlet { public index() { super(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie[] cookies=request.getCookies(); if(cookies!=null){ for(Cookie cookie:cookies){ String name=cookie.getName(); String value=cookie.getValue(); if("name".equals(name) && "chendong".equals(value)){ request.setAttribute(name, value); request.getRequestDispatcher("user.view").forward(request, response); return; } } } response.sendRedirect("login.jsp"); } }
二、確定用戶登錄的cookie不存在,在手動(dòng)登錄的過程中創(chuàng)建用戶登錄cookie。
package ahpudong.com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/login.do") public class login extends HttpServlet { public login() { super(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); String name=request.getParameter("name"); String pwd=request.getParameter("pwd"); String login=request.getParameter("auto"); if("chendong".equals(name) && "123456".equals(pwd)){ if("on".equals(login)){ Cookie cookie=new Cookie("name","chendong"); cookie.setMaxAge(7*24*60*60);//有效期為一個(gè)星期 response.addCookie(cookie); request.setAttribute("name",name); request.getRequestDispatcher("user.view").forward(request, response); } }else{ response.sendRedirect("login.jsp"); } } }
三、登錄成功時(shí)的視圖層
package ahpudong.com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/user.view") public class User extends HttpServlet { public User() { super(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request,response); } public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ response.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); String name=(String) request.getAttribute("name"); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println("<h1>"+"welcome "+name+" login"+"</h1>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)紀(jì)元秒和本地日期時(shí)間互換的方法【經(jīng)典實(shí)例】
這篇文章主要介紹了Java實(shí)現(xiàn)紀(jì)元秒和本地日期時(shí)間互換的方法,結(jié)合具體實(shí)例形式分析了Java日期時(shí)間相關(guān)操作技巧,需要的朋友可以參考下2017-04-04Java實(shí)現(xiàn)批量修改txt文件名稱的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)批量修改txt文件名稱的方法,結(jié)合實(shí)例形式分析了Java針對(duì)目錄文件遍歷及文件讀寫、屬性操作等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03非常全面的Java?SpringBoot點(diǎn)贊功能實(shí)現(xiàn)
但是這些功能再項(xiàng)目中是高頻出現(xiàn)的,如果直接操作數(shù)據(jù)庫的話,對(duì)數(shù)據(jù)庫壓力太大。那遇到這個(gè)問題怎么解決?這篇文章主要給大家介紹了關(guān)于Java?SpringBoot點(diǎn)贊功能實(shí)現(xiàn)?的相關(guān)資料,需要的朋友可以參考下2022-01-01SpringBoot中創(chuàng)建的AOP不生效的原因及解決
這篇文章主要介紹了SpringBoot中創(chuàng)建的AOP不生效的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11深入了解Java中Synchronized關(guān)鍵字的實(shí)現(xiàn)原理
synchronized是JVM的內(nèi)置鎖,基于Monitor機(jī)制實(shí)現(xiàn),每一個(gè)對(duì)象都有一個(gè)與之關(guān)聯(lián)的監(jiān)視器?(Monitor),這個(gè)監(jiān)視器充當(dāng)了一種互斥鎖的角色,本文就詳細(xì)聊一聊Synchronized關(guān)鍵字的實(shí)現(xiàn)原理,需要的朋友可以參考下2023-06-06IDEA?Error:java:無效的源發(fā)行版:13的解決過程
之前用idea運(yùn)行時(shí),也會(huì)出現(xiàn)這種情況,后面通過網(wǎng)上的資料解決了這個(gè)問題,下面這篇文章主要給大家介紹了關(guān)于IDEA?Error:java:無效的源發(fā)行版:13的解決過程,需要的朋友可以參考下2023-01-01SpringBoot可視化接口開發(fā)工具magic-api的簡(jiǎn)單使用教程
作為Java后端開發(fā),平時(shí)開發(fā)API接口的時(shí)候經(jīng)常需要定義Controller、Service、Dao、Mapper、XML、VO等Java對(duì)象。有沒有什么辦法可以讓我們不寫這些代碼,直接操作數(shù)據(jù)庫生成API接口呢?今天給大家推薦一款工具magic-api,來幫我們實(shí)現(xiàn)這個(gè)小目標(biāo)!2021-06-06java實(shí)現(xiàn)簡(jiǎn)單的webservice方式
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的webservice方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json)
本文主要介紹了Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07