Getting started: Setting Up Web Project
This series of posts will describe creation of web applications with Java language, using servlets and JSP (Java Server Pages). We will assume that user has basic knowledge of Java and Java SE libraries.
Servlets represent small Java programs which run on server and handle HTTP requests from clients. These are in some way similar to CGI scripts, i.e. they provide some sort of business logic which can be applied to a specific request. The advantage is that they are much simpler to implement and offer consistent API.
Java Server Pages are kind of dynamic HTML pages which can be used to display dynamic content. They provide custom expression language and variables to facilitate data representation.
Creating project with Maven
The first thing to do is to set up the project. We will use Maven as a build tool for this project, so we will use Maven archetype to create skeleton for our project (Maven installation instructions can be found here).
To begin, simply type the following command into console:
You will be presented with list of available archetypes. Choose
maven-archetype-webapp and then simply enter required information into console. Maven will create standard Maven project structure.
Project Structure Description
Maven will create a directory structure for Java web application project. This is the standard Maven project hierarchy, which we will not discuss here (for more information on Maven refer to http://maven.apache.org). Instead, we will focus on
src/main/webapp directory, which contains the files required for Java web application deployment. The image below shows contents of webapp directory:
WEB-INF directory is expected to appear in each web application. It contains the files needed for application at runtime, and also contains deployment descriptor file (web.xml). During application build, two new directories will be created inside WEB-INF: lib and classes. Directory classes will contain compiled java code for the application, while lib contains all dependencies (ie. Classpath).
The web.xml file is called deployment descriptor, and is vital part of the web application. It contains data needed by application server to correctly deploy and start the application. This file must always be called web.xml and its content is defined by Java servlet specification.
Finally, index.jsp is the welcome file, ie. the file shown by default when user enters application URL without specific page (eg. http://localhost:8080/samplewebapp). This is JSP file that is generated by Maven, and contains a simple “Hello, world” message.
Now, to try out the application, simply run the following command:
This will start Tomcat server on default port 8080. To test the application, enter the following URL into your browser:
You should get the following result:
Application Deployment Details
As mentioned above, the most important file is web.xml, which contains application deployment information. The Maven-generated file is quite simple, and its content is shown here:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<display-name>Archetype Created Web Application</display-name>
This deployment descriptor contains only one information, application display name. This is the name shown in server's administration console.
We will go into more details of deployment descriptor in the following chapters. For now, this will suffice.
One other thing to note is the context path. Context path is the part of URL after server and port, i.e. samplewebapp in this case. Each Java web application must have it's own unique context path. In Maven, default context path is application name. This can be overriden by specifying custom server settings, but it is out of scope of this tutorial. For more information about this, refer to Tomcat web site (http://tomcat.apache.org)
In the next chapter, we will create our first servlet.
Find the sample web application here.