Java学习之Cookie与Session篇

Java学习之Cookie与Session篇

0x00 前言

到后面的学习中,其实大部分都是一些类的调用,只要清楚一些方法的使用。

cookie 概念:客户端会话技术,将数据保存到客户端

常见方法:

1. 创建Cookie对象,绑定数据
			* new Cookie(String name, String value) 
2. 发送Cookie对象
response.addCookie(Cookie cookie)

3. 获取Cookie,拿到数据
* Cookie[]  request.getCookies() 

4.* setMaxAge(int seconds)

正数:将Cookie数据写到硬盘的文件中。持久化存储。并指定cookie存活时间,时间到后,cookie文件自动失效

负数:默认值

零:删除cookie信息
		
5.* setPath(String path):设置cookie的获取范围。默认情况下,设置当前的虚拟目录
* 如果要共享,则可以将path设置为"/",cookie默认情况下不共享	

6. setDomain(String path):如果设置一级域名相同,那么多个服务器之间cookie可以共享
	

cookieservlet1代码:


@WebServlet("/CookieServlet")
public class CookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("name","hello");
        cookie.setMaxAge(10);  //cookie 存储10秒
        cookie.setPath("/");  //设置共享

        response.addCookie(cookie);

    }

cookieservlet2代码:

@WebServlet("/CookieServlet2")
public class CookieServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println("cookie名"+name);
            System.out.println("cookie值"+value);
        }
    }
}

0x02 Session

服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象中。

常用方法:

 request.getSession()     :获取session对象
 
 
 session方法:
 
 Object getAttribute(String name)  
		void setAttribute(String name, Object value)
		void removeAttribute(String name)  
 

Cookie 和Session 不同的地方是 cookie是存在于客户端,而session是存在于服务器上。

如果客户端关闭后,服务端不关闭,session需要相同,则可以创建Cookie,键为JSESSIONID,设置最大存活时间,让cookie持久化保存。


@WebServlet("/CookieServlet")
public class CookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();  //获取session对象
        Cookie cookie = new Cookie("JSESSIONID",session.getId());  //cookie传入session的值


        cookie.setMaxAge(10);  //cookie 存储10秒
        cookie.setPath("/");  //设置共享

        response.addCookie(cookie);

    }
}

由此可见,session是依赖于cookie的。

当服务器关闭后,会将session序列化到硬盘里面,重启的时候seesion文件会转换为内存中的session对象。

session默认的过期时间是30秒,如果需要设置可以到tomcat/conf/web.xml文件里面修改。


<session-config>
		        <session-timeout>60</session-timeout>
		    </session-config>

0x03 结尾

结尾处就贴张图吧!

posted @ 2020-08-21 03:33  nice_0e3  阅读(385)  评论(0编辑  收藏  举报