会话:用户打开一个浏览器,点击了许多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话;
有状态会话:举个例子,一个学生来过教室,下次再来教室,我们会知道这个同学曾经来过,称之为有状态会话;
一个网站怎么证明你来过?
客户端 服务端
服务端给客户端一个信件,客户端下次访问服务器带上信件即可:cookie
服务端登记你来过了,下次你来的时候我匹配你:session
cookie
session
常见场景:网站登陆过后,下次不用重新登录,第二次访问直接就上去了
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决中文乱码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//cookie,服务器从客户端获取
Cookie[] cookies = req.getCookies();//这里返回数组,说明cookie可能存在多个
//判断cookie是否存在
if (cookies != null) {
out.write("您上一次访问的时间是:");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
//获取cookie的名字
if (cookie.getName().equals("lastLoginTime")) {
//获取cookie的值
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
} else {
out.write("这是您第一次登陆本站");
}
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
//设置cookie的有效期
cookie.setMaxAge(24*60*60);
resp.addCookie(cookie);
}
1.从请求中拿到cookie信息
2.服务器响应给客户端cookie
Cookie[] cookies = req.getCookies();//获得cookie
cookie.getName(); //获得cookie中的key
cookie.getValue(); //获得cookie中的vlaue
new Cookie("lastLoginTime", System.currentTimeMillis()+""); //新建一个cookie
cookie.setMaxAge(24*60*60); //设置cookie的有效期
resp.addCookie(cookie); //响应给客户端一个cookie
几个细节问题
删除cookie
关于编码解码问题(针对cookie中value为中文,亦可对比解决工作中遇到的乱码问题)
URLEncoder.encode("哈哈哈","utf-8")//编码
URLDecoder.decode(cookie.getValue(),"UTF-8")//解码
什么是Session?
Session和Cookie的区别
使用场景:
使用Session的举例:
创存session
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");
//得到Session
HttpSession session = req.getSession();
//给Session中存东西
session.setAttribute("person",new Person("无名",24,"男"));
//获取Session的ID
String sessionId = session.getId();
//判断Session是不是新创建的
if (session.isNew()){
resp.getWriter().write("session创建成功,id为:" + sessionId);
}else{
resp.getWriter().write("session已经在服务器中存在了,id为:" +sessionId);
}
}
Person类
public class Person {
private String name;
private int age;
private String sex;
public Person() {
}
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person{" +
"name=‘" + name + ‘\‘‘ +
", age=" + age +
", sex=‘" + sex + ‘\‘‘ +
‘}‘;
}
}
从session取值
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");
//得到Session
HttpSession session = req.getSession();
Person person = (Person) session.getAttribute("person");
System.out.println(person);
}
清除session
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("person");
session.invalidate();
}
定时自动刷新(web.xml配置)
<session-config>
<!--设置为15分钟失效 -->
<session-timeout>15</session-timeout>
</session-config>
其他web.xml配置就不说了
原文:https://www.cnblogs.com/meeseekjava/p/13457255.html