Nearby lessons
15 of 34Servlet - Filters
- Understand FilterConfig
- Understand FilterChain
- Understand Demo Program for <dispatcher> tag
- See complete working code examples
Filters is an essential part of the Java Servlet technology. This lesson explains FilterConfig, FilterChain and DemoFilter.java with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.
FilterConfig(I)
For every Filter web container creates a FilterConfig object and handover to the filter as argument
to init() method.
By using FilterConfig object,Filter can get its configuration information.
FilterConfig defines the following methods...
FilterConfig(I)
returns the logical name of the filter which is configured in web.xml by using <filter-name> tag.
FilterConfig(I)
FilterChain
We can use FilterChain object to forward request to the next level. (It may be another Filter or
Servlet)
FilterChain interface contains the following method.
public void doFilter(SR req,SR resp) throws SE,IOE
DemoFilter.java
;
DemoFilter.java
TargetServlet.java
web.xml:
TargetServlet.java
| Analysis: | Request | Request |
|---|---|---|
| Browser | Target |
Servlet
| Response | Response |
|---|
Demo Filter
- Whenever we are sending the request to TargetServlet,web container checks is there any filter
configured for this servlet or not.
If any Filter configured,web container forwards the request to the Filter instead of Servlet.
After completing Filtering logic,Filter forwards the request to the TargetServlet.
After processing that request by TargetServlet , the response will be forwarded to the Filter
instead of browser.
After executing Filtering logic , Filter forwards total response to the browser.
http://localhost:7777/filter1/test
Configuring Filter in web.xml
We can configure Filter by using <filter> tag.
Configuring Filter in web.xml
We can map Filter either for a Particular url-pattern or to a particular Servlet or to the total web
application.
Filter-Mapping to a Particular url-pattern:
Configuring Filter in web.xml
If the request is coming with the specified url-pattern then automatically this filter will be
executed.
Filter-mapping to a Particular Servlet:
Configuring Filter in web.xml
Filter-Mapping for Total web application:
Configuring Filter in web.xml
For any request to the web application whether it is for servlet or jsp, this Filter will be executed.
Note:
Mapping Filter to the total web application is possible from Servlet 2.5V onwards.
<dispatcher> tag:
A servlet can get the request in one of the following 4 ways
FORWARD
Intermediate INCLUDE
Servlet
Target
| REQUEST | Servlet |
|---|
Intermediate ERROR
Servlet
Configuring Filter in web.xml
By default Filter concept is applicable only for direct end user REQUEST and not applicable for RD's
forward,include calls and error page calls.
If we want to extend for remaining cases also then we should go for <dispatcher> tag.
<dispatcher> tag introduced in Servlet 2.4V.
The allowed values for <dispatcher> tag are:
- REQUEST:
Filter will be executed for direct end user request.
This is default value
- FORWARD:
Filter will be executed for RD's forward() call.
- INCLUDE:
Filter will be executed for RD's include() call
- ERROR:
Filter will be executed for Error page call
Configuring Filter in web.xml
If we want to execute Filter for direct end user's request and RD's forward() call, then we have to
configured filter as follows...
Configuring Filter in web.xml
In this case Filter won't be executed for RD's include call and error page calls.
Case-2:
Configuring Filter in web.xml
In this case Filter will be executed only for RD's include call and in the remaining 3 cases filter wont
be executed.
Demo Program for <dispatcher> tag
web.xml:
Demo Program for <dispatcher> tag
TargetServlet.java
TargetServlet1.java
FirstServlet.java
DemoFilter.java
;
Filter Chain
We can configure more than one Filter for a Target Resource and all these filters will be executed
one by one and forms Filter Chain.
Target
| Log | Authentication Request | Response |
|---|
Filter
| Filter Data | Encryption |
|---|
Convert Filter
Filter Chain
Demo Program for FilterChain
First
Servlet
Browser Log Demo
Filter Filter
web.xml:
Demo Program for FilterChain
FirstServlet.java
DemoFilter.java
LogFilter.java
RequestURL()+" at "+ new Date());
LogFilter.java
Note:
In filter3 demo program, the log file is available in the following location:
Tomcat/logs/localhost.2017-02-26.txt
Web Container's Rule for ordering of Filters in FilterChain
- Identify all filters which are configured according to url-pattern & execute all these filters from
top to bottom.
- Identify all filters which are configured according to <servlet-name> & execute all these filters
from top to bottom.
Note:
<filter> and <filter-mapping> tags are direct child tags of <web-app> and hence we can take these
tags anywhere within <web-app>.
Difference b/w Filter's doFilter() Method and FilterChain's doFilter() Method:
| Filter's doFilter() Method | FilterChain's doFilter() Method |
|---|---|
| 1) public void doFilter (SR req, SR resp, FC fc) | 1) public void doFilter (SR req, SR resp) |
| throws SE, IOE | throws SE, IOE |
Web Container's Rule for ordering of Filters in FilterChain
| Method only. | be another Filter OR Servlet) |
|---|---|
| 3) It is call back method because Web Container | 3) It is not call back method because we have to |
| calls this Method automatically. | call explicitly this Method. |
Wrappers
Sometimes it is required to alter request and response information in the filters. We can
implement this by using wrapper classes.
i.e we can use wrappers inside filters to alter request and response information.
Eg1:
Within the filter,we have to convert end user's resume from word format to pdf format.
Eg 2:
Within the filter,we have to compress the response and that compressed response we can send to
the browser,so that we can reduce download time.
There are 2 types of wrappers
- Request wrappers
- Response wrappers
- Request wrappers:
To alter request information
There are two request wrapper classes...
ServletRequest (I)
ServletRequestWrapper (C) HttpServletRequest (I)
HttpServletRequestWrapper (C)
- ServletRequestWrapper
- HttpServletRequestWrapper
- Response wrappers:
To alter response information
ServletResponse (I)
| ServletResponseWrapper (C) | HttpServletResponse (I) |
|---|
HttpServletResponseWrapper (C)
There are two response wrapper classes
- ServletResponseWrapper
- HttpServletResponseWrapper
- Key ideas of Servlet - Filters explained simply
- Ready-to-use code examples
- Exam-style questions at the end