Class PyFilter

java.lang.Object
org.python.util.PyFilter
All Implemented Interfaces:
javax.servlet.Filter

public class PyFilter extends Object implements javax.servlet.Filter
Enables you to write Jython modules that inherit from javax.servlet.Filter, and to insert them in your servlet container's filter chain, like any Java Filter.

Example:

/WEB-INF/filters/HeaderFilter.py:

 from javax.servlet import Filter

 # Module must contain a class with the same name as the module
 # which in turn must implement javax.servlet.Filter
 class HeaderFilter (Filter):
   def init(self, config):
       self.header = config.getInitParameter('header')

   def doFilter(self, request, response, chain):
       response.setHeader(self.header, "Yup")
       chain.doFilter(request, response)
 

web.xml:

 <!-- Initialize the Jython runtime -->
   <listener>
       <listener-class>org.python.util.PyServletInitializer</listener-class>
       <load-on-startup>1</load-on-startup>
   </listener>

 <!-- Declare a uniquely-named PyFilter -->
 <filter>
  <filter-name>HeaderFilter</filter-name>
  <filter-class>org.python.util.PyFilter</filter-class>

  <!-- The special param __filter__ gives the context-relative path to the Jython source file -->
  <init-param>
    <param-name>__filter__</param-name>
    <param-value>/WEB-INF/pyfilter/HeaderFilter.py</param-value>
  </init-param>

  <!-- Other params are passed on the the Jython filter -->
  <init-param>
    <param-name>header</param-name>
    <param-value>X-LookMaNoJava</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>HeaderFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
     
    void
    init(javax.servlet.FilterConfig config)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • PyFilter

      public PyFilter()
  • Method Details

    • doFilter

      public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws IOException, javax.servlet.ServletException
      Specified by:
      doFilter in interface javax.servlet.Filter
      Throws:
      IOException
      javax.servlet.ServletException
    • init

      public void init(javax.servlet.FilterConfig config) throws javax.servlet.ServletException
      Specified by:
      init in interface javax.servlet.Filter
      Throws:
      javax.servlet.ServletException
    • destroy

      public void destroy()
      Specified by:
      destroy in interface javax.servlet.Filter