FilterA.java
[java]package filters;
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;
public class FilterA implements Filter
{
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
System.out.println("Filter A initialized...");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException
{
System.out.println("Filter A executing Before JSP Processing ...");
filterChain.doFilter(request, response);
System.out.println("Filter A executing after JSP Processing...");
}
@Override
public void destroy() {
System.out.println("Filter A Destroyed..");
}
}[/java]
JSP Filters - The
init(FilterConfig filterConfig) method indicate to a filter being placed into service.The
void destroy() indicate to a filter that it is being taken out of service.
FilterB.java
[java]package filters;
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;
public class FilterB implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
System.out.println("Filter B initialized...");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException
{
System.out.println("Filter B executing Before JSP Processing ...");
filterChain.doFilter(request, response);
System.out.println("Filter B executing after JSP Processing...");
}
@Override
public void destroy()
{
System.out.println("Filter B Destroyed..");
}
}[/java]
web.xml
[xml]<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>filters</display-name>
<welcome-file-list>
<welcome-file>filter.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>FilterA</filter-name>
<filter-class>filters.FilterA</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterA</filter-name>
<url-pattern>/filter.jsp</url-pattern>
</filter-mapping>
<filter>
<filter-name>FilterB</filter-name>
<filter-class>filters.FilterB</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterB</filter-name>
<url-pattern>/filter.jsp</url-pattern>
</filter-mapping>
</web-app>[/xml]
Here URL pattern has been included and in the tag class name has mentioned with it's package.
filter.jsp
[java]<html>
<head>
<title> Filter Demo </title>
</head>
<body bgcolor="skyblue">
<center>
<img src="E:/splessons.png"></br></br>
<strong>
Welcome to Splessons, This is filter Demo !!!
</strong></center>
</body>
</html>[/java]
Here sample code has been written.
Output
JSP Filters - When compile the code following is the output will be displayed.
Following is the text will be displayed in the console.
[java]Filter A initialized...
Filter B initialized...
Test Param: Initialization Paramter
Filter A executing Before JSP Processing ...
Filter B executing Before JSP Processing ...
Filter B executing after JSP Processing...
Filter A executing after JSP Processing...
[/java]