cookie&session (top)

cookie&session

Chapter 1 Conversational Technology

1. What is a session (understand)

In daily life, a series of questions and answers from dialing the phone to hanging up the phone is a conversation. The session process in web applications is similar to the phone call process in life. It refers to a series of request and response processes that occur continuously between a client (browser) and a web server. The entire shopping process is a session.

In short: from the time the user visits the website to leaving the website and closing the browser, this process is called a session.

1537159221506.png

Note: Each user will inevitably generate some data in the process of using the browser to communicate with the server, and the program must find a way to save these data for each user. There are two technologies used by Servlet to save data during the session: Cookie technology and Session technology.

【summary】

  1. Both Cookie and Session technology are generated during the session ;
  2. The role of Cookie and Session is to save data ;

The relevant knowledge about Cookie and Session will be explained in detail in the following sections.

Chapter 2 Cookie Technology

1. What is a cookie?

Cookie means biscuit in English. A cookie is a piece of information temporarily stored by the server on our computer (where? Open the browser), and use our computer to save some content that the server wants to save. The cookie is generated by the server and sent to the browser. The browser will save the data of the cookie in the form of key/value to a text file in a certain directory. The next time the same website is requested, the cookie will be sent to the server. Of course, the premise is that the browser is set to enable cookies.

Cookie function: reduce the pressure on the server to save data.

Question: We found that saving data in the browser? Instead of saving to the server or database?

将数据保存到服务器中,会增加服务器的压力。同时,如果用户不进行购买,并没有带来经济利润。存储到数据库中,如果没有登录到网站,那么将无法查询出数据。而存储到浏览器中可以减轻服务器端压力,并且用户不用登陆也可以将商品添加到购物车中。

关于cookie介绍如下图所示:

cookie concept introduction.png

如何在浏览器中查看当前cookie:

1533001312045.png

1533001358814.png

1533001375815.png

1533001420358.png

注意:通过上面演示我们发现,当我们访问某个网站的时候,网站服务器会在我们浏览器中生成一个cookie,并且存在的时间有的网站还会很久,这样对于用户来说很不好。占用户电脑的硬盘位置。那我们能否禁用cookie,不让网站在我们的电脑浏览器端生产cookie呢?

不可以。因为当我们禁用了cookie,首先商品不能保存到购物车中了,并且由于cookie被禁用了,会导致有的网站我们不能登录了。

2、Cookie的应用场景

在开发中,cookie使用最多的应用场景就是登录页面记住用户名和密码的功能。

1531056973943.png

登录的原理如下图所示:

使用cookie记住用户名和密码案例.png

注意:上述就是使用cookie技术记住用户名和密码的原理。但是由于我们还没有学习如何在页面中获取cookie中的数据,所以今天这里我们暂时还实现不了该整体的功能。但是我们可以使用接下来要学习的cookie的API能够实现除了在页面获取cookie数据的其他功能。

并且可以实现向服务器再次发送请求时,在另一个servlet中从cookie中获取用户名和密码。

实现功能如下图所示:

cookie记住用户名和密码2.png

3、Cookie基本API(重要)

Cookie的基本API包括Cookie的创建,往Cookie中添加数据,获取Cookie中的数据以及将Cookie响应给浏览器。

方法 使用示例 说明
创建Cookie对象 Cookie(String name,String value)name表示指定 cookie 名称 ,value 表示指定 cookie 值 Cookie c1 = new Cookie("username","suoge")
获取cookie的name值 String getName() c1.getName()
获取cookie的value值 String getValue() c1.getValue()
设置cookie的值 void setValue(String value) c1.setValue("李四")

说明:上述都是登录功能中需要的Cookie类中的API.但是该功能除了需要上述API,还需要如下API:

1、HttpServletResponse接口中的方法:
    void addCookie(Cookie cookie)   将cookie发送给浏览器
    
2、HttpServletRequest接口中的方法:
    Cookie[] getCookies()  获取浏览器发送的cookie

4、登录的实现的步骤

  • 步骤一:浏览器发送请求
  • 步骤二:服务器创建cookie并且响应cookie
  • 步骤三:浏览器自动保存cookie
  • 步骤四:浏览器再次发送请求时请求头中会携带cookie (不管第二次请求的是servlet,还是页面)

5、登录案例的代码实现

