JavaWeb 之 Cookie 和 Session 技术 (四)
[](()2.4.2、cookie 的获取
? 在服务器端只提供了一个 getCookies()
的方法用来获取客户端回传的所有 cookie 组成的一个数组,如果需要获取单个 cookie ,则需要通过遍历,getName()
获取 Cookie 的名称,getValue()
获取 Cookie 的值
获取 cookie 的名称:
cookie.getName()
获取 cookie 的值:
cookie.getValue()
获取 cookie 数组:
req.getCookies()
// 获取 Cookie 数组
Cookie[] cookies = request.getCookies();
// 判断数组是否为空
if(cookies != null && cookies.length >0){
// 遍历 Cookie 数组:方式一
for(Cookie cookie : cookies){
System.out.println(cookie.getName());
System.out.println(cookie.getValue());
}
}
[](()2.4.3、cookie 设置到期时间
除了 Cookie 的名称和内容外,我们还需要关心一个信息,到期时间。
到期时间:到期时间用来指定该 cookie 何时失效,默认为当前浏览器关闭即失效
我们可以手动设定 cookie 的有效时间(通过到期时间计算)
通过
setMaxAge(int time)
方法设定 cookie 的最大有效时间,以秒为单位。
[](()2.4.4、到期时间的取值
负整数
若为负数,表示不存储该 cookie
cookie 的 maxAge 属性的默认值就是 -1 ,表示只在浏览器内存中存活,一旦关闭浏览器窗口,那么 cookie 就会消失
正整数
若大于 0 的整数,表示存储的秒数
表示 cookie 对象可存货指定的秒数。当生命大于 0 时,浏览器会把 Cookie 保存到硬盘上,就算关闭浏览器,就算重启客户端电脑,cookie 也会存活相应的时间
零
若为 0,表示删除该 cookie
也就是说,如果原来的浏览器已经保存了这个 Cookie,那么可以通过 Cookie 的 setMaxAge(0) 来删除这个 Cookie
Cookie cookie = new Cookie("uname","zhangsan");
cookie.setMaxAge(30); //表示存活 30s
resp.addCookie(cookie);
[](()2.4.5、cookie 编码
Cookie 保存在当前浏览器中
在一般的站点中常常有记住用户名这样一个操作,该操作只是将信息保存在本机上,换电脑以后这些信息就失效了,而且 cookie 还不能跨浏览器
Cookie 存中文问题
Cookie 中不能出现中文,如果有中文
通过
URLEncoder.encode()
来编码,获取时通过
URLDecoder.decode()
来进行解码
String name = "姓名";
String value = "张三";
// 通过 URLEncoder.encode() 来进行编码
name = URLEncoder.encode(name);
value = URLEncoder.encode(value);
// 创建 Cookie 对象
cookie cookie = new Cookie(name,value);
// 发送 Cookie 对象
resp.addCookie(cookie);
// 获取时通过 URLDecoder.decode()来进行解码
URLDecoder.decode(cookie.getName());
URLDecoder.decode(cookie.getValue());
同名 Cookie 问题
如果服务器端发送重复的 Cookie,那么会覆盖原有的 Cookie
浏览器存放 Cookie 的数量
一个 Cookie 只能保存一个信息
一个 web 站点可以给浏览器发送多个 cookie,最多存放 20 个 cookie
Cookie 大小有限制 4kb
[](()3、服务器端会话管理
==================================== Java 开源项目【ali1024.coding.net/public/P7/Java/git】 ========================================
[](()3.1、Httpsession
什么是 Httpsession?
服务器会给每一个用户(浏览器)创建一个 Httpsession 对象
一个 Seesion 独占一个浏览器,只要浏览器没有关闭,这个 Session 就存在
服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象中
[](()3.2、HttpSession 常用 API

[](()3.3、Httpsession 的获取
获取 HttpSession 对象
req.getSession()
获取 Session 对象
使用 HttpSession 对象
session.setAttribute(String name,object)
设置 session 域对象session.removeAttribute(String name)
移除指定名称的 session 域对象
[](()3.4、session 域对象
? Session 用来表示一次会话,在一次会话中数据是可以共享的,这时 session 作为域对象存在,可以通过setAttribute(name,value)
方法向域对象中添加数据,通过getAttribute(name)
从域对象中获取数据,通过removeAttribute(name)
从域对象中移除数据
session.getId()
获取 session 的 IDsession.isNew()
判断是否是新的 session 对象
// 得到 Session
HttpSession session = req.getSession();
// 给 Session 中存东西
session.setAttribute("name","狂神说");
// 获取 Session 的 ID
String id = session.getId();
// 判断 Session 是不是新创建的
session.isNew();
数据存储在 session 域对象中,当 session 对象不存在了,或者是两个不同的 session 对象时,数据也就不能共享了。
[](()3.5、例子
[](()3.5.1、例一
我们给 session 中存入一个字符串,并在另一个 session 中拿到数据
SessionDemo01.java
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
// 得到 Session
HttpSession session = req.getSession();
// 给 Session 中存东西
session.setAttribute("name","狂神说");
// 获取 Session 的 ID
String id = session.getId();
// 判断 Session 是不是新创建的
if(session.isNew()){
resp.getWriter().write("session 创建成功,ID:" + id);
}else {
resp.getWriter().write("session 已经在服务器中存在了,ID:" + id);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
在 web.xml 中注册 servlet 映射为 /s1
<servlet>
<servlet-name>SessionDemo01</servlet-name>
<servlet-class>com.kuang.servlet.SessionDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionDemo01</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
测试启动

当浏览器打开时,会自动创建一个 session 对象,此时 session 已经存在
我们在另一个 session 对象中拿到第一个 session 中存入的数据
SessionDemo02.java
public class SessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》开源 ");
// 得到 Session
HttpSession session = req.getSession();
// 获取第一个 session 中存入的数据 name
String name = (String) session.getAttribute("name");
resp.getWriter().write(name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
在 web.xml 中注册 servlet 映射为 /s2
<servlet>
<servlet-name>SessionDemo02</servlet-name>
<servlet-class>com.kuang.servlet.SessionDemo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionDemo02</servlet-name>
<url-pattern>/s2</url-pattern>
</servlet-mapping>
访问测试



[](()3.5.2、例二
我们给 session 中存入一个对象,并在另一个 session 中拿到数据
我们新建包 pojo
在包 pojo 下新建 Person 类
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
Docker 步步实践
目录文档:


①Docker 简介
②基本概念
③安装 Docker

④使用镜像:

⑤操作容器:

⑥访问仓库:

⑦数据管理:

⑧使用网络:

⑨高级网络配置:

⑩安全:

?底层实现:

?其他项目:

评论