- Filter also help to manipulate the responses that will be sent back to the client.
Different types of filters:
- Authentication-Blocking: Block or allow any requests based on user identity.
- Logging and auditing: Helps in user tracking.
- Image conversion: Helps with scaling maps
- Data compression: Reduces download size
- Localization: Helps support the code in various countries based on different languages and formatting.
- Encryption filters
- Tokenizing filters
- Mime-type chaining filters
- Caching filters
- XSL/T Filters: Helps transforming XML requests.
- Filters are specified in web.xml and are executed in the order specified in the file.
- Filter java class implements javax.servlet.Filter and implements these 3 methods: init(), doFilter(), destroy()
Compile TestFilter.java in usual way and put your class file in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classesimport javax.servlet.*; import java.util.logging.Filter; public class TestFilter implements Filter { public void init(FilterConfig config) throws ServletException { //Get and print init param String testParam = config.getInitParameter("param1"); System.out.println("Test Param: " + testParam); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException { // Get the method of the requestString method = request.getMethod(); logger.info("Request Method is: ", method); // Pass request back down the filter chainchain.doFilter(request,response); } public void destroy( ){ //close any resources here } }
Make following config change in the web.xml
<filter> <filter-name>TestFilter</filter-name> <filter-class>TestFilter</filter-class> <init-param> <param-name>param1</param-name> <param-value>Test</param-value> </init-param> </filter> <filter-mapping> <filter-name>TestFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
The TestFilter will apply to all the servlets since we specified url-pattern as /*. For example if we want to apply TestFilter to only a class called as TestFile then the config will look like
<filter> <filter-name>TestFilter</filter-name> <filter-class>TestFilter</filter-class> <init-param> <param-name>param1</param-name> <param-value>Test</param-value> </init-param> </filter> <filter-mapping> <filter-name>TestFilter</filter-name> <url-pattern>TestFile</url-pattern> </filter-mapping>
Multiple filters
<filter> <filter-name>TestFilter</filter-name> <filter-class>TestFilter</filter-class> <init-param> <param-name>param1</param-name> <param-value>Value1</param-value> </init-param> </filter> <filter-mapping> <filter-name>TestFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>TestFilter2</filter-name> <filter-class>TestFilter2</filter-class> <init-param> <param-name>param</param-name> <param-value>Value</param-value> </init-param> </filter> <filter-mapping> <filter-name>TestFilter2</filter-name> <url-pattern>TestFile</url-pattern> </filter-mapping>In this example, TestFilter will be applied followed by TestFilter2.
You might also like:
Java Concepts
Java String concepts
Immutable classes in Java
Remove duplicates from the array
Telephonic phone technical interview questions
Telephonic phone technical interview questions - Part 2
Serialization & Deserialization
Jersey Package Dependencies
Observable and Observer Interface
Servlet Client Request
Spring dependencies for Maven
Java String Operations