Servlet HttpSession Listener, Following is an example to calculate current logged-in users.
index.html
[html]
<form action="servlet1">
Name:<input type="text" name="username">
Password:<input type="password" name="userpass">
<input type="submit" value="login"/>
</form>
[/html]
Here the developer just created the two text boxes to enter the
Name and
Password.
web.xml
[xml]
<web-app>
<listener>
<listener-class>sessionevent1.CountUserListener</listener-class>
</listener>
<servlet>
<servlet-name>First</servlet-name>
<servlet-class>sessionevent1.First</servlet-class>
</servlet>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>sessionevent1.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
</web-app>
[/xml]
Make sure to mention the URL of the servlet here and that should be same with HTML form.
CountUserListener.java
[java]package sessionevent1;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class CountUserListener implements HttpSessionListener{
ServletContext ctx=null;
static int total=0,current=0;
public void sessionCreated(HttpSessionEvent e) {
total++;
current++;
ctx=e.getSession().getServletContext();
ctx.setAttribute("totalusers", total);
ctx.setAttribute("currentusers", current);
}
public void sessionDestroyed(HttpSessionEvent e) {
current--;
ctx.setAttribute("currentusers",current);
}
}
[/java]
Here CountUserListener is the class that implementing the
HttpSessionListener,
sessionCreated(HttpSessionEvent se) Receives the notification that a session has been created.
First.java
[java]package sessionevent1;
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 First extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("username");
out.print("Welcome "+n);
HttpSession session=request.getSession();
session.setAttribute("uname",n);
ServletContext ctx=getServletContext();
int t=(Integer)ctx.getAttribute("totalusers");
int c=(Integer)ctx.getAttribute("currentusers");
out.print("total users= "+t);
out.print("current users= "+c);
out.print("<a href='logout'>logout</a>");
out.close();
}
}
[/java]
The
setContentType(String) is defined in ServletResponse interface and inherited by HttpServletResponse interface.
request.getParameter() method in the servlet class, to retrieve the input values from HTML page.
request.getSession() is just a convenience method. It does exactly the same as
request.getSession(true).
LogoutServlet.java
[java]package sessionevent1;
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();
out.print("You are successfully logged out");
out.close();
}
}
[/java]
The
PrintWriter is an abstract class for writing to character streams. Methods implement are write
(char[], int, int), flush(), and close()). Print formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
request.getSession() is just a convenience method. It does exactly the same as
request.getSession(true).
Output
Following is the generated page when compile the code where user has to enter details.
Entered details will be visible in this page.
When user click on logout the page will be logged out.