JSP - SPLessons

Chapter 38

JSP Filters

JSP Filters

JSP Filters – JSP Filters are Java classes that can be utilized for catching requests from a client or controlling responses from server. The filters can be utilized to do Authentication, Encryption, Logging, Auditing. One can delineate to either JSP names or URL designs in application’s sending descriptor document web.xml. The filters execute in the request that they are pronounced in the deployment descriptor.

Servlet Filters

Following are the different types of filters.

In code JSP filter class has to implement javax.servlet.Filter interface and this interface will define the following three methods.


To catch requests from a client before they get to an asset at back end.To control responses from server before they are sent back to the customer.
FilterA.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..");
}
}

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

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..");
}
}

web.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>

Here URL pattern has been included and in the tag class name has mentioned with it’s package.
filter.jsp

<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>

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.

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...

Summary

  • JSP Filters – doFilter() method will be called by the container.
  • doFilter() method will be called only after init().
  • Filter class description will be written in web.xml file.