Skip to content
Related Articles

Related Articles

Java Servlets | Need of Filters

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 28 Nov, 2022
Improve Article
Save Article

The need for implementing filters can be understood with the help of few examples Let’s take example of a Web application that formats the data to be presented to clients in a specific format say Excel However, at a later point of time, the clients may require data in some other format, such as Hypertext Markup Language (HTML) Portable Document Format (PDF), or Word. In such a situation instead of modifying the code every time to change the format of data, a filter can be created to transform data dynamically in the required formats.

Let’s consider another example where a developer creates a Web application in which a servlet handles user logins. This implies that when a user submits his credentials, the servlet verifies the credentials against the user information. The servlet also creates a session for the user, so the other components in the application can also use the session details of the user. At a later point of time, the developer might require maintaining a login entry for each user login attempt in the application server’s log system. In order to implement this, the developer would need to change the existing code or add additional code to the servlet and redeploy the Web application.

In such a situation, a servlet, besides fulfilling its primary objective that is to accept request and send responses to clients has to implement additional functionalities. This additional lo servlet reduces the efficiency of the application. To overcome this problem filters were introduced that can implement these additional functionalities, such as verifying login credentials maintaining the server log in a database. One of the most striking features of the filters is that the can be reused in other Web applications as well.

Some situations and tasks where filters can be used are as follows:

  • Security verification
  • Session validation
  • Logging operations
  • Internationalization
  • Triggering resource access events
  • Image conversion
  • Scaling maps
  • Data compression
  • Encryption
  • Tokenization
  • Mime type changing
  • Caching and XSL transformations of XML responses
  • Debugging

Elaboration of some situation where filters are needed:

  • Modifying the Request Character cryptography : Currently, several browsers don’t send character cryptography data within the Content-Type header of a protocol request. If an cryptography has not been such by the shopper request, the instrumentation uses a default cryptography to analyse request parameters. If the shopper hasn’t set character cryptography and also the request parameters area unit encoded with a special cryptography than the default, the parameters are parsed incorrectly. you’ll be able to use the strategy setCharacterEncoding within the ServletRequest interface to line the cryptography. Since this technique should be known as before parsing any post knowledge or reading any input from the request, this perform may be a prime application for filters.
  • Reduce size of the Response : Another example of a filter that modifies the response is that the compression filter contained within the examples distributed with the Tomcat servlet engine. though high-speed net connections are getting a lot of commonplace, there’s still a requirement to use information measure effectively. A compression filter is handy as a result of you’ll be able to attach it to any servlet to cut back the scale of a response. 
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!