Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Defining and Configuring Filters in Web Applications

Tech May 10 4

Implementing filters in web applications follows a three-phase approach:

  1. Create backend resources including static assets (HTML, CSS, etc.) and dynamic components (Servlets, JSPs).
  2. Implemetn the Filter interface.
  3. Register the filter in web.xml to specify which resources it should intercept.

Creating Servlets

Two servlets are defined below:

package com.msb.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("myServlet1 executed service method");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().print("Data from myServlet1");
    }
}
package com.msb.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("myServlet2 executed service method");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().print("Data from myServlet2");
    }
}

Implementnig the Filter

The filter logic is implemented as follows:

package com.msb.filter;

import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("Filter processing request");
        chain.doFilter(request, response);
        System.out.println("Filter processing response");
        response.getWriter().print("Additional data from filter");
    }

    @Override
    public void destroy() {
    }
}

Configuring the Filter

The filter is registered in the deployment descriptor:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>myServlet1</servlet-name>
        <servlet-class>com.msb.servlet.MyServlet1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myServlet1</servlet-name>
        <url-pattern>/myServlet1.do</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>myServlet2</servlet-name>
        <servlet-class>com.msb.servlet.MyServlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myServlet2</servlet-name>
        <url-pattern>/myServlet2.do</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>filter1</filter-name>
        <filter-class>com.msb.filter.MyFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>filter1</filter-name>
        <servlet-name>myServlet1</servlet-name>
        <servlet-name>myServlet2</servlet-name>
    </filter-mapping>
</web-app>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.