Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of filter and chain in servlet?

chain.doFilter(req,res);
We used this in a servlet program. I want to know what is the use of the method doFilter() in a servlet? Also what is the use of filter and chain concept in Java servlets?

like image 980
Vinoth Kumar Avatar asked Nov 08 '10 10:11

Vinoth Kumar


People also ask

What is the use of filter in servlet?

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method.

Why filter is used in Java?

Filters are systems or elements used to remove substances such as dust or dirt, or electronic signals, etc., as they pass through filtering media or devices. Filters are available for filtering air or gases, fluids, as well as electrical and optical phenomena.

What is servlet and servlet filter?

Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes − To intercept requests from a client before they access a resource at back end. To manipulate responses from server before they are sent back to the client.

Why do we use filters in JSP?

To manipulate the server's response and to intercept the requests from a client, JSP has its proprietary filters that are used with the help of a java class. At the same time, the filters are used to authenticate the credentials of the user and to perform data encryption techniques on the data in question.


2 Answers

Servlet filters are implementation of the chain of responsibility pattern

The point is that each filter stays "in front" and "behind" each servlet it is mapped to. So if you have a filter around a servlet, you'll have:

void doFilter(..) {      // do stuff before servlet gets called      // invoke the servlet, or any other filters mapped to the target servlet     chain.doFilter(..);      // do stuff after the servlet finishes } 

You also have the option not to call chain.doFilter(..) in which case the servlet will never be called. This is useful for security purposes - for example you can check whether there's a user logged-in.

like image 200
Bozho Avatar answered Sep 30 '22 01:09

Bozho


What are Filters ?

Filters are used to intercept and process requests before they are sent to servlets(in case of request) .

OR

Filters are used to intercept and process a response before they are sent back to client by a servlet.

enter image description here

Why they are used ?

-Filters can perform security checks.

-Compress the response stream.

-Create a different response.

What does doFilter() do ?

The doFilter() is called every time the container determines that the filter should be applied to a page.

It takes three arguments

->ServletRequest

->ServlerResponse

->FilterChain

All the functionality that your filter supposed to do is implemented inside doFilter() method.

What is FilterChain ?

Your filters do not know anything about the other filters and servlet. FilterChain knows the order of the invocation of filters and driven by the filter elements you defined in the DD.

like image 22
Prateek Joshi Avatar answered Sep 30 '22 03:09

Prateek Joshi