Windows 2003 server web - CHAPTER 10 INTRODUCTION TO FILTERING 421 The

CHAPTER 10 INTRODUCTION TO FILTERING 421 The very first version of Tomcat to support the Servlet 2.5 specification is Tomcat 5. While the 4.x series of Tomcat supports filters, the Servlet 2.4 functionality (namely support for filtering the request dispatcher pipeline) will be available only in the 5.x versions. Our First Filter SimpleFilter Before coding your first filter, let s take a quick look at one additional interface that you ll be using within the filters sample. The ServletContext Interface Using the getServletContext() method on the javax.servlet.FilterConfig object, the filter can obtain a reference to the current ServletContext that it s executing under. There is a ServletContext for each running web application. Because of this single-instance nature, the ServletContext is frequently used for sharing information globally. Using this reference, the filter can utilize the context s logger service. It can also use this interface to attach arbitrary attributes to the context during runtime. An attribute can be an arbitrary object associated with a String name. Attaching attributes to ServletContext is a popular way to pass information between processing agents during runtime. For example, state information can be passed between filter instances by using these attributes. Here are several of the most frequently used methods by filter writers, and you ll see them used in the sample filters later: Object getAttribute(String name): Obtains the value of a named attribute void setAttribute(String name, Object object): Attaches a named attribute to the ServletContext void removeAttribute(String name): Removes a previously attached attribute Enumeration getAttributeNames(): Returns a java.util.Enumerationconsisting of the names of all the currently attached attributes You ll need to write to the log file in your filters, using the following methods of the ServletContext object: void log(string msg): Writes a string to the currently active logging facility associated with the context void log(String msg, Throwable throw): Writes a string and a stack trace to the log Coding the Filter Your first filter is called SimpleFilter. Listing 10-1 shows the source code for SimpleFilter, which implements the Filter interface. To compile this class, you ll need to include the appropriate library JAR for the servlet API. This JAR is named servlet-api.jar and is located in the Tomcat commonlib directory. This simple filter won t do anything useful for now. It will simply make a log entry before calling FilterChain.doFilter()and another one right after it.
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Leave a Reply