Servlets



Servlet Essentials

There are 3 parts to working with a Servlet:

1. Understand what it is does
A servlet is a piece of Java functionality (with the ground for it given to you by Sun Microsystems as the javax.servlet and javax.servlet.http packages) - that accepts requests over a network and replies with responses. Everywhere we write "requests" and "responses" we could write "HTTP Requests" and "HTTP Responses - because that by default is what it is used for. That's what it is. This should be easy to understand.

2. Write and Run a "Hello World" servlet

Make sure Tomcat is installed on your machine.

Make sure you have an Environment variable CLASSPATH that includes the servlet-api.jar file in the common\lib directory of your Tomcat installation. The CLASSPATH variable should also include the root directory of your Java installation, and also a period at the end to let you find the classes in your current directory.

Create a directory c:\work

Copy the following file into it: HelloWorld.java

Compile the above file.

If you get the following error:

C:\work> javac HelloWorld.java
HelloWorld.java:2: package javax.servlet does not exist
import javax.servlet.*;
^
HelloWorld.java:3: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
HelloWorld.java:5: cannot resolve symbol
symbol  : class HttpServlet
location: class HelloWorld
public class HelloWorld extends HttpServlet

it will be because your CLASSPATH variable does not include servlet-api.jar file.

If your computer does not recognize the javac or java command it will be because your PATH variable does not include your (JAVA_HOME)\bin directory. You must include this directory in the PATH in order to call java commands from your current directory.

We assume now that you have a compiled HelloWorld.java file.

Now create a directory "helloservlet" in the "webapps" directory of your Tomcat root.

Create a subdirectory : "WEB-INF" in the above directory.

Put the following file : web.xml in that "WEB-INF" directory.

Create a subdirectory : "classes" in that "WEB-INF" directory. In this directory put the compiled HelloWorld.java file : HelloWorld.class .

Now you are set to go.

Start Tomcat (see the Tomcat page on this site if you want to know how).

To deploy your new servlet, go to the Tomcat Web Application Manager and under the Deploy section, put the "WAR or directory URL" as "helloservlet". This is good for Tomcat 5.0.28 - in other versions of Tomcat, it should be something similar. When you do this, you should see the "Servlet Display Name" as "Hello World Servlet" in the Applications section just above the Deploy section.

Open the URL: http://localhost:8080/helloservlet/helloworld.do in your web browser.

You should see the text "Hello World" at the top of your browser window.

A brief explanation of the web.xml file is given below:

<display-name>Hello World Example</display-name>
(lets you identify your servlet in the Tomcat Manager)

<description>
    Hello World Example
</description>   
(description for understanding this servlet within a web.xml file)

<servlet>
    <servlet-name>
            MyHelloWorld
    </servlet-name>
    <servlet-class>
            HelloWorld
    </servlet-class>
</servlet>
(Associates the name MyHelloWorld with the class name of the servlet which is HelloWorld)

<servlet-mapping>
    <servlet-name>
            MyHelloWorld
    </servlet-name>
    <url-pattern>
            helloworld.do
    </url-pattern>
</servlet-mapping>
(Associates the name MyHelloWorld with the URL that is entered by the user which is helloworld.do in this case. This is how a URL by the user gets mapped to a Java servlet class in this system).

3. Servlets as self contained application modules

The following link on the Sun Microsystems website is the simplest description on how to make Servlets function as self contained application modules called "war files" (web archive files). It is worth looking at:

http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WCC3.html

Books

The best book I have come across on Servlets is
Developing Java Servlets
by James Goodwill
(who is also the author of the best book I have come across on JSP ).

If you are willing to buy these books it is to your benefit as a J2EE developer. I have found it very economical to buy used ("pre-owned") books from Amazon and for a few dollars or a few cents have picked up some outstanding titles in excellent condition..

Servlets and JSP constitute the inner-most core of J2EE along with JDBC. With these three technologies, you can do anything that is done in and with J2EE.

The first four chapters of Developing Java Servlets contain all the material that is relevant - you don't have to read the whole book to get started.