2.2 Getting Started With Servlet


In the following section we are going to discuss how to setup the development environment. How to configure
the server and how to write and run your first servlet.

Downloading and install Java SE 6 (including JRE)
You probably already have the JDK installed, but if not, installing it should be your first step.
Version 2.4 of the servlet API and version 1.2 of the JSP API require the Java 2 platform (standard or enterprise
edition).
If you aren't using J2EE features like EJB or JNDI, I recommend that you use the standard edition, JDK 1.3 or
1.4.
You can download Java from the link below http://www.java.com/en/download/index.jsp

Download Tomcat
Your second step is to download and install a server that implements the Java Servlet 2.4 and JSP 1.2
specifications for use on your desktop.
Apache's free Tomcat server will be suitable choice.

2.2 Getting Started With Servlet


You can download Tomcat from the link below. The latest version of Tomcat is Tomcat 7.0
http://tomcat.apache.org.
Figure 2.5: Tomcat Directory


2.2 Getting Started With Servlet


Table 2.1 Tomcat Directories/Files Description


Configure the server
Most of servers use a nonstandard default port in order to avoid conflicts with other web servers that may be
using the standard port (80).
However, if you are using the servers in standalone mode and have no other server running permanently on port
80, you will find it more convenient to use port 80.

2.2 Getting Started With Servlet


That way, you don't have to use the port number in every URL you type in your browser (Hall, More Servlets
and JavaServer Pages 2001).
If you want to change tomcat port number here are the steps:
Locate server.xml in {Tomcat installation folder}\ conf \
Find the following lines:
Change the Connector port="8080" port to any other port number (e.g. port 8181)
Save the server.xml file and Restart Tomcat.
The most critical Tomcat setting is the JAVA_HOME environment variable.
Failing to set it properly prevents Tomcat from handling JSP pages.
This variable should list the base JDK installation directory, not the bin subdirectory (Hall and Brown 2004).

2.2 Getting Started With Servlet


To set the JAVA_HOME variable (java.com n.d.):
Right-click the My Computer icon on your desktop and select Properties.
Click the Advanced tab.
Click the Environment Variables button.
Under System Variables, click New.
Enter the variable name as JAVA_HOME.
Enter the variable value as the installation path for the Java Development Kit.
If your Java installation directory has a space in its path name, you should use the shortened path name
(e.g. C:\ Program Files\Java\jre6) in the environment variable instead.
Click OK.
Click Apply Changes.
If the changes do not take effect even after reopening the command window, restart Windows.

2.2 Getting Started With Servlet


Test Tomcat setup
Start tomcat.
Open browser and type http://localhost:8080 then figure 2.6 will be displayed.
Figure 2.6: Tomcat home page


2.2 Getting Started With Servlet


Writing your first Servlet
Open any text editor and type the program in figure 2.3, and then compile it like any other Java Program using
java command.
The class file should be generated successfully.

Deploying and running your Servlet
Create a root directory for the application under the C:\Tomcat 7.0\webapps directory (e.g. C:\Tomcat 7.0
\webapps\Servlet)
Create a WEB-INF and WEB-INF\classes directories inside the Servlet Directory.
Make a directory called com inside WEB-INF\classes.
Make a directory called topictwo inside WEB-INF\classes\com directory as our Hello Servlet is inside this
package.
Put the class file of the servlet inside WEB-INF\classes\com\topictwo directory.
Write the Deployment Descriptor web.xml and put it under the WEB-INF directory.

2.2 Getting Started With Servlet


Deployment Descriptor for Hello World Servlet
The Deployment Descriptor (DD) is explained in next section, but for now, just notice four points:
There is one DD per web application.
A DD can declare many servlets.
A <servlet-name> ties the <servlet> element to the <servlet-mapping> element.
A <servlet-class> is the Java class.
A <url-pattern> is the name the client uses for the request.
Figure 2.8: Deployment Descriptor for Hello World Servlet


2.2 Getting Started With Servlet


Running Hello World Servlet
Start tomcat.
Open browser and type http://localhost:8080. The Home Page of Tomcat should open.
Type http://localhost:8080/Servlet/Hello in the address bar of your browser.
The Servlet should run and display the famous greetings in programming
Figure 2.9: Hello World servlet result