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


Top 5 Contributors of the Month
david stephan

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>
<filter-name>This contains mapping name for the filter</filter-name>
<filter-class> This contains class name for the filter </filter-class>
</filter>

<filter-mapping>
<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>
</filter-mapping>

Example: -
access.html

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

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();
  res.setContentType("text/html");
  out.println("<html><body bgcolor='blue'>");
  out.println("<h1>First  Servlet</h1>");
  out.println("</body></html>");
}
}

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");
fw.close();
        chain.doFilter(request, response);
    }
    catch(Throwable t) {
  System.out.println(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

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

<filter>
<filter-name>Filtercounter</filter-name>
<filter-class>Filtercounter</filter-class>
</filter>

<filter-mapping>
<filter-name>Filtercounter</filter-name>
<url-pattern>/genserv</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>genserv</servlet-name>
<servlet-class>genserv</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>genserv</servlet-name>
<url-pattern>/genserv</url-pattern>
</servlet-mapping>
     
</web-app>

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:

Responses

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