5.1、登录页面login.html的代码

<form action="/loginServlet" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    记住用户名和密码:<input type="checkbox" name="check"><br>
    <input type="submit" value="提交">
</form>

5.2、登录案例的java后台代码-将cookie响应给浏览器

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决乱码问题
        request.setCharacterEncoding("utf-8");
        //获取用户提交信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
         /*
            登录业务逻辑处理,到数据库查询用户名和密码是否存在
            假设存在
         */
        //获取CheckBox的值
        String check = request.getParameter("check");
​
        if ("on".equals(check)){
            //用户点击记住用户名和密码
            //生成cookie
            Cookie cookieName = new Cookie("name",username);
            Cookie cookiePwd = new Cookie("pwd",password);
            //将cookie响应回浏览器
            response.addCookie(cookieName);
            response.addCookie(cookiePwd);
        }
    }
}

浏览器接收到服务器响应的cookie结果如下所示:

cookie响应给浏览器结果.png

5.3、登录案例的java后台代码-浏览器再次向服务器发送请求并携带cookie给另一个servlet

【案例】从request请求中获取cookie

第一个案例完成之后,登录成功之后,访问一个新的Servlet,在这个Servlet中获取请求中携带的cookie。

@WebServlet("/getCookieServlet")
public class GetCookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //可以获取到浏览器保存所有cookie
        //获取cookie request
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            //获取cookie名称
            if ("name".equals(cookie.getName())){
                System.out.println(cookie.getName());
                //获取cookie的值
                System.out.println(cookie.getValue());
            }else  if ("pwd".equals(cookie.getName()))
            {
                System.out.println(cookie.getName());
                //获取cookie的值
                System.out.println(cookie.getValue());
            }
        }
    }
}

控制台结果

image.png

5.4、关于cookie中存储特殊字符问题 (理解原理即可)

tomcat8之前的版本,不支持中文,tomcat8以后的版本,支持中文.但是如果直接向cookie中存储特殊字符,例如空格,分号(;),逗号(,),等号(=)等特殊字符。那么就会出现问题。在向cookie中存储特殊字符之前必须要先进行编码处理,然后从cookie中取出之后在进行解码处理。

代码演示如下所示:

SpecialServlet.java

@WebServlet("/specialServlet")
public class SpecialServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //向cookie中保存特殊字符
        //定义一个字符串空格
        String str = "12  34";
        //将字符串str存储到cookie中
        Cookie cookie = new Cookie("demo",str);
        //cookie响应回浏览器
        response.addCookie(cookie);
    }
}

浏览器访问服务器结果:

cookie存储特殊字符错误.png

报错原因:cookie中不可以存储特殊的字符。

解决方案:

在向cookie中存储特殊字符之前必须要先进行编码处理,然后从cookie中取出之后在进行解码处理。

编码处理使用的API:

 String encode = URLEncoder.encode(str, "utf-8");

解码处理使用的API:

//对cookie进行解码
String decode = URLDecoder.decode(value, "utf-8");

演示编码处理特殊字符存储到cookie中代码:

@WebServlet("/specialServlet")
public class SpecialServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //向cookie中保存特殊字符
        //定义一个字符串空格
        String str = "12  34";
        //将字符串str存储到cookie中
//        Cookie cookie = new Cookie("demo",str);
​
        //对cookie中保存特殊字符解决方案
        // 对特殊字符进行一次编码
        //进行编码处理
        String encode = URLEncoder.encode(str, "utf-8");
        //将编码之后的数据存储到cookie中
        Cookie cookie = new Cookie("demo",encode);
​
        //cookie响应回浏览器
        response.addCookie(cookie);
    }
}

演示解码处理特殊字符,从cookie中获取数据代码:

@WebServlet("/decodeServlet")
public class DecodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("demo".equals(cookie.getName())){
                //获取到当前编码cookie
                String value = cookie.getValue();
                //对cookie进行解码
                String decode = URLDecoder.decode(value, "utf-8");
                //解码后值
                System.out.println("解码后值 = " + decode);
            }
        }
    }
}

注意:上述代码中只要保证编码和解码使用的编码表一致即可。

案例控制台效果图:

image.png

6、Cookie的存活时间&有效路径

