To give you a start, a small real-world-example:

package com.cr.manuals.filter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: HeaderFilter
 *
 */
 public class HeaderFilter implements javax.servlet.Filter {
        
         private static String PARAM_ADD_TO_CURRENT_MONTH = 
"ADD_TO_CURRENT_MONTH";
         private FilterConfig filterConfig = null;
         private int months2Add = 0;
         /* (non-Java-doc)
          * @see javax.servlet.http.HttpServlet#HttpServlet()
         */
         public HeaderFilter() {
                 super();
         }                              

        /**
        * init() : init() method called when the filter is instantiated.
        * This filter is instantiated the first time j_security_check is
        * invoked for the application (When a protected servlet in the
        * application is accessed).
        */
        public void init(FilterConfig aFilterConfig) throws ServletException {
                filterConfig = aFilterConfig;
                months2Add = 
Integer.parseInt(filterConfig.getInitParameter(PARAM_ADD_TO_CURRENT_MONTH));
        }

        /**
        * destroy() : destroy() method called when the filter is taken
        * out of service.
        */
        public void destroy() {
                filterConfig = null;
        }

        /**
        * doFilter() : doFilter() method called before the servlet to
        * which this filteris mapped is invoked. Since this filter is
        * mapped to j_security_check,this method is called before
        * j_security_check action is posted.
        */
        public void doFilter(ServletRequest aRequest, ServletResponse aResponse,
                                                FilterChain chain) throws 
java.io.IOException, ServletException {

              //System.out.println ("******** filter *******");
        
                  HttpServletRequest request = (HttpServletRequest)aRequest;
              HttpServletResponse response = (HttpServletResponse)aResponse;
              // call next filter in the chain : let j_security_check 
authenticate
              // user
              response.setHeader("Expires", createExpiresHeader(months2Add));
              chain.doFilter(request, response);
        
        }
        /**
         * Create a String in the format EEE, d MMM yyyy HH:mm:ss z"
         * Example: Fri, 4 Aug 2006 09:07:44 CEST
         * The value of the init-parama ADD_TO_CURRENT_MONTH is added to the
         * month-field of the current date
         * @return
         */
        private String createExpiresHeader(int someMonths2Add) {
                SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy
HH:mm:ss z", Locale.US);
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.MONTH, someMonths2Add);
                long millis = cal.getTimeInMillis();
                Date d = new Date(millis);
                return sdf.format(d);
        }
}

In your web.xml:

        <filter id="Filter_1">
                <filter-name>HeaderFilter</filter-name>
                <filter-class>com.cr.manuals.filter.HeaderFilter</filter-class>
                <init-param>
                        <param-name>ADD_TO_CURRENT_MONTH</param-name>
                        <param-value>1</param-value>
                </init-param>
        </filter>
        <filter-mapping>
                <filter-name>HeaderFilter</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>

HTH

Gregor
-- 
just because your paranoid, doesn't mean they're not after you...
gpgp-fp: 79A84FA526807026795E4209D3B3FE028B3170B2
gpgp-key available @ http://pgpkeys.pca.dfn.de:11371

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to