||
Skip to end of metadata
Go to start of metadata

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), on the response from a resource, or both. Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext.

Sample Filter

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

 public void destroy() {
 }

 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
   throws IOException, ServletException {
  servletResponse.setContentType("text/html");
  //Pre filter logic
  filterChain.doFilter(servletRequest, servletResponse);
                            //post filter logic
 }
}

This filter can be added at product-level or application-level by updating the relevant web.xml file. See the sample given below. To enable this filter at product-level, add it to the <PRODUCT_HOME>/repository/conf/tomcat/web.xml file. To enable the filter for an individual application, you can add it to the web.xml in your application.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <filter>
  <filter-name>MyFilter</filter-name>
  <filter-class>MyFilter</filter-class>
  <init-param>
   <param-name>my-param</param-name>
   <param-value>my-param-value</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>MyFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

The following filters are available in Carbon Kernel:

  • org.wso2.carbon.ui.filters.CSRFPreventionFilter

  • org.wso2.carbon.ui.filters.CRLFPreventionFilter

  • org.wso2.carbon.tomcat.ext.filter.CharacterSetFilter

  • No labels