have an (Web hosting support) online catalog application and you are
have an online catalog application and you are interested in knowing which catalogs the users are browsing most often. Because the request and response objects contain this information, you can easily track this information and store it into a database for further analysis. 15.2.2 Creating a Filter There are three basic steps for creating a filter: 1. Create a Java class that implements the javax.servlet.Filter interface and that contains a no-argument constructor. 2. Declare the filter in the web application deployment descriptor using the filterelement. 3. Package the filter class along with the rest of the web application resources. 15.2.2.1 Creating the filter class The first step in creating a servlet filter is to create a new Java class (or use an existing one) and have it implement the javax.servlet.Filter interface. Java classes can implement multiple interfaces, so you don’t necessarily have to create a new class. However, the class will eventually need to be loaded by the web container, so it shouldn’t be one that is installed only on a backend system, like an EJB container. The Filter interface has three methods that must be implemented by your class: public void init(FilterConfig filterConfig) throwsServletException; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException; public void destroy( ); The web container calls the init( )method when it’s ready to put the filter into service. You can initialize any needed resources in this method. The destroy( )method is the opposite of the init( )method. The web container calls this method when it takes the filter out of service. At this point, you should clean up any open resources that the filter may be using, such as database connections. Finally, the web container calls the doFilter( )method every time a request is received and the container determines that the filter instance should be notified. This is where you should place whatever functionality the filter is designed to perform. Example 15-2 shows an example filter class that could be used to log to the servlet log file or to initialize a third-party logging service. Example 15-2. A servlet filter class example import java.io.IOException; import javax.servlet.Filter;
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.