top of page

Servlet Filters

  • javastrokes
  • Sep 19, 2016
  • 3 min read

Functionalities of Servlet Filters A Java Servlet Filters are nothing but a Java class objects, used for pre-processing of requests and post-processing of responses. Servlet filter is one of the component types of Servlet programming. A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filter is a feature which intended for situations where the developer cannot change the coding of an existing resource and needs to modify the behaviour of that resource. Usually when the web/servelt container invokes a servlet for the incoming clients request by default, the request is passed directly to the servlet. The response that the servlet generates is, by default, passed directly back to the client, with its content unmodified by the container. Alternatively, you can use servlet filters to pre process Web application client requests and post process server’s responses. Java Servlet Filters Example Application Filters are pluggable compontents that you can use and configure to perform some filtering tasks. Filters are configured in deployment descriptor (web.xml) file. Servlets and filters both are unaware of each other and we can add or remove a servlet filter just by editing web.xml. For example, 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 source code.

Configuring a Servlet Filter in web.xml Add a filter declaration. The <filter> element declares a filter, defines a name for the filter, and specifies the Java class that executes the filter. The <filter> element must directly follow the <context-param> element and directly precede the <listener> and <servlet> elements. For example: <context-param>Param</context-param> <filter> <filter-name>myFilter</filter-name> <display-name>My Filter</display-name> <description>This is my filter</description> <filter-class>examples.myFilterClass</filter-class> <init-param> <param-name>myInitParam</param-name> <param-value>myInitParamValue</param-value> </init-param>

</filter> <listener>Listener</listener> <servlet>Servlet</servlet>

Your Filter class can read the initialization attributes as mentione above. You can add filter mappings. The <filter-mapping> element specifies which filter to execute based on a URL pattern or servlet name. The <filter-mapping> element must immediately follow the <filter> element(s).


To create a filter mapping using a URL pattern, specify the name of the filter and a URL pattern. For example, the following filter-mapping maps myFilter to requests that contain /myPattern/.


<filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/myPattern/*</url-pattern> </filter-mapping>

To create a filter mapping for a specific servlet, map the filter to the name of a servlet that is registered in the Web application. For example, the following code maps the myFilter filter to a servlet called myServlet:


<filter-mapping> <filter-name>myFilter</filter-name> <servlet-hame>myServlet</servlet-name> </filter-mapping>

How Servlet Filters Work: How the Servlet Container Invokes Filters

java servlet filter

The following figure, shows how the servlet container invokes filters. Several filters (1, 2, ..., N) have been configured in a chain to be invoked by the container before the servlet is called and after it has responded. The web.xml file specifies which servlets cause the container to invoke the filters. When a client’s request comes to the Servlet/web Container, it checks if any filter has URL patterns that matches the requested URL, then the servlet Container locates the first filter with a matching URL pattern in the filter chain and that filter's code is executed. Similarly, suppose if another or second filter in the chain has the same matching URL pattern, that filter’s code is then executed.


Note: The order in which filters are invoked depends on the order in which they are configured in the web.xml file. During the clients request, the first filter in web.xml is the first one to be invoked and the last filter in web.xml is the first one invoked during the response.


Comments


Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
Recent Posts
Archive
Search By Tags
  • Grey Facebook Icon
  • Grey Twitter Icon
  • Grey Instagram Icon

© 2016 by JavaStrokes. Proudly created with Wix.com

Success! Message received.

© Copyright 2016 by JavaStrokes
bottom of page