213 Servlet Filter | Servlet Tutorial Advanced java servlet tutorial | What is Servlet Filter ? Java

Published: 05 June 2020
on channel: tech fort
187
1

Servlet Filter : What is ServletFilter? - Servlet Filter is a special web resource program of java web application, that trap's request and response of other web resource programs of java web application.

- Servlet filter program can execute pre-request processing logic by trapping the request and it can also executes post-response generation logic by trapping the response.

Need/Use: To provide additional functionality in the web application without modifying the source code of existing web application, then we can take the support of filters in java web application.

Understanding Example diagram of Servlet filter
(1 diagram of Servlet filter):
------------------------------------------------




With respect to above diagram:
------------------------------
Step 1) Browser window gives request to TestSrv program.

Step 2), 3) Filter traps the request and executes pre-request processing logic.

Step 4), 5) Servlet filter passes the request To TestSrv servlet program & this program executes the main request processing logic.

Step 6), 7) Servlet program generates response to browser window and this response will be trapped by Servlet filter program and also executes post-response generation logic.

Step 8) Filter program sends final response to browser window.

Note: To link servlet filter with servlet program we need to take the url-pattern of servlet program as the url-pattern of ServletFilter program.