Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Java Servlet :Example of ServletContextListener
Previous Home Next

index.html

<html>
	<head>
		<title>My Second Web Application</title>
	</head>
	<body>
		<form method="get" action="listenerPage"> 
		<b>Name<input type="text" name="textName"> 
		<br><input type="submit" value="Login">
		</form>
	</body>
</html>

web.xml

<web-app>
	<listener> 
		<listener-class>MyListener</listener-class>
	</listener>
	<servlet> 
		<servlet-name>s1</servlet-name>
		<servlet-class>LoginServlet</servlet-class>
	</servlet>
	<servlet-mapping> 
		<servlet-name>s1</servlet-name> 
		<url-pattern>listenerPage</url-pattern>
	</servlet-mapping>
	<servlet> 
		<servlet-name>s2</servlet-name>
		<servlet-class>LogoutServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>s2</servlet-name>
		<url-pattern>logoutServlet</url-pattern>
	</servlet-mapping>
	<servlet>
		<servlet-name>s3</servlet-name>
		<servlet-class>AdminServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>s3</servlet-name>
		<url-pattern>adminServlet</url-pattern> 
	</servlet-mapping> 
</web-app>

AdminServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class AdminServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request,
	 HttpServletResponse response)throws ServletException,IOException
	{
	ServletContext ctx=getServletConfig().getServletContext();
	int t=((Integer) ctx.getAttribute("total")).intValue();
	int c=((Integer) ctx.getAttribute("current")).intValue();
	response.setContentType("text/html");
	PrintWriter out=response.getWriter();
	out.println("Welcome, Admin");
	out.println("<br>Count of user....");
	out.println("<br>Total user: "+t);
	out.println("<br>Current user: "+c);
	out.println("<br><a href=adminServlet>Refresh Counter</a>");
	out.close();
}
}

LoginServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class LoginServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request,
	 HttpServletResponse response)throws ServletException,IOException
	{
	String name=request.getParameter("textName");
	request.getSession();
	response.setContentType("text/html");
	PrintWriter out=response.getWriter();
	out.println("Welcome ,"+name);
	out.println("<br><a href=logoutServlet>LogOut</a>");
	out.close();
	}
}

LogoutServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class LogoutServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request,
	 HttpServletResponse response)throws ServletException,IOException
	{
	HttpSession session=request.getSession();
	session.invalidate();
	response.setContentType("text/html");
	PrintWriter out=response.getWriter();
	out.println("You are successfully logout");
	out.println("<br><a href=index.html>Login Again</a>");
	out.close();
	}
}

MyListener.java

import javax.servlet.*;
import javax.servlet.http.*;

public class MyListener implements ServletContextListener,
		HttpSessionListener
{
	ServletContext ctx;
	public void contextInitialized(ServletContextEvent e)
	{
	ctx=e.getServletContext();
	ctx.setAttribute("total",new Integer(0));
	ctx.setAttribute("current",new Integer(0));
	}
	public void contextDestroyed(ServletContextEvent e)
	{}
	public void sessionCreated(HttpSessionEvent e)
	{
	int t=((Integer) ctx.getAttribute("total")).intValue();
	int c=((Integer) ctx.getAttribute("current")).intValue();
	++t;
	++c;
	ctx.setAttribute("total",new Integer(t));
	ctx.setAttribute("current",new Integer(c));
	}
	public void sessionDestroyed(HttpSessionEvent e)
	{
	int c=((Integer) ctx.getAttribute("current")).intValue();
	--c;
	ctx.setAttribute("current",new Integer(c));
	}
}
Previous Home Next