You already know that if you type in the name of a web site and you don’t specify a specific file, you (usually) still get something back. Entering http://www.oreilly.com into your browser takes you to the O’Reilly web site, and even though you didn’t name a specific resource (like “home.html”, for example), you still get a default page.
You can configure your server to define a default page for the entire site, but we’re concerned here with default (also known as “welcome”) pages for individual web apps. You configure welcome pages in the DD, and that DD determines what the Container chooses when the client enters a partial URL—a URL that includes a directory, for example, but not a specific resource in the directory.
In other words, what happens if the client request comes in for:
http://www.wickedlysmart.com/foo/bar
and “bar” is simply a directory, and you don’t have a specific servlet mapped to this URL pattern. What will the client see?
In the DD:
Imagine you have a web app where several different directories have their own default HTML page, named “index.html”. But some directories use a “default.jsp” instead. It would be a huge pain if you had to specify a specific default page or JSP for each directory that needs one. Instead, you specify a list, in order, of the pages you want the Container to look for in whatever directory the partial request is for. In other words, no matter which directory is requested, the Container always looks through the same list—the one and only <welcome-file-list>.
The Container will pick the first match it finds, starting with the first welcome file listed in the <welcome-file-list>.
Multiple welcome files go in a single DD element.
No matter how many welcome files you might list, you put them ALL into a single entry in the DD: <welcome-file-list>. It’s tempting to think that each file might go in a separate <welcome-file-list> element, but that’s not how it works! Each file has its own <welcome-file> element, but you put ALL of them within a single <welcome-file-list>.
The files in the <welcome-file> element do NOT start with a slash!
Don’t be confused. The way in which the Container matches and chooses welcome files is not the same as the way in which it matches URL patterns. If you put the slash in front of the file name, you’ll be violating the spec, and bad things will happen.
BE the Container
Which welcome files will the Container choose given the DD and the client requests shown? You can expect something like this on the exam.
The DD:
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
Directory structure:
BE the Container: Answers
The DD:
<welcome-file-list> <welcome-file><b>index.html</b></welcome-file> <welcome-file>default.jsp</welcome-file></welcome-file-list> </welcome-file-list>
Directory structure:
Requests:
http://localhost:8080/MyTestApp/
Container choice: MyTestApp/index.html
http://localhost:8080/MyTestApp/registration/
Container choice: MyTestApp/registration/index.html
http://localhost:8080/MyTestApp/search
Container choice: MyTestApp/search/default.jsp (If there HAD been both a default.jsp and an index.html in the “search” directory, the Container would have chosen the “index.html” file, since it is listed first in the DD.)
http://localhost:8080/MyTestApp/registration/newMember/
Container choice: When no files from the <welcome-file-list> are found, the behavior is vendor-specific. Tomcat shows a 404 Not Found error. Another Container could show a directory listing for the newMember directory, which shows the “foo.txt” file.