code_junkie

Can I implement HttpSessionListener this way?

Nov 12th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. public class my_Servlet extends HttpServlet implements HttpSessionListener
  2. {
  3. String User_Id;
  4. static Vector<String> Valid_User_Id_Vector=new Vector<String>();
  5. private static int activeSessions=0;
  6.  
  7. public void sessionCreated(HttpSessionEvent se)
  8. {
  9. // associate User_Id with session Id;
  10. // add User_Id to Valid_User_Id_Vector
  11. Out(" sessionCreated : "+se.getSession().getId());
  12. activeSessions++;
  13. }
  14.  
  15. public void sessionDestroyed(HttpSessionEvent se)
  16. {
  17. if (activeSessions>0)
  18. {
  19. // remove User_Id from Valid_User_Id_Vector by identifing it's session Id
  20. Out(" sessionDestroyed : "+se.getSession().getId());
  21. activeSessions--;
  22. }
  23. }
  24.  
  25. public static int getActiveSessions()
  26. {
  27. return activeSessions;
  28. }
  29.  
  30. public void init(ServletConfig config) throws ServletException
  31. {
  32. }
  33.  
  34. public void destroy()
  35. {
  36.  
  37. }
  38.  
  39. protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
  40. {
  41. User_Id=request.getParameter("User_Id");
  42. }
  43.  
  44. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  45. {
  46. processRequest(request, response);
  47. }
  48.  
  49. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  50. {
  51. processRequest(request, response);
  52. }
  53.  
  54. public String getServletInfo()
  55. {
  56. return "Short description";
  57. }
  58. }
  59.  
  60. public void sessionCreated(HttpSessionEvent se) {
  61. Vector ids = (Vector) se.getSession().getServletContext().getAttribute("currentUserIds");
  62. //manipulate ids
  63. }
  64.  
  65. static String User_Id;
  66.  
  67. protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
  68. {
  69. HttpSession session=request.getSession(true);
  70. Add_User_Id(session.getId(),request.getParameter("User_Id"););
  71. }
  72.  
  73. static void Add_User_Id()
  74. {
  75. assiciate [Session_Id , User_Id] in a map
  76. }
  77.  
  78. static void Remove_User_Id(String Session_Id)
  79. {
  80. Valid_User_Id_Vector.remove(User_Id ) by looking it up in a [Session_Id , User_Id] map
  81. }
  82.  
  83. public void sessionCreated(HttpSessionEvent se)
  84. {
  85. activeSessions++;
  86. }
  87.  
  88. public void sessionDestroyed(HttpSessionEvent se)
  89. {
  90. if (activeSessions>0)
  91. {
  92. My_Servlet.Remove_User_Id(se.getSession().getId());
  93. activeSessions--;
  94. }
  95. }
Add Comment
Please, Sign In to add comment