這篇文章主要介紹java web如何實現(xiàn)自動登錄功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
十多年的平樂網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整平樂建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“平樂網(wǎng)站設計”,“平樂網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
具體內(nèi)容如下
主要思路就是:當用戶訪問網(wǎng)站的首頁時,瀏覽器端會先檢擦瀏覽器中存在的cookie中是否又登錄的用戶的用戶名,如果有,則直接跳轉(zhuǎn)至用戶登錄好的界面,如果沒有,則重定向至登錄界面,在服務器端創(chuàng)建該用戶登錄的cookie,響應時,將創(chuàng)建的cookie返回至瀏覽器端保存。
一、用戶訪問首頁時檢查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不存在,在手動登錄的過程中創(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);//有效期為一個星期 response.addCookie(cookie); request.setAttribute("name",name); request.getRequestDispatcher("user.view").forward(request, response); } }else{ response.sendRedirect("login.jsp"); } } }
三、登錄成功時的視圖層
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("<h2>"+"welcome "+name+" login"+"</h2>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
以上是“java web如何實現(xiàn)自動登錄功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞標題:javaweb如何實現(xiàn)自動登錄功能
標題鏈接:http://www.rwnh.cn/article36/jdgipg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊、營銷型網(wǎng)站建設、小程序開發(fā)、響應式網(wǎng)站、、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)