Serving static content - novalexei/mod_servlet GitHub Wiki

Serving static content

It is seldom when the web site is 100% dynamic. Usually there is a static content also. There are plenty of http files, javascript, Style Sheets, images and other content which is easier to store on disc instead of generating them on the fly. Fortunately it easy to serve this type of content together with servlets.

There are two ways to do this:

  1. If mod_servlet didn't find any servlets mapped to the requested URL, it will try to locate file in that path, and if found it'll serve it.

  2. You can configure extensions of the resources to be served statically in web.xml deployment descriptor.

Let's add to out web.xml file in tutorial web application the following section:

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

Servlet name "default" refers to special servlet which takes control when there are no other servlets found. For real application you'll probably want to add also mappings for "*.js", "*.css", "*.jpg", etc. but for now it'll suffice. Now copy some html file (for example example.html) to your tutorial web application directory, restart the server and try to go to URL http://localhost/tutorial/example.html and you'll see it's contents in the browser window. You also can configure the mod_servlet to serve files from Apache document root rather than web application directory. For more information see translate.filepath property in servlet.ini documentation

Now let's take a closer look at request input and file uploading.

To The Programming Guide