• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Servlet Filter

February 12, 2014 //  by Krishna Srinivasan//  Leave a Comment

  • Java EE Tutorials
  • Servlets Tutorials
  • Servlets Interview Questions

Filter is an object that transforms the information of request or response from one format to another. The filter class is declared in the deployment class.

The other types that can be implemented in a web application using filters is:

  • Security verification.
  • Session validation.
  • Internationalization.
  • Triggering resource access events.
  • Data compression.
  • MIME type changing.
  • Caching.
  • XSL transformation of XML response from a servlet

Methods Of Filter Interface

Name Description
init (FilterConfig) It is used during the initialization.
doFilter(ServletRequest, ServletResponse, FilterChain) It used when more than one filter is implemented in web application.
destroy( ) Destroy the instance of the filter class.

Example of Servlet Filter

Listing 1:FilterServ

package javabeat.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;

public class FilterServ implements Filter {
	public void  init(FilterConfig FilCon)
	{

	      String test = FilCon.getInitParameter("Javabeat");

	      System.out.println("Javabeat: " + test);

	}
 	 public void doFilter(ServletRequest req,
                       ServletResponse res,
                       FilterChain fc)
       		throws IOException, ServletExpection
       	{
	  	res.setContentType("text/html");

	  	PrintWriter out= res.getWriter();

		fc.doFilter(req,res);

	  	out.println("HelloWorldFilter");
      	 }

  	public void destroy( )
  	 	{

   		}
}

Details of the above code

The filter has initialization parameter, these can be read in the init method using the FilterConfig.

Listing 2:Web.xml

<web-app>

 	<filter>
  		 <filter-name>FilterServ</filter-name>
  		 <filter-class>javabeat.net.FilterServ</filter-class>

 	<init-param>
	  	<param-name>Javabeat</param-name>
	 	 <param-value>Hello World !!!!!!!!</param-value>
  	 </init-param>

</filter>

<filter-mapping>
   		<filter-name> FilterServ </filter-name>
 	 	 <url-pattern>/ FilterServ </url-pattern>
</filter-mapping>

</web-app>

Details of the program

  • In the above web.xml file the filter named is specified as FilterServ which is implemented in the filter class.
  • The <init-param> tag is used to initialize the parameter. It contains two tags namely <param-name> and <param-value> which is used to initialize the attributes of the filter.
  • Filter mapping is used to map the servlets and resources in the web application. It contains two tags namely <filter-name> and <url-pattern>. The <filter-name> tag is used to map the filter to a servlet or URL and the <url-pattern> tag associates the filter with a set of url’s.

The output of these program can be seen in console as:

Javabeat : Hello World!!!!!!!!!

Previous Tutorial : Session Tracking Using Servlet || Next Tutorial : How To Get Client IP Address In Servlet

Category: Java EETag: Servlets Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Session Tracking Using Servlet
Next Post: How To Get Client IP Address In Servlet »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact