一:Cookie
1.1 信件/凭证:
??????当用户通过浏览器访问web网页,即请求web资源时,用来证明当前浏览器访问过目标网站的信件,对应响应信件内容或其他资源。
1.2 实现:
可以对要访问的页面的添加Cookie,返回到浏览器保存: resp.addCookie(new Cookie(name,vale))
1.3 请求过程
当浏览器访问时会携带Cookie,如果携带的Cookie符合则对应设置的的值取出对应响应内容: ??????1.获取Cookie: req.getCookie() ??????2.获取Cookie的key: cookie.getName() ??????3.获取Cookie的value:cookie.getValue()
1.4 设置有效期
设置器浏览器保存的Cookie有效期时间: setMaxAge(time);单位s
1.5 删除cookie
1.不设置Cookie的有效期,浏览器关闭后自动销毁了 2.设置Cookie的有效期到期自动删除(失效)
1.6 解决乱码
1.resp:设置响应格式(例"text/html")和设置响应编码(例"utf-8") ???????resp.setCharacterEncoding(“utf-8”) ???????resp.setContentType(“text/html”)
2.req:设置请求编码(例"utf-8")和建立保存至浏览器时编码UrlEncoder与获取请求时解码UrlDecoder
1.7 源码
package com.zk.cookie;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
public class CookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
Cookie[] cookies = req.getCookies();
PrintWriter writer = resp.getWriter();
if (cookies==null){
writer.print("第一次访问");
}else {
boolean judge=false;
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("time")){
judge=true;
writer.print("上次访问时间为:" + (new Date(Long.parseLong(cookie.getValue()))).toLocaleString());
System.out.println("上次访问时间"+(new Date(Long.parseLong(cookie.getValue()))).toLocaleString());
}
}
if (!judge){
writer.print("第一次访问");
}
}
Cookie cookie = new Cookie("time", System.currentTimeMillis() + "");
cookie.setMaxAge(24*60*60);
System.out.println("当前访问时间"+(new Date(Long.parseLong(cookie.getValue()))).toLocaleString());
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
1.8 结果
1.第一次访问: 2.浏览浏览器的Cookie: 3.控制台输出
二:Session
2.1 Session理解
??????1.Session是与客户端(浏览器)绑定的,在不同的客户端中其不同,客户端退出就删除了,重启又会重新生成 ??????2.Session在客户端打开网页就有了,封装在Cookie中的 ??????3.其Session中的属性可以是自定义类,设置的属性也只与当前的浏览器关联,其他浏览器访问是没有
2.2 注销Session
1.退出浏览器:退出浏览器后Session就销毁了 2.调用session.invalidate()手动注销 3.在web.xml配置session-config中的timeout,其单位为分
<session-config>
<session-timeout>1</session-timeout>
</session-config>
2.3 Session测试
自定义类
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "{姓名为:"+this.name+" 年龄为:"+this.age+"}";
}
}
测试类
package com.zk.session;
import com.zk.pojo.Person;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
public class SessionServlet 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");
HttpSession session = req.getSession();
session.setAttribute("id",new Person("张三",22));
String sessionId = session.getId();
if (session.isNew()){
resp.getWriter().write("Session创建成功:"+sessionId);
}else {
resp.getWriter().write("Session已经创建了:"+sessionId);
}
resp.getWriter().write("<br>");
Cookie[] cookies = req.getCookies();
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("JSESSIONID")){
resp.getWriter().write("Cookie封装的Session信息:"+cookies[i].getValue());
}
}
resp.getWriter().write("<br>");
resp.getWriter().write("Session属性值为:"+session.getAttribute("id").toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.4 获取Session的属性
package com.zk.session;
import com.zk.pojo.Person;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionGetAttribute extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
HttpSession session = req.getSession();
Person person = (Person) session.getAttribute("id");
resp.getWriter().write("属性值为:"+person.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.5 手动注销Session
package com.zk.session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionInvalidate extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.6 运行结果
1.打开谷歌浏览器 ? ? ? 2.打开360浏览器
不同的浏览器访问其Session的ID不同
? ? ? 3.在谷歌浏览器中访问SessionServlet后不关闭浏览器新建标签页访问SessionServlet
在访问后不关闭浏览器继续重新访问和关闭当前页新建标签页访问其Session都不变
? ? ? 4.访问SessionServlet添加属性
? ? ? 5.访问SessionGetAttribute获取属性 ? ? ? 6.访问SessionInvalidate手动注销后不关闭浏览器重新访问
其Session也变了
? ? ? 7.在谷歌浏览器中访问SessionServlet添加属性后在360浏览器访问SessionGetAttribute获取属性
在360浏览器中报错,Session只与当前浏览器绑定
|