We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 13 April 2014

Servlet – Page View Counter for Web Site

In this tutorial we are going to see about Page counter for web site using filter.
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called ServletExample as given below.
Servlet-dynamic web project
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create following Filter called PageViewCounter as shown in figure.
Create Filter
Servlet Filter 
5. Click Next it will show URL mapping.You can edit Filter URL if you need.
6. Create Servlet called HelloWorld.
7. By default your Servlet and Filter  will be mapped in web.xml, if your Servlet and Filter not mapped in your web.xml use the following configuration to map the Servlet in your web.xml
Filter Mapping
<filter>
<filter-name>Name of Filter</filter-name>
<filter-class>Fully Qualified Filter Class</filter-class>
</filter>
<filter-mapping>
<filter-name>Name of Filter</filter-name>
<url-pattern>/URL Pattern for Filter</url-pattern>
</filter-mapping>

Servlet Mapping

<servlet>
    <servlet-name>Your Servlet Name</servlet-name>
    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Your Servlet Name</servlet-name>
    <url-pattern>/URL to Call Servlet</url-pattern>
  </servlet-mapping> 

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_3_0.xsd"
 id="WebApp_ID" version="3.0">
 <display-name>ServetExample</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
 <filter>
  <filter-name>PageViewCounter</filter-name>
  <filter-class>com.javatutorialscorner.servlet.PageViewCounter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>PageViewCounter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <servlet>
  <servlet-name>HelloWorld</servlet-name>
  <servlet-class>com.javatutorialscorner.servlet.HelloWorld</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/HelloWorld</url-pattern>
 </servlet-mapping>
</web-app>

8. Add the required code inside doFilter() method.
PageViewCounter.java

package com.javatutorialscorner.servlet;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Servlet Filter implementation class PageViewCounter
 */
public class PageViewCounter implements Filter {

 int totalPreview;

 /**
  * Default constructor.
  */
 public PageViewCounter() {
  // TODO Auto-generated constructor stub
 }

 /**
  * @see Filter#destroy()
  */
 public void destroy() {
  // TODO Auto-generated method stub
 }

 /**
  * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
  */
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  // TODO Auto-generated method stub
  // place your code here

  totalPreview++;
  System.out.println("Page view Count " + totalPreview);
  // pass the request along the filter chain
  chain.doFilter(request, response);
 }

 /**
  * @see Filter#init(FilterConfig)
  */
 public void init(FilterConfig fConfig) throws ServletException {
  // TODO Auto-generated method stub
  totalPreview = 0;

 }

}

9. Now save and Run the Program , Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse) 
http://www.javatutorialcorner.com/2014/03/how-to-configure-tomcat-in-eclipse.html

See when tomcat start itself filter get start, because URL pattern mapped as /*. So, filter will be called for each and every page view inside application .
10.call the following URL see the count value in console .
http://localhost:8080/ServletExample/HelloWorld

Output

Page view Count 1
Page view Count 2
Page view Count 3
Inside HelloWorld Servlet
Page view Count 4
Inside HelloWorld Servlet

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Servlet – Page View Counter for Web Site Rating: 5 Reviewed By: eHowToNow