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}");
	}
    
%>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值