Cookie 与 Session


Cookie:存在浏览器端,不能跨浏览器访问

<span style="font-size:14px;font-weight: normal;">	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//取得 cookies
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
			for(Cookie cookie :cookies){
				System.out.println(cookie.getName()+":"+cookie.getValue());
			}
		}
		else{
			System.out.println("不存在 cookie ");
		}
		
		Cookie cookie=new Cookie("c", "123");
		cookie.setMaxAge(60*60*24);//设置失效时间,注意这里如果设置的值为 0  ,则进行的是删除操作
		response.addCookie(cookie);
	}

}</span>

注意:getCookies();//是说获得当前servlet所在目录 以及之上所有目录下的cookie 信息

cookie.setPaht("/项目1/");//通过setPath() 设置访问路径


cookie 中 value 存中文:


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Cookie[] cookies=request.getCookies();
		if(cookies != null){
			for(Cookie cookie :cookies){
				System.out.println(cookie.getName()+":"+cookie.getValue());
				if("cn".equals((cookie).getName())){
					String cnValue=URLDecoder.decode(cookie.getValue(),"UTF-8");
					System.out.println(cnValue);
				}
			}
		}
		else{
			System.out.println("没有 cookie ");
		}
		
		//Cookie cookie=new Cookie("cn","中文");//这样直接写中文是错误的
		
		String value="中文";
		String returnValue=URLEncoder.encode(value,"UTF-8");
		Cookie cookie=new Cookie("cn",returnValue);
		response.addCookie(cookie);
		
		
	}

注意:传参数之前对中文转码 URLEncoder.encode(value,"UTF-8"); 取参数时对中文转码用 URLDecoder.decode(cookie.getValue(),"UTF-8");

//编码
		String data="中文";
		String cnData1=URLEncoder.encode(data,"UTF-8");
		//解码
		String cnData2=URLDecoder.decode(cnData1,"UTF-8");
		
		//手动编码
		byte[] cnData3=data.getBytes("UTF-8");
		StringBuffer buf=new StringBuffer();
		for(int i=0;i<cnData3.length;i++){
			buf.append(cnData3[i]).append(",");
		}
		//删除最后一个 ,
		buf.deleteCharAt(buf.lastIndexOf(","));
		String cnData4=buf.toString();
		//手动解码
		String[] strs=cnData4.split(",");
		byte[] bs=new byte[strs.length];//注意,数组的长度和字符串的长度一个是属性,一个是方法
		for(int i=0;i<strs.length;i++){
			bs[i]=new Byte(strs[i]);
		}
		String cnData5=new String (bs,"UTF-8");


cookie 的 value 的最大长度:4 kb


Session : 存在服务器端

默认30分钟后销毁,也可以自己设置

tomcat 会自动创建一个会话级cookie,浏览器关闭就没了

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session=request.getSession();
		System.out.println(session.isNew());//是新创建的还是访问的以前
		
		System.out.println(session.getId());
		
		//持久化 session 
		Cookie cookie=new Cookie("JSESSIONID",session.getId());
		cookie.setMaxAge(60*30);//设置失效时间30分钟
		response.addCookie(cookie);
		System.out.println(cookie);
		
	}




如果浏览器禁用了cookie ,那么每次访问都会新创建一个 session ,如果想不让他每次都创建可以在地址栏输入 Jsessionid=...

//URL 重写 ,可以做到如果禁用了cookie时类似在地址栏写入 jsessionid  的作用
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session=request.getSession();
		System.out.println("url:"+session.getId());
		String url="DemoSessionServlet";
		
		//URL 重写 ,可以做到如果禁用了cookie时类似在地址栏写入jsessionid 的作用
		url=response.encodeURL(url);//如果不加这一句,并且浏览器禁用了cookie , 那么下面的连接将会新创建一个 sessionid,但是如果使用了这一句,那么这句话会将URL 重写,将当前页面的sessionid 传递给 下面连接的页面,也就是说连接后的 sessionid 和当前页面的sessionid 是相同的,如果浏览器没有禁用cookie ,那么URL 将不会发生改变
	
		PrintWriter out=response.getWriter();
		out.print("<a href='"+url+"'>demosession</a>");

	}


注意参数 url 必须有效,否则返回没有改变的页面,如果为空,则返回绝对路径+路径,当使用“/”开头时,相对于web 站点,即,加上网站名等路径
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值