Sunday, November 20, 2011

How to add a filter in dynamic web project of java or control traffic on web






We can add a Filter in web project of java to filter the traffic on the web server.
    Basically filter a class as the Servlet Class in java. To make a Filter, class is necessary implements with the interface.
Source code to add the filter on the project:-
CounterFilter.java
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CounterFilter implements Filter  // create a class implements by Filter interface
{
       FilterConfig con;
   
  public void destroy()
  {
            }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws         IOException, ServletException {
                       
                        ServletContext context=con.getServletContext();
                        Integer count=(Integer)context.getAttribute("count");
                        if(count==null)
                        {
                                    count = new Integer(0);
                        }
                       
                        count=new Integer(count.intValue()+1);
                        context.setAttribute("count", count);
                       
                       
                        chain.doFilter(request, response);
            }

            public void init(FilterConfig fConfig) throws ServletException {
                       
                        con=fConfig;
            }

}
DispalyFilter.java
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;

public class DisplayFilter extends HttpServlet {
           
         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                   ServletContext context=getServletContext();
                   Integer count=(Integer) context.getAttribute("count");//use the filter variable
                   response.setContentType("text/html");
                   PrintWriter pw=response.getWriter();
                
                   if(count!=null)
                   {
                           pw.print("the current count is"+count);
                           
                   }
                  
                   else
                   {
                           pw.println("no avilable");
                   }
                                       
            }

           
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            }

}

Index.html:-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="DisplayFilter" method="get">

    <input type="submit"/>
</form>
</body>
</html>
Web.xml:-
 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
            <display-name>filters</display-name>
            <filter>
                        <description>
                        </description>
                        <display-name>CounterFilter</display-name>
                        <filter-name>CounterFilter</filter-name>
                        <filter-class>CounterFilter</filter-class>
            </filter>
            <filter-mapping>
                        <filter-name>CounterFilter</filter-name>
                        <url-pattern>*.html</url-pattern>
            </filter-mapping>
            <servlet>
                        <description>
                        </description>
                        <display-name>DisplayFilter</display-name>
                        <servlet-name>DisplayFilter</servlet-name>
                        <servlet-class>DisplayFilter</servlet-class>
            </servlet>
           
            <servlet-mapping>
                        <servlet-name>DisplayFilter</servlet-name>
                        <url-pattern>/DisplayFilter</url-pattern>
            </servlet-mapping>
           
            <welcome-file-list>
                        <welcome-file>index.html</welcome-file>
                        <welcome-file>index.htm</welcome-file>
                        <welcome-file>index.jsp</welcome-file>
                        <welcome-file>default.html</welcome-file>
                        <welcome-file>default.htm</welcome-file>
                        <welcome-file>default.jsp</welcome-file>
            </welcome-file-list>
</web-app>


This filter execute when request occurs of the servlet class only by the .html page.







For Further Reading,
General, How To, Java

0 comments:

Post a Comment


 

Site Status

Man Behind Technical Today

Hello, I am Navin Bansal. I am a student of MCA in Rajsthan Institute of Engineering and Technology and owner of this blog. I share my view and ideas among people.