且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HttpSession - 如何获取session.setAttribute?

更新时间:2023-11-28 11:16:46

HttpSessionListener 接口用于监视在应用程序服务器上创建和销毁会话的时间。 HttpSessionEvent.getSession()返回一个新创建或销毁的会话(取决于它是否被 sessionCreated /调用)分别 sessionDestroyed

The HttpSessionListener interface is used to monitor when sessions are created and destroyed on the application server. The HttpSessionEvent.getSession() returns you a session that is newly created or destroyed (depending if it's called by sessionCreated/sessionDestroyed respectively).

如果您想要现有会话,则必须从请求中获取会话。

If you want an existing session, you will have to get the session from the request.

HttpSession session = request.getSession(true).
String username = (String)session.getAttribute("username");