======================================================================
How to See Total Visitors in webpage Using  HTMl front End and Servlet Used for forwarding the Control ?
=======================================================================
For creating this project we need to create Some files like :-
index.jsp :- for front page
logout.jsp:- for log out page
            
________________________________index.jsp________________________________________

<html>
<body bgcolor="#FFCCFF">
<h1 align="center">Welcome to the World of java </h1>
<table align="right">
<tr>
<td>
<h4>
Total Visited :<%=application.getAttribute("TV") %>
</h4></td>
</tr>
<tr>
<td>
<h4>
Total Active :<%=application.getAttribute("TA") %>
</h4></td>
</tr>
</table>
<br />
<br />
<h4 align="center">Home page</h4>
<br />
<br />
<a href="logout.jlc">LOGOUT</a>
</body>
</html>

____________________________________Logout.jsp____________________________________
<%@ page session="false"%>
<html>
<body bgcolor="#FFCCFF">
<h1>Welcome to the World of java </h1>
<table align="right">
<tr>
<td>
<h4>
Total Visited :<%=application.getAttribute("TV") %>
</h4></td>
</tr>
<tr>
<td>
<h1>
Total Active :<%=application.getAttribute("TA") %>
</h1></td>
</tr>
</table>
<br />
<br />
<h4>You have logged out successfully</h4>
<br />
<br />
<a href="index.jsp">Go To Index Page</a>
</body>
</html>

======================================================================
Now for forwarding a Control we have create three servlet classes 
LogoutServlet.java
MyContextListener.java
MySessionListener.java

______________________________LogoutServlet.java ________________________________
package com.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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 {
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    HttpSession sess=req.getSession(false);
    if(sess!=null)
    sess.invalidate();
    RequestDispatcher rd=req.getRequestDispatcher("logout.jsp");
    rd.forward(req, res);
}

}

__________________________MyContextListener.java________________________________
package com.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyContextListener implements ServletContextListener {
      public void contextInitialized(ServletContextEvent event) {
    System.out.println("**ContextInitialized***");
    ServletContext ctx=event.getServletContext();
    ctx.setAttribute("TV", 0);  
    ctx.setAttribute("TA", 0);  
    }
    public void contextDestroyed(ServletContextEvent arg0) {
    System.out.println("***contextDestroyed**");
    }
}

__________________________MySessionListener.java______________________________
package com.servlet;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener {
    public void sessionCreated(HttpSessionEvent event) {
    HttpSession sess=event.getSession();
    ServletContext ctx=sess.getServletContext();
    int tv=(Integer) ctx.getAttribute("TV");
    tv++;
    ctx.setAttribute("TV", tv);
    int ta=(Integer) ctx.getAttribute("TA");
    ta++;
    ctx.setAttribute("TA", ta);
    }
    public void sessionDestroyed(HttpSessionEvent event) {
    HttpSession sess=event.getSession();
    ServletContext ctx=sess.getServletContext();
    int ta=(Integer) ctx.getAttribute("TA");
    ta--;
    ctx.setAttribute("TA",ta);
    
    }
}
====================================================================
For mapping
web.xml 

_______________________________web.xml___________________________________
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Lab29</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>logoutServ</servlet-name>
    <servlet-class>com.servlet.LogoutServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>logoutServ</servlet-name>
    <url-pattern>/logout.jlc</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>com.servlet.MyContextListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.servlet.MySessionListener</listener-class>
  </listener>
</web-app>



==================================================================
Please find attached image   where we put all these files so it can be accessible  and O/P Console as well .


if any Doubt and suggestion  wanted Contact any time 





Thanks & Regards
Anand Dwivedi
Software Developer 
Email: ananddwivedi92@gmail.com

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Java Design pattern

Map class field to map