Servlets
Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend the functionality of the Web server. Servlets can:
generate HTML dynamically to be returned to the client in response to some request act as a communication gateway to other resources on the server, e.g. databases, other applications, etc.
Servlets (2)
coordinate connections from a number of clients open a separate connection from the server to an applet on the browser using a "state-full" protocol, e.g. IIOP, RMI, TCP/IP provide additional (customized) processing for the server's standard routines filter data to be delivered to client applet
Servlets (3)
Servlets are a superior solution over alternative technologies, e.g. CGI and server specific APIs:
Better performance (same address space); no, per request, process startup time Written in Java -- better performance than scripting languages, e.g. PERL or Unix shell scripts More portable (write-once run-on-any-server) Stronger security model
Servlet Lifecycle
The lifecycle of a servlet begins when it is loaded into a Web server and ends when it is terminated or is reloaded. When a servlet is loaded, the server creates an instance of the servlet and calls the servlet init() method. A servlet may be loaded:
automatically at server startup at first client request when a servlet is reloaded
HttpServletRequest Objects
Provides access to HTTP header data (e.g., cookies found, HTTP method, arguments that the client sent as part of the request). To access client data:
The getParameter method returns the value of a named parameter. If your parameter could have more than one value, use getParameterValues instead. The getParameterValues method returns an array of values for the named parameter. (The method getParameterNames provides the names of the parameters.)
HttpServletResponse Objects
An HttpServletResponse object provides two ways of returning data to the user:
The getWriter method returns a Writer
Use the getWriter method to return text data to the user
Closing the Writer or ServletOutputStream after you send the response allows the server to know when the response is complete.
Session Tracking
Session tracking is a mechanism that servlets use to maintain state about a series of requests from the same user (that is, requests originating from the same browser) across some period of time. Sessions are shared among the servlets accessed by a client. This is convenient for applications made up of multiple servlets. To use session tracking,
Get a session (an HttpSession object) for a user. Store or get data from the HttpSession object. Invalidate the session (optional).
Obtaining a Session
The getSession method of the HttpServletRequest object returns a user's session. When you call the method with its create argument as true, the implementation creates a session if necessary. To properly maintain the session, you must call getSession before any output is written to the response.
References
Mary Campione, Kathy Walrath, The Java Tutorial Second Edition: Object-Oriented Programming for the Internet, AddisonWesley, 1998. IBM Introduction to Servlets, Online tutorial, 1999.