Java 我可以这样实现HttpSessionListener吗?

Java 我可以这样实现HttpSessionListener吗?,java,servlets,Java,Servlets,我试图在我的Javaservlet中跟踪有效的用户ID,我可以这样实现HttpSessionListener吗 public class my_Servlet extends HttpServlet implements HttpSessionListener { String User_Id; static Vector<String> Valid_User_Id_Vector=new Vector<String>(); private static int

我试图在我的Javaservlet中跟踪有效的用户ID,我可以这样实现HttpSessionListener吗

public class my_Servlet extends HttpServlet implements HttpSessionListener
{
  String User_Id;
  static Vector<String> Valid_User_Id_Vector=new Vector<String>();
  private static int activeSessions=0;

  public void sessionCreated(HttpSessionEvent se)
  {
// associate User_Id with session Id;
// add User_Id to Valid_User_Id_Vector
    Out(" sessionCreated : "+se.getSession().getId());
    activeSessions++;
  }

  public void sessionDestroyed(HttpSessionEvent se)
  {
    if (activeSessions>0)
    {
// remove User_Id from Valid_User_Id_Vector by identifing it's session Id
      Out(" sessionDestroyed : "+se.getSession().getId());
      activeSessions--;
    }
  }

  public static int getActiveSessions()
  {
    return activeSessions;
  }

  public void init(ServletConfig config) throws ServletException
  {
  }

  public void destroy()
  {

  }

  protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
  {
    User_Id=request.getParameter("User_Id");
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    processRequest(request, response);
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    processRequest(request, response);
  }

  public String getServletInfo()
  {
    return "Short description";
  }
}
公共类my_Servlet扩展了HttpServlet,实现了HttpSessionListener { 字符串用户标识; 静态向量有效用户Id向量=新向量(); 私有静态int-activeSessions=0; 已创建公共无效会话(HttpSessionEvent se) { //将用户Id与会话Id关联; //将用户Id添加到有效的用户Id向量 Out(“sessionCreated:+se.getSession().getId()); activeSessions++; } public void sessionDestroyed(HttpSessionEvent se) { 如果(activeSessions>0) { //通过标识会话Id,从有效的用户Id向量中删除用户Id Out(“sessionDestroyed:+se.getSession().getId()); 活动会话--; } } 公共静态int getActiveSessions() { 返回活动会话; } public void init(ServletConfig config)抛出ServletException { } 公共空间销毁() { } 受保护的void processRequest(HttpServletRequest请求,HttpServletResponse响应)引发ServletException,IOException { User_Id=request.getParameter(“User_Id”); } 受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException { processRequest(请求、响应); } 受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException { processRequest(请求、响应); } 公共字符串getServletInfo() { 返回“简短描述”; } }
如何在会话结束时通知侦听器?我试图绕过“/WEB-INF.WEB.xml”所有这些,这可行吗?或者它有意义吗?

这不会绕过
/WEB-INF/WEB.xml
。此外,您将得到该类的2个实例,而不是1个同时执行这两个函数。我建议你把这个向量放在两个独立的类中

在servlet中,您可以通过。在侦听器中,您将执行以下操作:

public void sessionCreated(HttpSessionEvent se) {
    Vector ids = (Vector) se.getSession().getServletContext().getAttribute("currentUserIds");
    //manipulate ids
}

这不会绕过
/WEB-INF/WEB.xml
。此外,您将得到该类的2个实例,而不是1个同时执行这两个函数。我建议你把这个向量放在两个独立的类中

在servlet中,您可以通过。在侦听器中,您将执行以下操作:

public void sessionCreated(HttpSessionEvent se) {
    Vector ids = (Vector) se.getSession().getServletContext().getAttribute("currentUserIds");
    //manipulate ids
}

由于这与您的原始问题相关,作为后续问题,可能最好将后续问题作为原始问题的一部分发布。由于这与您的原始问题相关,作为后续问题,可能最好将后续问题作为原始问题的一部分发布。