Nearby lessons

15 of 34

Servlet - Filters

📌 What You Will Learn
  • 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.

Example02
Loading editor…

FilterConfig(I)

Example03
Loading editor…

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

;

Example05
Loading editor…

DemoFilter.java

Example06
Loading editor…

TargetServlet.java

web.xml:

Example07
Loading editor…

TargetServlet.java

Analysis:RequestRequest
BrowserTarget

Servlet

ResponseResponse

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

Example08
Loading editor…

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:

Example10
Loading editor…

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:

Example11
Loading editor…

Configuring Filter in web.xml

Filter-Mapping for Total web application:

Example12
Loading editor…

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

REQUESTServlet

Intermediate ERROR

Servlet

Example13
Loading editor…

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

Example14
Loading editor…

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

Example15
Loading editor…

Configuring Filter in web.xml

In this case Filter won't be executed for RD's include call and error page calls.

Case-2:

Example16
Loading editor…

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.

Example17
Loading editor…

Demo Program for <dispatcher> tag

web.xml:

Demo Program for <dispatcher> tag

Example19
Loading editor…

TargetServlet.java

Example20
Loading editor…

TargetServlet1.java

Example21
Loading editor…

FirstServlet.java

Example22
Loading editor…

DemoFilter.java

;

Example23
Loading editor…

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

LogAuthentication RequestResponse

Filter

Filter DataEncryption

Convert Filter

Filter Chain

Demo Program for FilterChain

First

Servlet

Browser Log Demo

Filter Filter

web.xml:

Demo Program for FilterChain

Example26
Loading editor…

FirstServlet.java

Example27
Loading editor…

DemoFilter.java

Example28
Loading editor…

LogFilter.java

RequestURL()+" at "+ new Date());

Example29
Loading editor…

LogFilter.java

Note:

In filter3 demo program, the log file is available in the following location:

Tomcat/logs/localhost.2017-02-26.txt

Example30
Loading editor…

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() MethodFilterChain's doFilter() Method
1) public void doFilter (SR req, SR resp, FC fc)1) public void doFilter (SR req, SR resp)
throws SE, IOEthrows 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 Container3) 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
Example32
Loading editor…
📝 Key Takeaways
  • Key ideas of Servlet - Filters explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.