In this chapter we will discuss the typical directory structure of a JSP web application and its deployment.
6.2 JSP Application Directory Structure
In any jsp web application we can have following components –
· Static contents like HTML.
· JSP (Java Server Pages) to generate dynamic content.
· External library or jar files.
· Any other java utility classes.
· web.xml also known as deployment descriptor
All web servers and container expects the web application to be available in a specific directory structure. Below is the typical directory structure
JSP Web Application Root Directory – This is the main or Root folder of web application. Usually name of this folder becomes your web application context. For example, if our web application name is jsp-tutorial , then folder name will be jsp-tutorial and web application will be accessible via http://localhost:8080/jsp-tutorial /jsp page.
WEB-INF- This is the special directory under web application root directory. This is special because this is secured folder and files available within this folder will not be accessible to client directly. Which means say if this directory has one file “index.html”, then this file cannot be accessed directly via http://localhost:8080/ jsp-tutorial /index.html.
WEB-INF/lib- All the required jar files or third party jar files will be placed inside this directory.
WEB-INF/classes- All the java code including servlets for the web application will go inside classes folder. We can also put our own classes into a JAR file, and place it here, rather than putting those classes in the classes directory.
web.xml – web.xml resides under WEB-INF folder and it is also known as deployment descriptor. All the configuration of web application like servlets configuration, filters configuration, welcome file list etc are configured in web.xml. We will discuss web.xml in detail.
META-INF/MAINFEST.MF- This is the manifest file
JSP File – All JSP files can be placed directly inside a root directory or inside sub folders of WEB-INF folder. When placed inside WEB-INF or its sub folders, jsp can not be accessed directly. While working with eclipse, we create the JSP files inside WebContent directory.
6.3 Web.xml (Deployment Descriptor)
This is the standard file for any web application and container came to know about all the detail of web application through this file only.
All configuration of all components of a web application are done in web.xml and it is placed directly under WEB-INF folder.
6.3.1 Context Parameters
Context parameters are not tied to any jsp or servlets instead these parameters are for the complete web application and can be accessed in any jsp or servlets. Below is the sample configuration of context parameter.
Since these parameters are not limited to any specific jsp, they are defined outside any tag but within <web-app> tag.
<context-param> <param-name>param</param-name> <param-value>the value</param-value> </context-param>
. (Refer Section 6.8 for example )
6.3.2 Welcome File List
Welcome file list tag of the web.xml file is used to specify the default page of a web application that will be displayed if none is specified. This tag can have any number of "<welcome-file>" tags.
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>home.jsp</welcome-file> </welcome-file-list>
With this configuration , if user hits http://localhost:8080/WebApplication then http://localhost:8080/WebApplication/index.jsp will be displayed. (Refer Section 6.8 for example )
6.4 Web Archive (WAR)
Once the web application is developed, it needs to be deployed on server. Sun specified a format known as WAR (web achieve) in which the web application has to be packaged.
Once the packaged war file is deployed to the server, it gets exploded and deploys to the server.
We can create a war file out of web application by executing below command using command prompt.(we need to run this command after navigating to the web application root directory)
jar cvf myapplication.war.
Above command says generate a war with name “myapplication.war” in a current directory (there is a . in the end of command).