Java Reference
In-Depth Information
Using Servlets
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 informa-
tion 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
n
Receiving information from arguments specified as part of a uniform resource
locator (URL)
n
Running programs on the computer that runs the web server
n
Storing and retrieving cookies , files that store a user's preferences and other infor-
mation on his computer
n
Sending data back to a user in the form of an HTML document, GIF graphic, or
another format
n
Java servlets can do all these things along with some behavior that's difficult to imple-
ment 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.
 
Search WWH ::




Custom Search