Orion Filter Tutorial, Lesson 2, Your first parameterised Filter

In this part of the tutorial, we will build a simple Filter that uses an initialising parameter.



1 Introduction

    In this part of the tutorial, we will build a simple Filter that uses an initialising parameter.

    We will begin with creating a Filter in the development environment set up in the previous lesson. We will then create a jsp page that we will attach our filter to. We will then configure the usage of this filter, and finally test it.

2 Creating the MessageFilter

    This filter will put the value of the initialising parameter 'message' into the Request scope before the target is called.

    We will extend the GenericFilter we created in lesson 1, so if you havent already gone through lesson 1, this is a good time to do so.


    Figure 1: The MessageFilter extends the GenericFilter, overriding the doFilter method. It also uses the FilterConfig

    We will create the MessageFilter in the following steps:



    2.1 Step 1: Create a file named 'MessageFilter.java'

      Fire up your editor and start editing a source file with the name 'MessageFilter.java'

    2.2 Step 2: Add import statements

      Start of with the package name and any imports we need, so that your source looks like the following:


      package com.acme.filter;

      import javax.servlet.FilterChain;
      import javax.servlet.ServletRequest;
      import javax.servlet.ServletResponse;
      Listing 1: The import statements

      This will make sure that we have the neccessary classes available.

    2.3 Step 3: Add class declaration

      Continue with the following:


      public class MessageFilter extends GenericFilter
      {

      Above we let the MessageFilter class extend the GenericFilter class we created in lesson 1 of this tutorial.

    2.4 Step 4: override the doFilter method

      We should now override the doFilter() method in the following way:


      public void doFilter(final ServletRequest request,final ServletResponse response, FilterChain chain) throws java.io.IOException, javax.servlet.ServletException
      {
      System.out.println("Entering Filter");
      String message=filterConfig.getInitParameter("message");
      request.setAttribute("message",message);
      chain.doFilter(request,response);
      System.out.println("Exiting Filter");
      }
      }
      Listing 2: The doFilter method

      Above, we first print out that we are entering the filter. We then read the initialising parameter with the name of 'message' and add that value to the request in an attribute called 'message'. After that, we use the FilterChain that the container has given us to invoke the doFilter() method of the next Filter.

      Your MessageFilter.java should now look like this.

    2.5 Step 5: compile your Filter

      Compile your Filter.

      Your MessageFilter.class should be located in the 'orion/default-web-app/WEB-INF/classes/com/acme/filter/' directory when done.

3 Creating a JSP page to test the Filter

    Now, in order to use our filter we need to attach that filter to something. We will create a real simple JSP page and place it in the root of the default-web-app.

    We will create the JSP page in the following steps:



    3.1 step 1: Create a file named 'filter2.jsp'

      Fire up your editor so that you can create a file called 'filter2.jsp'.

    3.2 step 2: Write the source

      Add the following to your file:


      <HTML>
      <HEAD>
      <TITLE>Lesson 2</TITLE>
      </HEAD>
      <BODY>
      <HR>
      <P><%=request.getAttribute("message")%></P>
      <P>Check your console output!</P>
      <HR>
      </BODY>
      </HTML>
      Listing 3: A sample JSP page

      The JSP page above will try to print out a Request attribute with the name "message" to the page.

    3.3 step 3: Save the JSP page

      Save your file in the 'orion/default-web-app/' directory with the name 'filter2.jsp'.

      Your JSP page should now look like this.

4 Configuring your Filter

    It is now time for us to configure our Filter usage.

    To do so we will go through the following steps:



    4.1 Step 1: Configure Filter existence in web.xml

      Start up your editor and open the file 'orion/default-web-app/WEB-INF/web.xml'.

      After the staring <web-app>, after any </display-name> or </description>, add the following rows:


      <filter>
      <filter-name>message</filter-name>
      <filter-class>com.acme.filter.MessageFilter</filter-class>
      <init-param>
      <param-name>message</param-name>
      <param-value>A message to you!</param-value>
      </init-param>
      </filter>
      Listing 4: A Filter entry in web.xml

      This will tell the container that our web-application has yet another filter that we will refer to as 'message', which represents the class 'com.acme.filter.MessageFilter', and should have a init-param called 'message' set with the value specified.

    4.2 Step 2: Configure Filter mapping in web.xml

      After the mapping configuration of the 'helloWorld' Filter, add the following rows:


      <filter-mapping>
      <filter-name>message</filter-name>
      <url-pattern>/filter2.jsp</url-pattern>
      </filter-mapping>
      Listing 5: A Filter mapping entry in web.xml

      This will tell our container that within this web-application, any requests for '/filter2.jsp' will first invoke the filter named 'message'.

    4.3 Step 3: Save changes to web.xml

      Save your 'web.xml' and make sure that it is located in the 'orion/default-web-app/WEB-INF/' directory.

      Your web.xml should now look something like this.

5 Testing your Filter

    It is now time to test your filter.

    We will do so in the following steps:



    5.1 Step 1: Make sure Orion is running

      Make sure that your Orion Application Server is up and running.

    5.2 Step 2: Open the URL

      Open up the location 'http://localhost/filter2.jsp' in a normal browser.

      Substitute 'localhost' with the name of the server you are working with if needed.

      The result should be a page saying 'A message to you!' and telling you to view your console for any output.

    5.3 Step 3: Check the console output

      Check your console output.

      The console output should look something like:


      Entering Filter
      Exiting Filter
      Listing 6: Sample Filter output in console

    Congratulations! You have just created and successfully tested your first parameterised Filter!

    Continue with Lesson 3, Manipulating the response.

Copyright © 2003 IronFlare AB