Nov 9, 2021 3:37:19 PM Thomas Dumont avatar

Create servlets and filters

Introduction

Servlets and filters are usually declared in the WEB-INF/web.xml file, but this file is unique and can not be modified by plugins.

In order to offer plugins such components, Lutece proposes to create servlets and plugin filters. These are declared in the xml file of the plugin (located in webapp/WEB-INF/plugins/).

Java classes

Java classes for servlets or filters must be written in exactly the same way as standard servlets or filters. Only their declaration is different.

Existing classes, for example from external products, can be packaged as a servlet or plugin filter (example : the url rewrite filter used by the urlrewriter plugin).

Declaration of servlets and filters

Servlets and filters are to be declared in the plugin's XML file as follows :

<!-- servlets -->
<servlets>
    <servlet>
        <servlet-name>myServlet1</servlet-name>
        <url-pattern>/servlet/plugins/myplugin/myServlet1</url-pattern>
        <servlet-class>fr.paris.lutece.plugins.myplugin.web.MyFirstServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>myServlet2</servlet-name>
        <url-pattern>/servlet/plugins/myplugin/myServlet2</url-pattern>
        <servlet-class>fr.paris.lutece.plugins.myplugin.web.MySecondServlet</servlet-class>
    </servlet>
</servlets>

<!-- filters -->
<filters>
    <filter>
        <filter-name>myFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <filter-class>fr.paris.lutece.plugins.myplugin.web.MyFilter</filter-class>
        <init-param>
            <param-name>param1</param-name>
            <param-value>value of param1</param-value>
        </init-param>
    </filter>
</filters>

Servlet URLs must be based on "/servlet/plugins/myplugin"