Servlets are Java classes run by a web server that has an interpreter that supports the Java Servlet specification. This interpreter, which is called a servlet engine, is optimized to run servlets with a minimum of the server’s resources. Java servlets often serve the same purpose as programs implemented using the common gateway interface (CGI), a protocol for writing software that sends and receives information through a web server. CGI programming has been supported on the web for most of its existence. Most CGI programs, which also are called CGI scripts, have been written using languages such as Perl, Python, and PHP. CGI programs are used often for these purposes:
· Collecting user input from a form on a web page
· Receiving information from arguments specified as part of a uniform resource locator (URL)
· Running programs on the computer that runs the web server
· Storing and retrieving cookies, files that store a user’s preferences and other information on his computer
· Sending data back to a user in the form of an HTML document, GIF graphic, or another format
Java servlets can do all these things along with some behavior that’s difficult to implement using most CGI scripting languages. Servlets offer full support for sessions, a way to keep track of a particular user over time as a website’s pages are being viewed. They also can communicate directly with a web server using a standard interface. As long as the server supports Java servlets, it can exchange information with those programs. Java servlets have the same portability advantages as the language itself.
Although Sun’s official implementation of servlets was created with the Apache Software Foundation— the open source developers who created the Apache web server—many other companies and groups have introduced tools to support Java servlets such as IBM WebSphere, BEA WebLogic, and the Jetty server. Servlets also run efficiently in memory. If 10 people are simultaneously using the same CGI script, a web server will have 10 copies of that script loaded into memory. If 10 people are using a Java servlet, only one copy of the servlet will be loaded, spawning threads to handle each user. Servlets can be created using the javax.servlet and javax.servlet.http packages, which are a standard part of the Java’s enterprise edition, an expanded version of the Java class library that supports large-scale development projects.
These classes implement the Java Servlet and JSP specifications. At this time, the current versions are Java Servlet 2.5 and JSP 2.1. For users of the standard Java edition, which has been covered throughout this book, the servlet packages can be downloaded from Sun’s Java servlet site at http://java.sun.com/products/servlet. Click the Downloads link and under the Specifications heading, choose the Download Class Files link for the Java Servlet specification version 2.3 (class files for newer versions also might be available). After installing the class files, to make them available, add the root folder of the installation to your computer’s Classpath environmental variable (for example, if the javax folder was saved in c:\java\servlet-2.3, add the c:\java\servlet-2.3 folder to the Classpath).