Java操作Cookie

直接上代码

<%@ page language="java" pageEncoding="utf-8"%>
<%
	String username = request.getParameter("username") ;
	String password = request.getParameter("password") ;
	String method = request.getParameter("method") ;
	String systemKey = request.getParameter("systemKey") ;
	
	if("save".equals(method)){
		System.out.println("写入Cookie:"+username+"   "+password);
		Cookie cookie1 = new Cookie("username", java.net.URLEncoder.encode(username,"UTF-8"));
	    cookie1.setPath("/");//这个要设置
	    //cookie.setDomain(".localhost");//这个也要设置才能实现上面的两个网站共用
	    cookie1.setMaxAge(24*60*60*30);//不设置的话,则cookies不写入硬盘,而是写在内存,只在当前页面有用,以秒为单位
	    response.addCookie(cookie1);
	    
	    Cookie cookie2 = new Cookie("password", java.net.URLEncoder.encode(password,"UTF-8"));
	    cookie2.setPath("/");
	    cookie2.setMaxAge(24*60*60*30);
	    response.addCookie(cookie2);
	    
	    System.out.println("写入Map:key "+systemKey+"  user "+username+"   psd "+password);
	    java.util.Map map = com.targ.sso.common.OnlineUser.add(username,password) ;
    	com.targ.sso.common.OnlineUser.put(systemKey,map) ;
    	
	    out.print("{success : true}");
	    
	}else if("delete".equals(method)){
		System.out.println("删除Cookie:"+username+"   "+password);
		Cookie cookie1 = new Cookie("username", java.net.URLEncoder.encode("","UTF-8"));
	    cookie1.setPath("/");
	    cookie1.setMaxAge(0);
	    response.addCookie(cookie1);
	    
	    Cookie cookie2 = new Cookie("password", java.net.URLEncoder.encode("","UTF-8"));
	    cookie2.setPath("/");
	    cookie2.setMaxAge(0);
	    response.addCookie(cookie2);
	    
	    System.out.println("删除Map:key "+systemKey+"  user "+username+"   psd "+password);
    	com.targ.sso.common.OnlineUser.remove(systemKey) ;
    	
	    out.print("{success : true}");
	}
    
%>

 

你可能感兴趣的:(java,.net,SSO)