Pages

Wednesday 17 March 2010

Explicitly Specifying Welcome Pages

A welcome page refers to the page that appears when you specify a URL that is a directory. On UNIX systems, an example is /usr/local/ In web applications, this translates to http://localhost:8080/, http://localhost:8080/restricted/ and on and on and on.

The servlet specification states that the server should first locate the index.jsp page. If unavailable, then the index.html page. Now the servlet specification does not specify what the servlet/ web container should do if the index.jsp and/ or the index.html pages are unavailable. The next action of the server is therefore server specific. Some servlet containers simply throw an HTTP 404 error, others list the contents of that directory. Such divergent behaviour of servlet containers from the various vendors may make your application behave differently on different containers (much like the behaviour of programs running on different platforms such as UNIX and Windows without any code changes that were developed using other programming languages such as C). This definitely makes your web application much less portable.


The purpose of specifying the welcome pages in the web.xml file is to ensure uniform behaviour of your web application across all these different containers keeping the promise of the Java programming language and platform of Write Once and Run Anywhere (WORA). There are ways in which such diverse behaviour of the web servers can be overcome.

  1. One way is to explicitly create the index.jsp file in that directory or the index.html page or both.

  2. The other way is to specify the welcome page using the welcome tags of the web.xml file.
The first approach is self explanatory. We will discuss the second approach instead.

In order to specify the resource that should be returned when a visitor of your site or user of your web app, you should consider the <welcome-file-list> tag of the web.xml file. This tag has an opening and closing tag. Do not forget the closing tag How this works is like so;

1:                 <welcome-file-list>
2: <welcome-file>firstPage.jsp</welcome-file>
3: <welcome-file>secondPage.html</welcome-file>
4: <welcome-file>thirdPage.jsp</welcome-file>
5: </welcome-file-list>

The servlet container will be obliged to display firstPage.jsp first; if firstPage.jsp suddenly becomes unavailable for some sinister reason, then secondPage.html will be displayed instead. If secondPage conspires to go AWOL as well, the server loads up thirdPage.jsp. This helps in making issues explicit and not having to rely on server specific behaviour which in some cases may not be merciful. Consider the situation where the index.jsp and index.html files are unavailable and the server displays the contents of the directory as if it were on ftp. Also the server throwing a HTTP 404 Error. Such behaviour is definitely unacceptable.


No comments: