javaweb22/4/4
Cookie与Session
https://www.cnblogs.com/l199616j/p/11195667.html
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话。常用的会话跟踪技术是Cookie与Session。
Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身份。
常见场景:网站登陆之后,第二次就不用登录了
用Cookie保存用户上一次访问的时间
1.cookie的Servlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决请求响应中的中文乱码问题
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//Cookie是在客户端保存,所以req.getCookies
Cookie[] cookies = req.getCookies();//返回数组,说明Cookie可能有多个
if (cookies!=null){
out.write("您上次访问的时间是:");
for (int i = 0; i <cookies.length ; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("wenping")){
//获取cookie中的值
long l = Long.parseLong(cookie.getValue());
Date date = new Date(l);
out.write(date.toLocaleString());
}
}
}else{
out.write("这是您第一次访问本网站");
}
//服务器端给客户端响应一个Cookie
Cookie cookie = new Cookie("wenping",System.currentTimeMillis()+"");
resp.addCookie(cookie);
cookie.setMaxAge(24*60*60);//设置cookie的有效期
}
2.注册Servlet
3.结果

Session
Session是另一种记录客户状态的机制,不同的是Cookie保存在客户端浏览器中,而Session保存在服务器上。客户端浏览器访问服务器的时候,
服务器把客户端信息以某种形式记录在服务器上。这就是Session。服务器会给每个用户(浏览器)创建一个Session对象,一个浏览器一个Session,例如,用户登录后可访问整个网站
使用Session
1.创建Session
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
//得到Session
HttpSession session = req.getSession();
//在Session中存东西
session.setAttribute("name",new Person("文萍",20));
//获取Session的ID
String sessionId = session.getId();//sessionId会放入Cookie中
//判断Session是不是新创建
if (session.isNew()){
resp.getWriter().write("新创建的SessionId为:"+sessionId);
}else{
resp.getWriter().write("Session在服务器中已经存在了,SessionId为:"+sessionId);
}
}
2.获取SessionID
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
//得到Session
HttpSession session = req.getSession();
//session也可以实现数据共享,content也可以实现
Person name = (Person) session.getAttribute("name");
System.out.println(name);
}
3.手动注销Session
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
//手动注销session
session.removeAttribute("name");
session.invalidate();
}
4.在web.xml中设置自动注销Session
<session-config>
<session-timeout>1</session-timeout>
</session-config>
Cookie和Session的区别
两者最大的区别在于生存周期,一个是IE启动到IE关闭.(浏览器页面一关 ,session就消失了),一个是预先设置的生存周期,或永久的保存于本地的文件。(cookie)
1.cookie数据存放在客户的浏览器上,session数据放在服务器上.
2.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗考虑到安全应当使用session。
3.设置cookie时间可以使cookie过期。但是使用session-destory(),我们将会销毁会话。
4.session会在一定时间内保存在服务器上。当访问增多,会比较占用你服务器的性能考虑到减轻服务器性能方面,应当使用cookie。
5.单个cookie保存的数据不能超过4K,很多浏览器都限制一个站点最多保存20个cookie。(Session对象没有对存储的数据量的限制,其中可以保存更为复杂的数据类型)
6.记住密码功能就是使用永久cookie写在客户端电脑,下次登录时,自动将cookie信息附加发送给服务端。
浙公网安备 33010602011771号