O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.8. Creating Welcome Files for a Web Application

Problem

You want to configure one or more welcome files for a web application.

Solution

Use a welcome-file-list element in your deployment descriptor.

Discussion

A welcome file is a tradition as old as the hypertextual Internet. Many sites have homepages or other welcome files that are designed to be the entry page or front door for their web sites. These pages usually have names like index.html, welcome.html, or default.html. You can configure your web application to direct requests toward these pages by adding a welcome-file-list element to your web application's deployment descriptor. Set up a welcome file list in web.xml in the manner demonstrated by Example 3-12. The welcome-file-list element must come after any servlet and servlet-mapping elements, and precede any error-page or taglib elements in the servlet 2.3 deployment descriptor.

Example 3-12. Setting up welcome files in web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
           "http://java.sun.com/dtd/web-application_2_3.dtd"
>

<web-app>

<!-- Define servlets and servlet-mappings here -->

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

</web-app>

Whenever the servlet container encounters a URL for a web application that specifies only a directory, not a particular filename or servlet, then it looks for a welcome-file-list element ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required