JavaWeb学习之Cookie、Session(保持会话的两种技术)
一、Cookie
客户端技术(响应,请求)
1.1 cookie的一些细节知识
- 一个cookie只能保存一个信息
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
- Cookie大小有限制4kb
- 300个cookie浏览器上限
1.2 删除cookie
- 不设置有效期,关闭浏览器,自动失效
- 设置有效期为0。例如:cookie.setMaxAge(0);
1.3 Cookie的小练习
- java代码(创建到发送到浏览器)
获取cookie(可能是多个):Cookie[] cookies = req.getCookies(); 获取cookie的名字:cookie.getName() 获取cookie的值:cookie.getValue() 服务器给客户端响应一个cookie: Cookie cookie = new Cookie(“lastLoginTime”,System.currentTimeMillis()+""); resp.addCookie(cookie);
package com.jzq.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 CookieDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter printWriter = resp.getWriter();
Cookie[] cookies = req.getCookies();
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if(cookie.getName().equals("name")) {
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
printWriter.write(date.toLocaleString());
}else {
printWriter.write("这是你第一次登录!");
}
}
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
- java代码2(清除cookie)
设置cookie有效期:cookie.setMaxAge(0); 注意!!! 同名返回即可清除现有cookie
package com.jzq.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;
public class CookieDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
cookie.setMaxAge(0);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
二、Session
服务器技术,利用这个技术,可以保存用户的会话信息?可以把信息或者数据放在Session中 (常见应用) 网站登录以后,下次不用再登录了,第二次访问直接就上去了!
1.1 什么是Session?
① 服务器会给每一个用户(浏览器)创建一个Session对象; ② 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在; ③ 用户登录之后,整个网站它可以访问>>>存一些用户信息和购物车信息
1.2 Session和Cookie的区别:
① Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个); ② Session把用户的数据写到用户独占Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费); ③ Session对象由服务创建;
1.3 Session的小练习
- java代码01
① 获取session :HttpSession httpSession = req.getSession(); ② session存数据:httpSession.setAttribute(“name”,new Person(“一山一水”, 18)); ③ 获取session的id:String sessionId = httpSession.getId(); ④ 判断session是不是创建: httpSession.isNew();
package com.jzq.Session;
import com.jzq.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 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");
HttpSession httpSession = req.getSession();
httpSession.setAttribute("name",new Person("一山一水", 18));
String sessionId = httpSession.getId();
if(httpSession.isNew()) {
resp.getWriter().write("session创建成功,ID:" + sessionId);
}else {
resp.getWriter().write("session以及在服务器中存在了,ID:" + sessionId);
}
}
}
- java代码02
① 移除session的数据: httpSession.removeAttribute(“name”); ② 移除现有 session: httpSession.invalidate();
package com.jzq.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 SessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession httpSession = req.getSession();
httpSession.removeAttribute("name");
httpSession.invalidate();
}
}
- java代码03
①获取session的数据:Person person = (Person) httpSession.getAttribute(“name”);
package com.jzq.Session;
import com.jzq.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 SessionDemo03 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");
HttpSession httpSession = req.getSession();
Person person = (Person) httpSession.getAttribute("name");
System.out.println(person);
}
}
- 设置xml,session的时常
<!-- 设置Session默认的失效时间-->
<session-config>
<!-- 设置1分钟Session失效-->
<session-timeout>1</session-timeout>
</session-config>
|