Skip to content
Related Articles

Related Articles

What is JSP Filters?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 01 Sep, 2020

JSP(Java Server Pages) is a server-side technology used to create web applications in Java. 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.


A web.xml file can be created to deploy JSP filters, following code syntax can be inferred for reference:









Working of a JSP Filter

Working of a JSP Filter

JSP filters are the java classes that primarily are used for intercepting the requests from any client or to manipulate the server’s response. Additionally, techniques like logging and auditing are used by the filters to provide the functionality of data security by encrypting the data as well as providing user authentication. To create and implement a filter interface, javax.servlet class can be employed.

Apart from JSP, servlets and other markup languages like HTML make use of filters. Moreover, we can also see the use of filters in images and videos as well. It is evident that in some conditions where it is required to degrade the performance of say videos, filters are being used to add some extra complexity maybe because it is very fast and very much accessible by different user environments.

The web.xml file which is used to define filters is being mapped to the servlets and/or JSP pages in order to use the filter functionalities. JSP containers are invoked when JSP codes are run and at the same time, an instance of the filters that are deployed in the deployment descriptor file is created.

Types of Filters in JSP

1. Logging Filters: Logging filters are the ones that can be used in place whenever we are trying to log any information in the backend or say over a server. A simple example to understand this is to use a LoggingTimeFilter to log in the times for incoming requests and outgoing responses in the logs.

2. Data Compression Filters: As the name suggests, the filter can be employed to the incoming information where the data needs to be compressed to a certain extent. The data is being compressed to the required percentage by the compression filters and then communicated accordingly.

3. Tokenizing Filters: All the user data that are coming in are stored and retrieved with the help of employing the tokenizing filters. 

4. Encryption Filters: Sometimes the code is secured with the help of filters and sometimes we want to encrypt and save user data that is flowing through the filters, in order to achieve this goal Encryption filters come into existence and does the job well.

5. Authentication Filters: The authentication filters are employed to accomplish the motive of authentication only. All the users trying to get in are authenticated by such filters based on the pre set criteria.

The need for JSP Filters

The filters can be used in plenty of ways in a web page definition and backend management as all the data sent over by a user can be successfully translated and encrypted by the Logging filters and Encryption filters and the data can be transacted or retrieved employing the Tokenizing filters. Similarly, the user data coming in can be compressed accordingly using the Data Compression filters.

If we have some sensitive code to be written then this can also be effectively accomplished using filters, where we will be writing the code in the filters and not in the servlet. When a user calls the servlet, the filter will not be retrieved by the user, but the corresponding JSP page will be retrieved. Writing sensitive codes in the servlets make it more prone to be easily attacked by the hackers and may cause a heavy loss in case a database is associated.



public class tagHandler implements Filter {
    public void init(FilterConfig filters)
        throws ServletException
        System.out.println("Filter  started...");
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain f)
        throws IOException, ServletException
        String user = request.getParameter("user");
        if (user == null || user.equals("")) {
            PrintWriter writer = response.getWriter();
            String m = "Welcome to my domain";
        f.doFilter(request, response);
    @Override public void destroy()
        System.out.println("Filter  deleted..");
<body> < % String user = request.getParameter("user");
% > < % out.print(user);
% > </ body>


Web applications are primarily employing filters for JSP and servlets because of the capability of filters to regulate data based on user needs and modifying it accordingly. Be its authentication to the encryption or managing sensitive data, filters handle it all based on the requirements and hence find a firm place in the programming systems, mostly in case of dealing with sensitive data.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!