Her is an example of such a servlet (this one does parse static files) :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class StaticDocumentServer extends HttpServlet {

        public void doGet(      HttpServletRequest request,
                                                HttpServletResponse response)
                                                throws IOException, ServletException {
                doPost(request, response);
        }

        public void doPost(     HttpServletRequest request,
                                                HttpServletResponse response)
                                                throws IOException, ServletException {

                response.setContentType("text/html");
                PrintWriter pw = response.getWriter();
                String path = getServletConfig().getServletContext().getRealPath("/");
                String directory = getServletConfig().getInitParameter("directory");
                String extraPath = request.getPathInfo();
                StringBuffer sbuf = null;
                try {
                        sbuf = new StringBuffer();;
                        BufferedReader bf = new BufferedReader(new FileReader(path + 
directory +
extraPath));
                        String line;
                        while ((line = bf.readLine()) != null) {
                                sbuf.append(line);
                        }
                }
                catch (FileNotFoundException fnfe) {
                        sbuf = new StringBuffer();
                        sbuf.append("<html><body>Sorry, the page you requested does 
not exist on
this server.</body></html>");
                }
                catch (IOException ioe) {
                        sbuf = new StringBuffer();
                        sbuf.append("<html><body>Sorry, a server error 
occured.</body></html>");
                }
                finally {
                        pw.print(sbuf.toString());
                }
        }
}

Here is the corresponding web.xml :

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd";>

<web-app>
        <servlet>
                <servlet-name>jgr</servlet-name>
                <servlet-class>StaticDocumentServer</servlet-class>
                <init-param>
                        <param-name>directory</param-name>
                        <param-value>jgr</param-value>
                </init-param>
        </servlet>

        <servlet-mapping>
                <servlet-name>
                        jgr
                </servlet-name>
                <url-pattern>
                        /jgr/*
                </url-pattern>
        </servlet-mapping>
</web-app>

Now, if you access http://www.mydomain.com/jgr/mydoc.html you get exactly
what you asked for.

Pierre-Yves

-----Message d'origine-----
De : A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]De la part de
Marco Trevisan
Envoy� : jeudi 12 juillet 2001 14:11
� : [EMAIL PROTECTED]
Objet : Re: url mapping


"Pierre-Yves Saumont" <[EMAIL PROTECTED]> wrote:

> Or you might use your servlet to display the static file, using the extra
> path information.
>
Well, a web server like Apache isn't tuned just to accomplish this work?
Serving static files isn't an unnecessary burden to a JVM ?

> This technique can be use to map a directory to a servlet. You can then
> access all document in this directory (and subdiretories) with their
static
> path, altough the document are served by the servlet. For example, when a
> user request :
>
> http://www.mydomain.com/jgr/mydoc.html
>
> he gets exactly what he asked for (document mydoc.html in directory jgr),
> but trough the servlet. The user never know he is accessing a servlet,
> altough you can use it to parse the contain of the file and do whatever
you
> want.
>
How can such kind of servlet locate files indenpently from servlet container
and operating system ?

Marco

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to