文章詳情頁
jsp實現簡單用戶7天內免登錄
瀏覽:88日期:2022-06-07 13:42:58
本文實例為大家分享了jsp實現簡單用戶7天內免登錄的具體代碼,供大家參考,具體內容如下
(1)登陸頁面:login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <form action="LoginServlet" method="post"> 用戶名:<input type="text" name="username" value="${cookie.cook_name.value }"/><br/> <input type="checkbox" name="chkCookie" value="7"/>記住我,7天內免登錄<br/> <input type="submit" value="登錄"/><br/> <a href="sessionUser.jsp" rel="external nofollow" >驗證session</a> <a href="cookieUser.jsp" rel="external nofollow" >驗證cookie</a> </form> </body> </html>
(2)跳轉頁面:cookieUser.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% //從瀏覽器獲取cookie Cookie[] cookies = request.getCookies(); String uname = null; //判斷語句條件小技巧 if(null != cookies){ for(Cookie cookie:cookies){ //遍歷本地瀏覽器中的所有cookie if(cookie.getName().equals("cook_name")){ //把cookie中的用戶名取出 uname = cookie.getValue(); } //out.print("<p>name: " + cookie.getName()+"</p>"); //out.print("<p>value: " + cookie.getValue()+"</p>"); } } //判斷cookie有沒有失效 if(null != uname){ out.print("<p>用戶名:"+uname +"</p>"); } else{ response.sendRedirect("login.jsp"); } %> </body> </html>
(3)Servlet文件:LoginServlet類
package com.servlet; import java.io.IOException; 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; import javax.servlet.http.HttpSession; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //獲取用戶登錄名 String username = request.getParameter("username"); //獲取7天內是否免登錄 String day7 = request.getParameter("chkCookie"); int cookie_day = 1; if(null != day7 && day7.equals("")){ cookie_day = Integer.valueOf(day7); } //創建cookie Cookie cookie = new Cookie("cook_name",username); //設置Cookie有限期 //cookie.setMaxAge(6);//有效期6秒 cookie.setMaxAge(3600 * 24 * cookie_day);//設置7天免登錄 //寫入cookie到瀏覽器 response.addCookie(cookie); response.sendRedirect("cookieUser.jsp"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。
標簽:
JSP
上一條:JSP數據交互實現過程解析下一條:jsp網頁實現貪吃蛇小游戲
排行榜