在上面的案例中我们发现,将浏览器关闭。然后再打开一个新的浏览器,查看浏览器中的cookie,发现之前保存的cookie都消失了。我们再次登录发现之前设置的Cookie的到期时间为"浏览器会话结束"。

在浏览器设置中查看cookie.png

1530018396006.png

如果,我们想让这个cookie在浏览器关闭后能够在一段时间内都存在,比如记住用户名或密码一周。我们可以通过设置Cookie的最大存活时间来对这个Cookie进行设置。

【Cookie级别】

  • 会话级别 的Cookie:浏览器关闭后消失;
  • 持久化 的Cookie:浏览器关闭后能够保存一段时间;具体的时间可以使用相关API进行设置。

注意:如果没有设置Cookie的保存时间,那么Cookie就是会话级别的Cookie。

6.1 Cookie的最大存活时间(重要)

设置Cookie的最大存活时间的方法是:

方法 使用示例 说明
void setMaxAge(int seconds) userNameCookie.setMaxAge(60*60);设置存活时间是1小时。 设置Cookie的有效时间,单位是秒: 如果没有设置,cookie只会保存在浏览器的缓存中,浏览器关闭后cookie立即被删除。 如果设置有效时间,在时间范围内,cookie被写入到浏览器端,关闭浏览器下次访问仍可活的,直到过期。

【案例1】cookie的存活时间练习

需求:向cookie中存储数据,并设置cookie的存活时间为1周;

@WebServlet("/lifeServlet")
public class LifeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //新建life  cookie
        Cookie cookieLife = new Cookie("life","lifetime");
        //设置cookie存活时间 存活一周
        cookieLife.setMaxAge(60*60*24*7);
        response.addCookie(cookieLife);
    }
}
​

【注意事项】Cookie分类

  1. 会话级别的Cookie:浏览器关闭后销毁;
  2. 持久型的Cookie:设置最大存活时间,在存活的时间内,浏览器关闭,依然存在;

6.2 Cookie的有效路径(了解即可)

当我们访问某个商城网站的时候,由于商城网站会分为多个部分,例如包含用户模块、商品模块等。那么当我们访问用户模块我们希望请求携带的是用户的cookie,而不需要商品的cookie。但是按照我们之前的做法,只要访问服务器,那么所有的cookie都会被携带到服务器中,这并不符合我们开发中的需求。所以我们接下来要学习Cookie的有效路径,只要设置了Cookie的有效路径就可以解决访问哪个模块就携带哪个模块的Cookie。

说明:Cookie的有效路径指的是Cookie允许被访问的路径。设置路径,以及子路径都被允许访问。方法如下:

方法 使用示例 说明
void setPath(String path) cookie.setPath("/suoge") cookie.setPath("/") * cookie.setPath("/suoge"):指的是/suoge路径及其子路径可以访问这个cookie,例如:/suoge/a/b/c都可以访问; *cookie.setPath("/")指的是当前tomcat下的所有的web项目都可以访问这个cookie。如果代码中不进行有效路径的设置,那么就是默认的路径/,即tomcat下的所有的web项目都可以访问这个cookie;

【练习】Cookie的有效路径练习

需求:

  1. 在PathServlet中创建一个Cookie,设置路径为"/suoge/a/b";
  2. 新建一个PathTwoServlet,设置该servlet的访问路径:"/suoge/a/b/d";
  3. 新建一个PathThrServlet,设置该servlet的访问路径:/suoge/a";
  4. 分别在PathTwoServlet,PathThrServlet中获取cookie;

【设置cookie】

1.新建PathServlet,设置有效路径为/suoge/a/b

@WebServlet("/pathServlet")
public class PathServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //给cookie设置有效路径
        Cookie cookie = new Cookie("path","path");
        //给cookie设置生存时间 1小时
        cookie.setMaxAge(60*60);
        //给cookie设置有效路径
        cookie.setPath("/suoge/a/b");
        //cookie响应浏览器
        response.addCookie(cookie);
    }
}

浏览器效果:

image.png

【获取cookie】

新建PathTwoServlet,设置该servlet的访问路径:/suoge/a/b/d

@WebServlet("/suoge/a/b/d")
public class PathTwoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName());
            System.out.println(cookie.getValue());
        }
    }
}

【浏览器效果】

cookie的有效路径2.png

【idea控制台效果】

image.png

注意:浏览器中有什么cookie,那么访问对应的servlet就会在请求中携带什么cookie

