這篇文章主要介紹java web如何實(shí)現(xiàn)自動(dòng)登錄功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比紅山網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式紅山網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋紅山地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴(lài)。具體內(nèi)容如下
主要思路就是:當(dāng)用戶(hù)訪問(wèn)網(wǎng)站的首頁(yè)時(shí),瀏覽器端會(huì)先檢擦瀏覽器中存在的cookie中是否又登錄的用戶(hù)的用戶(hù)名,如果有,則直接跳轉(zhuǎn)至用戶(hù)登錄好的界面,如果沒(méi)有,則重定向至登錄界面,在服務(wù)器端創(chuàng)建該用戶(hù)登錄的cookie,響應(yīng)時(shí),將創(chuàng)建的cookie返回至瀏覽器端保存。
一、用戶(hù)訪問(wèn)首頁(yè)時(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"); } }
二、確定用戶(hù)登錄的cookie不存在,在手動(dòng)登錄的過(guò)程中創(chuàng)建用戶(hù)登錄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(""); out.println(""); out.println("A Servlet "); out.println(" "); out.println(""+"welcome "+name+" login"+"
"); out.println(" "); out.println(""); out.flush(); out.close(); } }
以上是“java web如何實(shí)現(xiàn)自動(dòng)登錄功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!