Introduction to Servlets
In this section we look at using servlets and how to use them. You need to have an understanding of Java to use servlets, so if you're completely new to Java I suggest going to the Java section of the site and doing the lessons there first. So before we get started, lets define what a servlet is and what it's used for. We can think of a servlet as a Java class which we can use to extend the capabilities of an application server. Servlets have the capability to respond to any types of request but are commonly used to extend applications hosted by web servers such as Tomcat. Servlets give us the ability to serve dynamic web content to users from a container within our web server and are generally used with the HTTP protocol.
Before we get started with the lessons, the following link will take you to the download page for the latest specification for the 2.5 release which you can download and read at your leisure, JSR-154 specification.
For us to use servlets we need a Java
JDK and a web container to host our servlets on. The following table shows the correalation between the Servlets, JSP, Tomcat and Java versions used:
|Servlets||JSP||Tomcat Version||Minimum Java Version|
Getting The Java Software Development Kit (
Ok, before we delve into servlets programming we need a Java
JDK to compile our 2.5 servlets. java is the minimum Java version required to run Servlets, but I suggest downloading the latest
version of the
JDK for use in these lessons. The latest version at the time of writing was Java SE 7u7. The download page can be found at this link
Oracle JDK Download. The JDK includes the Java Runtime Environment (JRE) which you must have on your system to run Java
applications. The JRE includes the JVM and some other library files and as such creates a JVM to run our Java applications at runtime.
The download comes in Windows, Mac, Solaris and Linux flavours, so after accepting the license agreement, just download the appropriate
JDK for your
system. When you install the download you get the option of choosing where to put the
JDK on your hard drive. I am just using the default directory
provided by Oracle, which in my case on my Windows 7 system is C:\Program Files\Java\jdk1.7.0_07\. Where you put the
up to you but remember the path for a bit later in this lesson. I put the JRE in the default directory as well when prompted which in my case was
C:\Program Files\Java\. You can register with Oracle after the download has completed for product updates etc.
Setting A Classpath For The Java Compiler
Now we have the
JDK installed we need to add a classpath entry to the Path environment variable for where the Java compiler command
(javac) resides. First lets open a command line prompt and type in javac. Below is a screenshot of what happens when we do
this on my Windows7 system.
The javac command lies within the bin directory of the filepath where the
JDK was installed which in my case is
C:\Program Files\Java\jdk1.7.0_07\bin\ and so we need to add this classpath to our environment variables. Below is a screenshot of how this is
achieved on my Windows7 system and of course other OS will vary. But for Windows From Control Panel, select System >> Advanced System Settings and at the bottom
of the window select Environment Variables. Edit the system variable "Path" and add C:\Program Files\Java\jdk1.7.0_07\bin\ (or whatever you
selected as your filepath to the
JDK) to the end seperating this classpath from the last with a semi-colon.
Once you have done this press OK on the windows to close them all and reopen the command line prompt and type in javac. Below is a screenshot of what happens when we do this on my Windows7 system after adding the classpath. As you can see the javac command now brings up a list of options showing it is now on our classpath and can be used to compile our Java servlets.
Getting A Servlet Container
Ok, we now have a
JDK installed and have set a classpath to this to compile our Java servlets, but we still a container to hold our 2.5 servlets in. Apache Tomcat is an open source software implementation
of the Java Servlet and JavaServer Pages technologies that we use as a container for our servlet classes and JSP pages. So we need to download, verify and install a compatible version of Tomcat for this purpose.
Instructions for doing this can be found in the Tomcat section of the site.
Are We Going To Use An Integrated Development Environment? (IDE)
You could use an IDE such as Eclipse or Netbeans for developing and compiling your programs in these lessons, but doing so will automate some of the processes that we need to learn to get a real grip of Java Servlets. So my suggestion is to use a command-line editor for now so we can see all the processes involved without them being abstracted away beneath the bonnet of an IDE.
Choosing A Text Editor
As we are not using an IDE, we need a simple text editor such as Notepad or Wordpad to write our code. For these lessons we will be using Notepad but the choice is dependant on your preference and OS of course.
Java comes with very rich documentation that you will go back to time and again. The following link will take you to the online version of documentation for the JavaTM 2 Platform Standard Edition 5.0 API Specification. I suggest adding this link to your browser's favourites toolbar for fast access.
Lesson 1 Complete
In this lesson we downloaded the tools necessary for setting up our environment to begin coding using Servlets.
Now we have the basic environment set up and before we start working with servlets we need to have an understanding of the HTTP protocol and how information is passed to and from a web server. In this lesson we take a look at HTTP, the methods used and other useful information.