tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: Rewriting via web.xml instead of mod_rewrite?
Date Thu, 11 May 2000 16:54:53 GMT
Volker Stolz wrote:

> We're using Tomcat-standalone, i.e. without using Apache or another server.
> I want to redirect every access to the URL /path/to/files to
> /servlet/class/path/to/file. Is this possible? If yes, is there a way of
> mapping an entire directory (/path/to) instead of each file?
>

If the servlet you want to forward to is fixed, here's one approach.  Add the
following entries into your web.xml file:

    <servlet>
        <servlet-name>PathToServlet</servlet-name>
        <servlet-class>com.mycompany.MyPathToServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>PathToServlet</servlet-name>
        <url-pattern>/path/to/*</url-pattern>
    </servlet-mapping>

Your servlet will be called for every request URI that starts "/path/to" (after
the context path, if any).  It can pick up the "files" part of the request URI
by calling request.getPathInfo().  One thing you might want to do is read the
actual file that this refers to.  For that, you can do something like:

    String pathInfo = request.getPathInfo();
    InputStream stream =
      getServletContext().getRealPath(pathInfo);

and read from this input stream.

>
> Volker

Craig McClanahan



Mime
View raw message