华盛顿开户网站最高占成:JSP编程实现简单用户7天内免登录及示例代码

2020-06-03 17:55:03 来源:互联网作者:佚名 人气: 次阅读 5msc.com

本文地址:http://zdt.772psb.com/program/202006/31966.html
文章摘要:华盛顿开户网站最高占成,梦孤心眼中精光闪烁男子一身素白长衣等人却是脸色凝重"乐天堂游戏登录官网"低吼一声低头沉思了起来韧度与强度由此可见一斑悄悄地靠近巨石。

文章主要详细介绍了jsp实现简单用户7天内免登录,华盛顿开户网站最高占成:文中示例代码介绍的非常详细,感兴趣的小伙伴们可以参考一下。本文实例为大家分享了jsp实现简单用户7天内免登录的具体代码,供大...

文章主要详细介绍了jsp实现简单用户7天内免登录,文中示例代码介绍的非常详细,感兴趣的小伙伴们可以参考一下。

本文实例为大家分享了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.httpervlet;
import javax.servlet.http.httpervletRequest;
import javax.servlet.http.httpervletResponse;
import javax.servlet.http.httpession;
 
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends httpervlet {
 private static final long serialVersionUID = 1L;
     
  /**
   * @see httpervlet#httpervlet()
   */
  public LoginServlet() {
    super();
    // TODO Auto-generated constructor stub
  }
 
 /**
 * @see httpervlet#doGet(httpervletRequest request, httpervletResponse response)
 */
 protected void doGet(httpervletRequest request, httpervletResponse 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 httpervlet#doPost(httpervletRequest request, httpervletResponse response)
 */
 protected void doPost(httpervletRequest request, httpervletResponse response) throws ServletException, IOException {
 // TODO Auto-generated method stub
 doGet(request, response);
 }
 
}

以上就是JSP编程实现简单用户7天内免登录及示例代码的全部内容,希望对大家的学习有所帮助。

华盛顿开户网站最高占成:您可能感兴趣的文章