ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
![](https://box.kancloud.cn/22518a9203b6c1bdfd42b4ad2aa05843_550x305.png) User .java ~~~ package zyw.bean; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class User implements HttpSessionBindingListener { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) { //绑定 System.out.println("对象被绑定了"); } @Override public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) { //解绑 System.out.println("对象被解除绑定了"); } } ~~~ TestBindingServlet .java ~~~ package zyw.servlet; import zyw.bean.User; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet(name = "TestBindingServlet",urlPatterns = "/testbind") public class TestBindingServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); User user=new User(); user.setId(1); user.setName("xiaoming"); //绑定对象 session.setAttribute("user",user); //解除绑定 session.removeAttribute("user"); } } ~~~ ![](https://box.kancloud.cn/57556bad99d0f28d09f28fc202b6d33c_132x54.png)