Filter API
Like servlet filter have its own API. The
javax.servlet package contains the three interfaces of Filter API.
1. Filter
2. FilterChain
3. FilterConfig
1) Filter interface
For creating any filter, you must implement the
Filter interface. Filter interface provides the life cycle methods for a
filter.
Method
|
Description
|
public
void init(FilterConfig config)
|
init()
method is invoked only once. It is used to initialize the filter.
|
public
void doFilter(HttpServletRequest request,HttpServletResponse response,
FilterChain chain)
|
doFilter()
method is invoked every time when user request to any resource, to which the
filter is mapped.It is used to perform filtering tasks.
|
public
void destroy()
|
This is
invoked only once when filter is taken out of the service.
|
The object of FilterChain is responsible to
invoke the next filter or resource in the chain.This object is passed in the
doFilter method of Filter interface.The FilterChain interface contains only one
method:
public void
doFilter(HttpServletRequest request, HttpServletResponse response):it passes the control to the next filter or resource.
How to define Filter
We can define filter same as servlet. Let's see
the elements of filter and filter-mapping.
1. <web-app>
2.
3. <filter>
4. <filter-name>...</filter-name>
5. <filter-class>...</filter-class>
6. </filter>
7.
8. <filter-mapping>
9. <filter-name>...</filter-name>
10. <url-pattern>...</url-pattern>
11. </filter-mapping>
12.
13. </web-app>
For mapping filter we can use, either
url-pattern or servlet-name. The url-pattern elements has an advantage over
servlet-name element i.e. it can be applied on servlet, JSP or HTML.
If this information is important to you , and you want to , I will continue writing some more details of programming language, so click on the advertising, available on this page. This is motivate me for writing some more blogs.
Comments
Post a Comment