JSR286 Portlet Filter Sample


The Java Portlet Specification 2.0 added the concept of Portlet Filters, to allow for code to be called on portlet initialization, destruction and action, event and render phases, so that developers could intercept portlet requests and insert additional functionality to existing portlets (eg, for common mechanisms across portlets like auditing or a hit counter) similar to what has been available to J2EE developers via Servlet Filters for quite a while now.

For more information about JSR286 Java Portlet Specification 2.0 Portlet Filters and other mechanisms new to the 2.0 portlet spec, see the following article:


A sample portlet filter

import java.io.IOException;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.filter.FilterChain;
import javax.portlet.filter.FilterConfig;
import javax.portlet.filter.RenderFilter;
public class MyPortletFilter implements RenderFilter
public void doFilter(RenderRequest req, RenderResponse res, FilterChain chain)
throws IOException, PortletException
System.out.println("DEBUG: My Portlet Filter Pre-processing a render request");
// Hand off control to the next filter in the chain, 
// or the portlet itself if no more filters
chain.doFilter(req, res);
System.out.println("DEBUG: My Portlet Filter Post-processing a render request"); 
public void destroy()
System.out.println("DEBUG: Destroying My Portlet :-(");  
public void init(FilterConfig arg0) throws PortletException
System.out.println("DEBUG:  Initializing My Portlet");  

Configuring a portlet filter

The Java Portlet Specification 2.0 defines optional elements to define portlet filters, and then to map them to portlets (via portlet name) also defined in the descriptor.

WPF generates the WEB-INF/portlet.xml descriptor each time you publish a portlet WAR from a project, from a template under WEB-INF/bin/deploy ment. To avoid upgrade issues, WPF recommends not editing the portlet.xml template directly, but rather to use the new deployment descriptor fragment processing mechanism available as of the August 09 WPF Next beta update and followon WPF releases.
The fragment processing mechanism allows you to add elements to standard web and portlet deployment descriptors, by defining a subset of a descriptor in one of the WEB-INF/bin/deployment/extensions folders. For instance, see the readme in your project's WebContent/WEB-INF/bin/deployment/extensions/portlet.jsr286/Readme.txt

The following filter fragment file (MyPortletFilter.xml) should be placed under your project's WebContent/WEB-INF/bin/deployment/extensions/portlet.jsr286 folder.

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">