Introduction to Servlets

The Java Servlet framework is a means of writing web server code in Java without the programmer having to deal with the HTTP protocol directly. Servlets are therefore a convenient means of generating dynamic web pages. Moreover, many web hosting companies nowadays provide cheap hosting plans that allow you to run servlets. They are able to do so even on fairly cheap plans because Java servlets are generally more efficient than some other ways of providing dynamic content, such as using perl scripts1.

To write, compile and run a Java servlet, you will need the following (in addition to your usual JDK and Java development tools):

Getting the Java Enterprise Edition

The Java Enterprise Edition is available from Sun's Java web site. Once installed, this will give you a jar called javaee.jar inside the lib folder. This jar contains (among other extensions) the javax.servlet package. You must include this jar in your classpath (or add it to your project if using an IDE) when compiling your servlet.

Hosting your servlet

In addition to a regular web server, you will need a servlet runner plugin. Luckily for beginners (and indeed experienced developers), many web hosting companies provide plans that are more or less ready to run servlets "out of the box". See this separate section on servlet hosting tips, which deals with how to choose a hosting company and the criteria you should consider if you decide to go down this route.

Writing your first servlet

On the next page, we look at how to write your first servlet, introducing the basic anatomy of a servlet class.


1. Note that the spelling servelet is mainly used by recruitment agencies and companies offering "client-oriented embedded web presence solutions".


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.