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+"/";
%>



  
    
    
    My JSP 'login.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
  <%
  //拿到客户端的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);
	  }
  }
  
  }
	 
		  %>
             
账户:
密码:
自动登录

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+"/";
%>
<


  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
    <% 
    //从客户端取出存入的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");
		%>
		
欢迎<%=user%>登录!!
<% } } %>


你可能感兴趣的:(java,web)