session应用及问题
session和cookie的区别
- cookie是把用户的数据写给浏览器,浏览器保存(可以保存多个);
- session是把用户的数据写到用户独占的session中,服务器端保存(保存重要的信息,避免服务器的资源浪费);
- session由服务器创建
简单例子
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");
//获取session
HttpSession session = req.getSession();
//给session存值
session.setAttribute("name", "狂神");
//获取session的id
String id = session.getId();
if (session.isNew()) {
resp.getWriter().write("session创建成功,id:" + id);
} else {
resp.getWriter().write("session已经在服务器中存在了,id:" + id);
}
resp.getWriter().flush();
//session创建的时候做了什么
// Cookie cookie = new Cookie("JSESSIONID", id);
// resp.addCookie(cookie);
}
session存储对象
//创建实体类
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return 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-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//获取session
HttpSession session = req.getSession();
//给session存值
session.setAttribute("person", new Person("zyy", 18));
//获取session的id
String id = session.getId();
if (session.isNew()) {
resp.getWriter().write("session创建成功,id:" + id);
} else {
resp.getWriter().write("session已经在服务器中存在了,id:" + id);
}
resp.getWriter().flush();
//session创建的时候做了什么
// Cookie cookie = new Cookie("JSESSIONID", id);
// resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
//跨页面调用session
public class SessionDemo02 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");
//获取session
HttpSession session = req.getSession();
//给session存值
Person person = (Person) session.getAttribute("person");
System.out.println(person.getName());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
//销毁session
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");
//获取session
HttpSession session = req.getSession();
//手动注销session
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
本文来自博客园,作者:Cn_FallTime,转载请注明原文链接:https://www.cnblogs.com/CnFallTime/p/15966712.html