.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Filter in servlet.

Posted By:satyapriyanayak       Posted Date: April 29, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know the use of filter in servlet.

Filter: - It is a program which executes on the time of providing request to a specific url. The filter resides in a webserver. Whenever the user provides the request to the associated URL then the request goes to the filter. The filter can modify the request or perform any task before passing the request to the destination.

Creation process of filter

1.create a class by inheriting javax.servlet.filter interface.
2.Override methods of filter interface. The methods are as follows:
public void init()
public void doFilter(servletRequest req,servletResponse res,filterchain chain)
public void destroy()
3.Perform the required task inside doFilter()
4.Transfer request and response to the destination by using doFilter() of filterchain.

Storing, compilation and deployment of filter
1.Store the filter file inside classes' folder of the context.
2.Compile the filter file by using servlet-api.jar in classpath.
3.deploy the filter by using the required tags in web.xml file. The tags are as follows:

<filter-name>This contains mapping name for the filter</filter-name>
<filter-class> This contains class name for the filter </filter-class>

<filter-name> This contains mapping name for the filter </filter-name>
<url-pattern>/ This contains the url for whom the filter will execute </url-pattern>

Example: -

<h1><a href="./genserv">Visit Genserv</a></h1>

genserv.java file

//Using GenericServlet
import java.io.*;
import javax.servlet.*;
public class genserv extends GenericServlet

public void service(ServletRequest req,ServletResponse res)throws IOException,ServletException
  PrintWriter out=res.getWriter();
  out.println("<html><body bgcolor='blue'>");
  out.println("<h1>First  Servlet</h1>");

Filtercounter.java file

//A filter file
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Filtercounter implements Filter {

public void init(FilterConfig con){}
    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)throws IOException, ServletException
    try {
                FileWriter fw=new FileWriter("E:/filter/fil.txt",true);
                fw.write("page accessed");
        chain.doFilter(request, response);
    catch(Throwable t) {
public void destroy(){}


Web.xml settings

<?xml version="1.0" encoding="ISO-8859-1"?>
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at


  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  See the License for the specific language governing permissions and
  limitations under the License.
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"





Compile both the files as below

javac -cp servlet-api.jar genserv.java (for tomcat 6.0)
javac -cp servlet-api.jar Filtercounter.java (for tomcat 6.0)

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend