0% found this document useful (0 votes)
24 views

Servlet Filter

The document discusses servlet filters in Java, which are objects that intercept HTTP requests for various tasks such as logging, encryption, and input validation. It explains the lifecycle of filters, including initialization, service, and destruction methods, and emphasizes their pluggable nature for easier maintenance. Additionally, it outlines the process of creating filters and mapping them to servlets using web.xml or annotations.

Uploaded by

kaushiksubs000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Servlet Filter

The document discusses servlet filters in Java, which are objects that intercept HTTP requests for various tasks such as logging, encryption, and input validation. It explains the lifecycle of filters, including initialization, service, and destruction methods, and emphasizes their pluggable nature for easier maintenance. Additionally, it outlines the process of creating filters and mapping them to servlets using web.xml or annotations.

Uploaded by

kaushiksubs000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Advanced Java

- Payal Chaudhari
Working of Web API (with servlets)
Filters for Servlet
Filters :
• A Servlet filter is an object that can intercept HTTP requests targeted
at your web application.

• A servlet filter can intercept requests both for servlets, JSP's, HTML
files or other static content,
Usage of Filters
Filters for Servlet
Filters :
• A filter is an object that is invoked at the preprocessing and
postprocessing of a request.
• It is mainly used to perform filtering tasks such as conversion, logging,
compression, encryption and decryption, input validation etc.
• The servlet filter is pluggable, i.e. its entry is defined in the web.xml
file, if we remove the entry of filter from the web.xml file, filter will be
removed automatically and we don't need to change the servlet.
• So maintenance cost will be less.
• More than one Filter can also work in chain mode.
More than one filters can work in Chain
Usage of Filter
• Recording all incoming requests
• Logs the IP addresses of the computers from which the requests
originate
• Conversion
• Data compression
• Encryption and decryption
• Input validation etc.
Advantages of Filter
• Filter is pluggable.
• One filter don't have dependency onto another resource.
• Less Maintenance
Creating Filters
To work with Filters, we need following
• 3 Interfaces
• jakarta.servlet.Filter <- You have to implement it
• jakarta.servlet.FilterChain
• jakarta.servlet.FilterConfig
• 2 classes
• jakarta.servlet.annotation.WebFilter (Optional if you are going to work with web.xml)
• jakarta.servlet.http.HttpFilter <- You have to extend it
Filter Lifecycle
• Initialization (using Init Method)
• Service (using doFilter Method)
• Destroy (using destroy Methos)
Init Method
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}

Called only once when first time invoked


Destory method
public void destroy() {
// TODO Auto-generated method stub
}

Called when the Server stops.


Service (using doFilter Method)

public void doFilter(ServletRequest request, ServletResponse


response, FilterChain chain) throws IOException, ServletException {
//Code for Preprocessing (Before the request is passed to target)
chain.doFilter(request, response);
//Code for Postprocessing (After the response has been
generated by target, before passing response to client)
}
chain.doFilter(…) method
• Takes care of a chain of Filter.
• The chain of filter is decided by their declaration in web.xml (or in
annotation)
• During Preprocessing, If filter1 is passing request to filter2 and filter2 is
passing request to target servlet, then during postprocessing, servlet2 will
give response to filter2 and filter2 will pass the response to filter1. Filter1
will send the response to client.

• We will see the Filter creation process step by step from next slide
<- Right click on Project
name
<- Select New
<- Select Filter
Write Package Name

Write Class
Name

See the super class


name should be as
mentioned here
• Select the default filter mapping
(which is as per the name of your
filter)
• Click on Edit
• A new dialog window will appear
that will ask for the URL pattern
to be mapped (See on Next Page).
• You can add multiple URL
patterns also using Add button
• If you want you can add certial
init parameters also to filter as
shown in this window.
• As Discussed in Previous slide, You can add the URL
patterns from here with which the Filters will
collaborate for pre and post processing
• You can either select the URL pattern (if you want filter
to work for html, jsp and all other pages of your
WebApp.
• Or else You can select the Servlet also instead of going
for URL pattern.
• You can also select the type of dispatcher when the
filter should be called.
• If you want the filter to work for all pages, then inside
Pattern text box for URL Pattern write down /*
• When you will click Finish, the NewFilter.java (That you have created
using the steps shown in earlier slides) will open.

• In the import section you will see following that will cause errors

• Change these lines with the following ones:


Now it’s time for process
public void doFilter(HttpServletRequest request, HttpServletResponse
response, FilterChain chain) throws IOException, ServletException
{
System.out.println("Hello before process");
chain.doFilter(request, response);
System.out.println("Hi after process");
}
Mapping
of Filters
to
Servlet
• Here I have two filters
and two servlets.

• One filter is only


applicable to one
servlet.

• Second Filter is for all


Next Things with experiments

You might also like