目录
会话
会话:用户打开一个浏览器,点击链接/访问多个web资源,关闭浏览器,这个过程可以称之为会话
有状态会话:
服务端给客户端一个cookie,客户端访问服务端,带上cookie就可以了
服务器登记session
保存会话的两种技术
cookie
客户端技术(响应,请求)
1.从请求中拿到cookie信息
2.服务器响应给客户端cookie
配置pom.xml
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
</dependency>
</dependencies>
创建类
//保存用户上一次访问的时间
public class cookieDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//服务器告诉你你来的时间,你下次带来我就知道你来了
//解决中文乱码
req.setCharacterEncoding("utf-16");//utf-8如果乱码,改为utf-16
resp.setCharacterEncoding("utf-16");
PrintWriter out=resp.getWriter();
//Cookie服务器端从客户端获取
Cookie[] cookies = req.getCookies();
//判断cookies是否存在
if (cookies!=null) {
//如果存在,取出数据,遍历数组
out.write("上次访问的时间是");
for (int i = 0; i < cookies.length; i++) {
Cookie cookies1=cookies[i];
//获取cookies的名字
if (cookies1.getName().equals("lastLoginTime")) {
//获取cookie的值
long l = Long.parseLong(cookies1.getValue());
Date date=new Date(l);
out.write(date.toLocaleString());
}
}
}else{
out.write("这是我妹第一次称为小可爱");
}
//服务器给客户端一个cookie
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
//设置cookie有效时间(一天),如果不设置,关闭浏览器后再次打开访问cookie为空
cookie.setMaxAge(24*60*60);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
配置web.xml
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
</dependency>
</dependencies>
添加tomcat(Edit configrations)
访问网站
一个网站cookie是否存在上限
- 一个cookie只能保存一个信息;
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
- cookie大小有限制4KB;
- 300个cookie浏览器上限
删除cookie
- 不设置有效期,关闭浏览器,自动失效
- 设置有效期时间为0
创建类
public class cookieDemox extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie=new Cookie("lastLoginTime", System.currentTimeMillis()+"");
cookie.setMaxAge(0);//设置为0立马失效
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
配置web.xml
<servlet>
<servlet-name>cookie1</servlet-name>
<servlet-class>com.yl520.cookieDemox</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookie1</servlet-name>
<url-pattern>/cookie1</url-pattern>
</servlet-mapping>
验证,先访问http://localhost:8080/javaweb_session_cookie_war/cookie,让浏览器存入cookie,再访问http://localhost:8080/javaweb_session_cookie_war/cookie1,删除cookie,再访问http://localhost:8080/javaweb_session_cookie_war/cookie验证cookie是否删除(如果为空则删除成功)
遇到乱码问题(编码解码)
编码
Cookie cookie=new Cookie("name", URLEncoder.encode("我妹最漂亮","utf-8"));
解码
out.write(URLDecoder.decode(cookie.getValue(), "utf-8"));
Session(重点)
服务器技术,利用这个技术可以保存用户的会话信息,可以把信息或者数据放在Session中
概念:
- 服务器会给每一个用户(浏览器)创建一个Session对象
- 一个session独占一个浏览器,只要浏览器没有关闭,这个Session就存在。
- 用户登录后,整个网站它都能访问-》保存用户信息,保存购物车信息
session存字符串
创建类创建session
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-16");
resp.setCharacterEncoding("utf-16");
resp.setContentType("text/html;charset=urf-8");
//得到Session
HttpSession session = req.getSession();
//给session中存东西
session.setAttribute("name", "我妹最可爱");
//获取Session的id
String id=session.getId();
//判断是不是新的session
if (session.isNew()) {
resp.getWriter().write("session创建成功,sessionid为:"+id);
}else{
resp.getWriter().write("session已存在,id:"+id);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
创建类获取session
public class sessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-16");
resp.setCharacterEncoding("utf-16");
resp.setContentType("text/html;charset=utf-8");
//得到Session
HttpSession session=req.getSession();
String name = (String) session.getAttribute("name");
System.out.println(name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
配置web.xml
<servlet>
<servlet-name>session</servlet-name>
<servlet-class>com.yl520.SessionDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>session</servlet-name>
<url-pattern>/session</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>session2</servlet-name>
<servlet-class>com.yl520.sessionDemo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>session2</servlet-name>
<url-pattern>/session2</url-pattern>
</servlet-mapping>
session存对象
创建person对象
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建类创建session
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-16");
resp.setCharacterEncoding("utf-16");
resp.setContentType("text/html;charset=urf-8");
//得到Session
HttpSession session = req.getSession();
//给session中存东西
session.setAttribute("name", new Person("我妹最可爱",18));
//获取Session的id
String id=session.getId();
//判断是不是新的session
if (session.isNew()) {
resp.getWriter().write("session创建成功,sessionid为:"+id);
}else{
resp.getWriter().write("session已存在,id:"+id);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
创建类得到创建的session
public class sessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-16");
resp.setCharacterEncoding("utf-16");
resp.setContentType("text/html;charset=utf-8");
//得到Session
HttpSession session=req.getSession();
Person person = (Person) session.getAttribute("name");
System.out.println(person.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
session注销(手动注销)
创建类
public class sessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("name");
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
配置web.xml文件
<servlet>
<servlet-name>session3</servlet-name>
<servlet-class>com.yl520.sessionDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>session3</servlet-name>
<url-pattern>/session3</url-pattern>
</servlet-mapping>
session注销(自动注销)
配置web.xml文件
<session-config>
<!-- session默认失效时间,1分钟-->
<session-timeout>1</session-timeout>
</session-config>
Session和cookie的区别
- cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
- session把用户的数据写到用户独占session中,服务器端保存(保存重要信息,减少服务器资源浪费)
- session对象有服务器创建
使用场景:
- 保存一个登录用户的信息;
- 购物车信息
- 再整个网站中经常会使用的数据,我们将他保存到session中