Cookie 与 Session

来源:互联网 发布:matlab如何检验数据 编辑:程序博客网 时间:2024/05/17 16:16

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

<span style="font-size:14px;font-weight: normal;">public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//取得 cookiesCookie[] 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
原创粉丝点击