SlideShare a Scribd company logo
SESSION – 12
By→ Anuj Kumar Singh
Servlet Filter
1. A filter is an object that is invoked at the preprocessing and
postprocessing of a request.
2. It is mainly used to perform filtering tasks such as conversion, logging,
compression, encryption and decryption, input validation etc.
3. 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.
4. So maintenance cost will be less.
Usage of Filter
1. recording all incoming requests
2. logs the IP addresses of the computers from which the requests
originate
3. Conversion
4. data compression
5. encryption and decryption
6. input validation etc.
Advantage of Filter
1. Filter is pluggable.
2. One filter don't have dependency onto another resource.
3. Less Maintenance
Servlet 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.
2) FilterChain interface
1. The object of FilterChain is responsible to invoke the next filter or
resource in the chain.
2. This object is passed in the doFilter method of Filter interface. The
FilterChain interface contains only one method:
3. public void doFilter(HttpServletRequest request, HttpServletResponse
response): it passes the control to the next filter or resource.
4. <web-app>
<filter> <filter-name>...</filter-name> <filter-class>...</filter class>
</filter>
<filter-mapping> <filter-name>...</filter-name> <url-pattern>...</url-
pattern></filter-mapping>
</web-app>
Servlet   session 12

More Related Content

PPTX
Advanced java programming
PPTX
Servlet Filters
PPTX
Filter
PDF
Day3_Part 1_Apache_JMeter_Logic_Controllers
PDF
Servlet Filter
PDF
Java - Singleton Pattern
PPTX
Singleton Pattern (Sole Object with Global Access)
PPTX
Filter
Advanced java programming
Servlet Filters
Filter
Day3_Part 1_Apache_JMeter_Logic_Controllers
Servlet Filter
Java - Singleton Pattern
Singleton Pattern (Sole Object with Global Access)
Filter

Similar to Servlet session 12 (20)

PPTX
Advance java session 17
PPTX
Servlets - filter, listeners, wrapper, internationalization
PPT
Session 3 inter-servlet communication & filters - Giáo trình Bách Khoa Aptech
PPTX
Session 5 - Working withAFTER Filters.pptx
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
PPTX
PDF
Servlet
PPT
Listeners and filters in servlet
PPTX
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
PPTX
PPTX
Servlets
PDF
Ch. 13 filters and wrappers
RTF
Servlet lifecycle
PPTX
Unitwwsbdsbsdbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-4.pptx
PPTX
servlets sessions and cookies, jdbc connectivity
PDF
Introduction tomcat7 servlet3
PPTX
Wt unit 3
PPT
Java - Servlet - Mazenet Solution
PPTX
Servlet session 2
PPTX
SERVLET in web technolgy engineering.pptx
Advance java session 17
Servlets - filter, listeners, wrapper, internationalization
Session 3 inter-servlet communication & filters - Giáo trình Bách Khoa Aptech
Session 5 - Working withAFTER Filters.pptx
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
Servlet
Listeners and filters in servlet
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
Servlets
Ch. 13 filters and wrappers
Servlet lifecycle
Unitwwsbdsbsdbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-4.pptx
servlets sessions and cookies, jdbc connectivity
Introduction tomcat7 servlet3
Wt unit 3
Java - Servlet - Mazenet Solution
Servlet session 2
SERVLET in web technolgy engineering.pptx
Ad

More from Anuj Singh Rajput (20)

PPTX
Web technology
PPTX
Java script
PPTX
Html (hypertext markup language)
PPTX
PPTX
Jsp session 13
PPTX
Jsp session 12
PPTX
Jsp session 11
PPTX
Jsp session 10
PPTX
Jsp session 9
PPTX
Jsp session 8
PPTX
Jsp session 7
PPTX
Jsp session 6
PPTX
Jsp session 5
PPTX
Jsp session 4
PPTX
Jsp session 3
PPTX
Jsp session 2
PPTX
Jsp session 1
PPTX
Servlet session 14
PPTX
Servlet session 13
Web technology
Java script
Html (hypertext markup language)
Jsp session 13
Jsp session 12
Jsp session 11
Jsp session 10
Jsp session 9
Jsp session 8
Jsp session 7
Jsp session 6
Jsp session 5
Jsp session 4
Jsp session 3
Jsp session 2
Jsp session 1
Servlet session 14
Servlet session 13
Ad

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PDF
Pre independence Education in Inndia.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
master seminar digital applications in india
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Institutional Correction lecture only . . .
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Complications of Minimal Access Surgery at WLH
Pre independence Education in Inndia.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
TR - Agricultural Crops Production NC III.pdf
master seminar digital applications in india
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Basic Mud Logging Guide for educational purpose
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
Abdominal Access Techniques with Prof. Dr. R K Mishra
Supply Chain Operations Speaking Notes -ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPH.pptx obstetrics and gynecology in nursing
Institutional Correction lecture only . . .
Sports Quiz easy sports quiz sports quiz
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF

Servlet session 12

  • 1. SESSION – 12 By→ Anuj Kumar Singh
  • 2. Servlet Filter 1. A filter is an object that is invoked at the preprocessing and postprocessing of a request. 2. It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc. 3. 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. 4. So maintenance cost will be less.
  • 3. Usage of Filter 1. recording all incoming requests 2. logs the IP addresses of the computers from which the requests originate 3. Conversion 4. data compression 5. encryption and decryption 6. input validation etc. Advantage of Filter 1. Filter is pluggable. 2. One filter don't have dependency onto another resource. 3. Less Maintenance
  • 4. Servlet 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.
  • 5. 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.
  • 6. 2) FilterChain interface 1. The object of FilterChain is responsible to invoke the next filter or resource in the chain. 2. This object is passed in the doFilter method of Filter interface. The FilterChain interface contains only one method: 3. public void doFilter(HttpServletRequest request, HttpServletResponse response): it passes the control to the next filter or resource. 4. <web-app> <filter> <filter-name>...</filter-name> <filter-class>...</filter class> </filter> <filter-mapping> <filter-name>...</filter-name> <url-pattern>...</url- pattern></filter-mapping> </web-app>