欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp实现简单用户7天内免登录

程序员文章站 2022-03-21 12:50:06
本文实例为大家分享了jsp实现简单用户7天内免登录的具体代码,供大家参考,具体内容如下(1)登陆页面:login.jsp<%@ page language="java" contenttype=...

本文实例为大家分享了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);
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。