Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Tomcat Users »

Re: Is it possible to intercept all requests and be serviced by a
ser  vlet?

Tim Funk

2004-05-18

Replies:

It sounds like using a Filter would be easier. With a filter - you can use
the Servlet API to get a Dispatcher for the servlet you wish to call. For
example, this filter reroute all requests to FooServlet:

package more.cowbell;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;

public class ReRouteFilter implements Filter {

  public void init(FilterConfig filterConfig) throws ServletException {
     ;
  }

  public void doFilter(ServletRequest request,
                ServletResponse response,
                FilterChain chain)
         throws IOException, ServletException {
    request.getRequestDispatcher("/FooServlet").forward(request, response);
  }

  public void destroy() {
    ;
  }
}

-----------
Then in $CATALINA_HOME/conf/web.xml
  <filter>
     <filter-name>ReRoute Filter</filter-name>
     <filter-class>more.cowbell.ReRouteFilter</filter-class>
  </filter>
  <!-- Trap everything going to the default servlet-->
  <filter-mapping>
     <filter-name>ReRoute Filter</filter-name>
     <servlet-name>default</servlet-name>
  </filter-mapping>
  <!-- Trap by file extension html-->
  <filter-mapping>
     <filter-name>ReRoute Filter</filter-name>
     <url-pattern>*.html</url-pattern>
  </filter-mapping>
-----------

-Tim

Chippada, Sreeni wrote:

> Hi Tim,
>  Thanks for your reply.
>  I know how to create a valve. But I do not know how to execute a
> servlet from a valve. I will be glad if you can you can tell me how to ro
> point me to appropriate resources.
>
>  Also, I do not know how to configure the filters in the server.xml.
> Appreciate any help.
>
> Thanks,
> Sreeni
>
> -----Original Message-----
> From: Tim Funk [mailto:funkman@(protected)]
> Sent: Tuesday, May 18, 2004 7:50 AM
> To: Tomcat Users List
> Subject: Re: Is it possible to intercept all requests and be serviced by a
> ser vlet?
>
> You can use a Valve.
>
> Or you can use a Filter configured in $CATALINA_HOME/conf/server.xml and the
>
> class would live in the common/ classloader
>
> -Tim
>
> Chippada, Sreeni wrote:
>
>>Hi,
>>   I am using Tomcat 5. I need all the requests be serviced by a
>>particular servlet irrespective of the web apps deployed. Is there a way
>
> to
>
>>do it?
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected)
For additional commands, e-mail: tomcat-user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.