国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

jsp實(shí)現(xiàn)簡單用戶7天內(nèi)免登錄

瀏覽:133日期:2022-06-07 13:42:58

本文實(shí)例為大家分享了jsp實(shí)現(xiàn)簡單用戶7天內(nèi)免登錄的具體代碼,供大家參考,具體內(nèi)容如下

(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天內(nèi)免登錄<br/>
  <input type="submit" value="登錄"/><br/>
  <a href="sessionUser.jsp" rel="external nofollow" >驗(yàn)證session</a>
  <a href="cookieUser.jsp" rel="external nofollow" >驗(yàn)證cookie</a>
</form>
</body>
</html>

(2)跳轉(zhuǎn)頁面: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天內(nèi)是否免登錄
 String day7 = request.getParameter("chkCookie");
 int cookie_day = 1;
 if(null != day7 && day7.equals("")){
  
  cookie_day = Integer.valueOf(day7);
 }
 
 //創(chuàng)建cookie
 Cookie cookie = new Cookie("cook_name",username);
 
 //設(shè)置Cookie有限期
 //cookie.setMaxAge(6);//有效期6秒
 cookie.setMaxAge(3600 * 24 * cookie_day);//設(shè)置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);
 }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。

標(biāo)簽: JSP
主站蜘蛛池模板: 疏附县| 岳阳市| 师宗县| 保山市| 城口县| 双江| 德化县| 商丘市| 托克托县| 琼海市| 大石桥市| 安康市| 伊吾县| 临潭县| 澎湖县| 福贡县| 剑川县| 醴陵市| 甘孜县| 黄石市| 博湖县| 扶余县| 迭部县| 宜春市| 砚山县| 上虞市| 西和县| 禹州市| 沙坪坝区| 三门峡市| 奎屯市| 洱源县| 十堰市| 上栗县| 长汀县| 株洲县| 宜兰县| 称多县| 叙永县| 沂南县| 黑河市|