Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • HttpSessionListener Interface in Servlet

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 295
    Comment on it

    HttpSessionListener in Servlet : We all know that user state is maintained in almost all the web applications like you have seen in shopping websites or banking websites etc. In java we mostly use application server's session handler. But what if we need to know number of sessions our application has created ? What if we need to limit the number of sessions created ?


    So there is a way to know the number of sessions our appication has created or also we can set some init parameters while session is created or removed. J2EE provides HttpSessionListener interface and HttpSessionEvent to notify the server that session is created or modified.


    HttpSessionListener interface has two methods which needs to be implemented :

    1. public void sessionCreated(HttpSessionEvent e): called when new session is created.
    2. public void sessionDestroyed(ServletContextEvent e): called when session is invalidated.


    Example of HttpSessionListener Interface : I will show here how we do operations like counting total and current logged-in users, maintains a log of user details such as login-logout time etc.

    Index.html

    <html>
    <body>
    <form action="servlet1">  
    Name:<input type="text" name="username"><br>  
    Password:<input type="password" name="password"><br>  
    
    <input type="submit" value="Login"/>  
    </form>
    </body>
    </html>
    

    CountSessionListener.java

    import javax.servlet.ServletContext;  
    import javax.servlet.http.HttpSessionEvent;  
    import javax.servlet.http.HttpSessionListener;  
    
    public class CountSessionListener implements HttpSessionListener{  
        ServletContext context = null;  
        static int totalLoginUsers = 0, currentLoginUsers = 0;  
    
        public void sessionCreated(HttpSessionEvent event) {  
        totalLoginUsers++;  
        currentLoginUsers++;  
    
        context = event .getSession().getServletContext();  
        context.setAttribute("totalLoginUsers", totalLoginUsers);  
        context.setAttribute("currentLoginUsers", currentLoginUsers);
        }  
    
        public void sessionDestroyed(HttpSessionEvent e) {  
            currentLoginUsers--;  
            context.setAttribute("currentLoginUsers", currentLoginUsers);  
        }  
    }
    

    TestServlet.java

    import java.io.IOException;  
    import java.io.PrintWriter;  
    import javax.servlet.ServletContext;  
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
    import javax.servlet.http.HttpSession;  
    
    public class TestServlet extends HttpServlet {  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {  
    
            response.setContentType("text/html");  
            PrintWriter out = response.getWriter();  
    
            String username = request.getParameter("username");  
            out.print("Welcome "+ username+ !);  
    
            HttpSession session=request.getSession();  
            session.setAttribute("username", username);  
    
            //retrieving data from ServletContext object  
            ServletContext context = request.getServletContext();  
            int total = (Integer)context.getAttribute("totalLoginUsers");  
            int current = (Integer)context.getAttribute("currentLoginUsers");  
            out.print("<br>total logged-in users= "+total);  
            out.print("<br>current logged-in users= "+current);  
            out.println(<br>);
            out.print("<a href='logout'>Logout</a>");     
            out.close();  
        }
    }
    

    LogoutServlet.java

    import java.io.IOException;
    import java.io.PrintWriter;  
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
    import javax.servlet.http.HttpSession;  
    
    public class LogoutServlet extends HttpServlet {  
    public void doGet(HttpServletRequest request,  
     HttpServletResponse response)  
            throws ServletException, IOException {  
    
            response.setContentType("text/html");  
            PrintWriter out = response.getWriter();  
    
            HttpSession session=request.getSession(false);  
            session.invalidate();//invalidating session  
    
            out.print("You are successfully logged out");   
            out.close();  
        }  
    }
    

    Last you need to do the entry of the listener in the web.xml

    <web-app>
    <listener>
           <listener-class>CountSessionListener</listener-class>
    </listener>
    </web-app>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: