Cookie、Session
1. 会话
会话:用户打开了浏览器,打开了多个链接,然后关闭浏览器,这就是一次会话。
有状态会话:
浏览器怎么证明你来过。
服务端和客户端
1.服务端给客户端一个信物,客户端拿着信物来,服务端就能识别(cookie);
2.服务器登记你来过,下才来就能识别你(session);
2.保存会话的两种技术
1.cookie
客服端技术请求和响应
解决中文乱码问题
resp.setContentType("text/html");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
源码
package com.wang.servlet;
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 javax.xml.crypto.Data;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
public class cookie extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//把时间当成信件
resp.setContentType("text/html");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//服务器从客户端获取cookie
Cookie[] cookies = req.getCookies();
//判断cookie是否存在
if(cookies!=null)
{
out.print("你上一次访问的时间为");
for(int i=0;i<cookies.length;i++)
{
Cookie cookie=cookies[i];
if(cookie.getName().equals("lastLoginTime"))
{
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
}
else
{
out.print("添加cookie");
}
//服务端给客户端cookie
Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
resp.addCookie(cookie);
}
}
2.session
服务端技术,浏览器记录保存会话,把数据和信息放在session中
比如一个网站登录,你上次登录过,下次就不用登录了
3.Cookie
1.从请求中拿到Cookie信息
2.服务器给客户端一个Cookie
Cookie[] cookies = req.getCookies();//获取Cookie
cookie.getName()//获得Cookiekey
cookie.getValue()//获得Cookie的值
Cookie cookie = new Cookie//新建Cookie
cookie.setMaxAge(24*60*60);//设置有效期
resp.addCookie(cookie);//服务器响应给客户端一个Cookie
一个Cookie保存一个信息
Cookie上限大小为4kb
4.session
1.Session将用户交互信息保存在了服务器端,ID是客户端第一次访问服务器的时候生成的,而且每个客户端是唯一的,这样就不用Cookie每次请求响应,ID就能在服务器取得所有的用户交互信息
2.服务端记录你来过,session起登记作用(个人理解)
3.例如,用户登陆了一个网站,这个用户就可以访问这个网站的整个内容
请求设置session源码
package com.wang.servlet;
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 session extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//请求Session
HttpSession session = req.getSession();
//设置Session的值
session.setAttribute("name","jinnice");
//得到Session的id
String id = session.getId();
if(session.isNew())
{
resp.getWriter().write("session是新的");
}
else
{
resp.getWriter().write("session已经创建"+id);
}
}
}
读取session源码
package com.wang.servlet;
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 session02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//请求Session
HttpSession session = req.getSession();
//得到session中的值
String name = (String) session.getAttribute("name");
System.out.println(name);
}
}
session手动注销
package com.wang.servlet;
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 session03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("name");
session.invalidate();
}
}
按时自动注销
<session-config>
<session-timeout>1</session-timeout>
</session-config>
Cookie和Session的区别
Session储存在服务器端,相当于sessionid是给用户的一把钥匙,可以打开服务器存储的数据,session存在时间短
Cookie保存在浏览器端(可以存多个)
session对象由服务器创建
标签:Javaweb,resp,cookiea,session,Cookie,import,servlet,javax From: https://www.cnblogs.com/jinnice/p/16634897.html