MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

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