cookie保存user数据及cookie实现自动登录

1.login.jsp


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <%
  //拿到客户端的cookies文件
  Cookie[] cookies=request.getCookies();
  //若cookies不为空,,循环去数据
  if(cookies != null)
  {
  
  for(Cookie c: cookies)
  {   //若有name为"autoLogin"的cookies,,直接跳转到主页面
	  if(c.getName().equals("autoLogin"))
	  {
	     request.getRequestDispatcher("index.jsp").forward(request,response);
	  }
  }
  
  }
	 
		  %><!-- 否则显示登录页面 -->
             <form method="post" action="servlet/LoginServlet">
               <table align="center">
                 <tr><td>账户:</td><td><input type="text" name="user"></td></tr>
                 <tr><td>密码:</td><td><input type="password" name="pwd"></td></tr>
                 <tr ><td><input type="checkbox" name="autologin">自动登录</td></tr>
                 <tr><td><input type="submit" value="提交"></td><td><input type="reset" value="取消"></td></tr>
                 <tr><td></td></tr>
               </table>
             </form>

    
  </body>
</html>

2,LoginServlet

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import java.net.URLEncoder;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

	public LoginServlet() {
		super();
	}


	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

        String user = request.getParameter("user");
        System.out.println(user);
        //设置user的cookie,完整页面间共享,替代session
        
        //中文保存数据  encode保存进去 decode出来 
        user = URLEncoder.encode(user,"utf-8");
        
        Cookie cookieUser = new Cookie("user", user);
        cookieUser.setMaxAge(20*60000);
        cookieUser.setPath("/");
        //将cookie添加到http首部里去
        response.addCookie(cookieUser);
        //判断是否自动登录,
        String autoLogin = request.getParameter("autologin");
      
        if(autoLogin != null)
        {
      
        Cookie autoLoginCookie=new Cookie("autoLogin","auto");
        autoLoginCookie.setMaxAge(20*60000);
        autoLoginCookie.setPath("/");
        response.addCookie(autoLoginCookie);
        }
      
        response.sendRedirect(request.getContextPath()+"/index.jsp");
	}

	public void init() throws ServletException {
	}

}

3,index.jsp

<%@ page language="java"  import="java.util.*,java.net.URLDecoder,com.servlet.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<<style>
.d1{font-size:xx-large;font-weight: 300;font-style:italic;color: red; }
</style>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <% 
    //从客户端取出存入的cookie用户信息
    String user=" ";
    Cookie[] cookies = request.getCookies();
    for (int i = 0; i < cookies.length; i++) 
    {
		if(cookies[i].getName().equals("user"))
		{
			user=cookies[i].getValue();
			user=URLDecoder.decode(user, "utf-8");
		%>
		<div align="center" class="d1"> 
		欢迎<%=user%>登录!!
		</div>
	<%
	    }
    }
    %>
    <div align="center">
  
    
    </div>
  </body>
</html>


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值