新建PathThrServlet,设置该servlet的访问路径:/suoge/a

@WebServlet("/suoge/a")
public class PathThrServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName());
            System.out.println(cookie.getValue());
        }
    }
}

【浏览器效果】:

cookie有效路径3.png

【idea控制台效果】:

idea的控制台中并没有获取到之前的cookie中的path。

当给cookie设置有效路径"/suoge/a/b"后:

  1. 访问路径:/suoge 获取不到cookie;
  2. 访问路径:/suoge/a/b/d 能获取到cookie ;
  3. 访问路径:/suoge/a/b 能获取到cookie
  4. 访问路径:/suoge/a/d 获取不到cookie;

【结论】

  1. 当设置cookie的有效路径后,只能在有效径或其子路径下访问这个cookie;
  2. 如果不给cookie设置有效路径,默认的有效路径为"/" ,即在当前tomcat下的任意访问路径均可获取这个cookie;

7、Cookie删除(重要)

通常情况下,如果浏览器中的cookie存储的太多,我们要对浏览器中的cookie进行删除。删除浏览器中的cookie有以下几种方式:

1)在浏览器中手动删除cookie;

手动删除cookie.png

2)等待设置cookie的时间到,自动删除;

3)使用代码来删除,Servlet中没有专门提供API去删除cookie。我们可以通过以下方式删除cookie;

说明:思想:置换或者替换。

【操作步骤】

  1. 创建与要删除的cookie同名的cookie,将其值设置成"";
  2. 将这个cookie的最大存活时间设置成0;
  3. 设置这个cookie的有效路径(与原cookie的有效路径一致);
  4. 将这个新的cookie响应给浏览器,置换原来的cookie;

【参考代码】

1. First generate cookie code reference: CreServlet.java

@WebServlet("/creServlet")
public class CreServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建cookie
        Cookie cookie = new Cookie("cre","hello");
        //设置cookie存活时间
        cookie.setMaxAge(60*60);
         //设置路径
        cookie.setPath("/");
        response.addCookie(cookie);
    }
}

Effect:

1537160162874.png

Operation reference code for cookie deletion: DeleteServlet.java

@WebServlet("/delServlet")
public class DeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //删除cookie
        //新建相同名称cookie
        Cookie cookie = new Cookie("cre","");
        //保证cookie有效路径一致
        //设置cookie存活时间  0
        cookie.setMaxAge(0);
        //浏览器接受新的cookie,置换之前cookie,完成cookie删除
        response.addCookie(cookie);
    }
}

Effect:

Close your browser and view cookies in your browser.

找不到cookie.png

[Summary] Several ways to make cookies disappear

  1. For cookies that do not have a maximum survival time set, the cookie disappears immediately after the browser is closed;
  2. Use your browser to manually clear cookies;
  3. the browser disables cookies;
  4. Delete cookies through Servlet technology;
  5. Wait for the set time to expire and disappear by itself.

8. Summary of cookies

  • Cookie: the technology for the server to store data on the client (browser);

  • Cookie classification:

    • Session level: Cookies without a maximum survival time will disappear after the browser is closed;
    • Persistence level: The maximum survival time is set, and it will not disappear after the browser is closed;
  • Cookie basic API:

    • Create Cookie: Cookie c = new Cookie("name","zhangsan");
    • Get name value: c.getName();
    • Get value: c.getValue();
    • Respond the cookie to the browser: response.add(c);
    • Get all cookies: Cookie[] cookies = request.getCookies();
  • Cookie valid time:

    • setMaxAge(int seconds): Set the survival time of the cookie in seconds;
  • Valid paths for cookies:

    • setPath(String path): Set the effective path of the cookie, which is valid under this path and its subpaths;
  • Cookie deletion:

    • client:

      • Session-level cookies, which disappear immediately after closing the browser;
      • disable cookies;
      • Clear cookies manually;
    • Server:

      • Through Servlet, replace the original cookie;
  • Advantages and disadvantages of cookies:

    • Advantage:

      • Provide a basis for the server to identify users;
      • Reduced the pressure on server-side data storage;
    • Disadvantages:

      • Data storage on the client side is insecure;
      • The size of stored data is limited, and the maximum data stored in a cookie is 4K;

Guess you like

Origin juejin.im/post/7240465997002195003
top