Given by Nancy McCracken at Java Servlets on Spring Semester 1999. Foils prepared May 19 99
Outside Index
Summary of Material
A servlet is a Java program that is run by a Web server. |
Servlets follow a standard servlet API defining the interface between the server and the servlet, and are designed to work within a request/response model. The servlet API is part of the standard extensions of JDK. |
Servlets can provide all the services of standard CGI, but are platform-independent, i.e. they can be used with any server implementing the API. |
More generally, servlets play the role of providing middle-tier services between the client and the back-end applications. |
References:
|
Outside Index
Summary of Material
2/4/99 |
Nancy McCracken |
NPAC |
A servlet is a Java program that is run by a Web server. |
Servlets follow a standard servlet API defining the interface between the server and the servlet, and are designed to work within a request/response model. The servlet API is part of the standard extensions of JDK. |
Servlets can provide all the services of standard CGI, but are platform-independent, i.e. they can be used with any server implementing the API. |
More generally, servlets play the role of providing middle-tier services between the client and the back-end applications. |
References:
|
Middle-tier process:
|
Proxy server for applets to connect to other hosts. |
Protocol support other protocols: SMTP, POP, FTP, . . . |
Some servers allow the <servlet> tag in HTML, and the server will call the servlet to insert text into the outgoing HTML stream |
CGI scripts |
A temporary servlet is started when a request arrives and shut down after the response is generated. |
A permanent servlet is loaded when the server is started and lives until the server is shut down.
|
Being temporary or permanent is part of the server configuration. |
Install Java Servlet Development Kit (JSDK). This works with both JDK1.1 and JDK1.2 platforms. |
Servlets can be tested with the utility program servletrunner. |
Servlet API is a standard extension to the JDK under javax:
|
The servlet API provides support in 4 categories:
|
On the web server host machine, servlets run under the same process as the web server. |
The web server is responsible for creating an instance of the servlet and invoking standard methods from the interface (in a similar way to a browser invoking methods of an applet). There are three main methods:
|
and two helper methods:
|
public void init (ServletConfig config) |
The init method is invoked when the servlet is first started. For permanent servlets, this is when the server is started. |
The init method is called only once and is guaranteed to finish before any calls to the service method. |
The ServletConfig object passed as the parameter has a method getServletContext ( ) that returns a ServletContext with information about the servlet environment. |
public void service (ServletRequest req, ServletResponse res) |
Each request message from the client results in invoking the service method. |
There are two ways the client can send information:
|
Similarly, the servlet can invoke getOutputStream () to generate the response. Other methods, such as setContentType can set information that the web server will use in generating the output to the client. |
More than one instance of the service method can be invoked at one time to respond to multiple requests. |
Called when the servlet in unloaded to clean up any open resources. |
Although the server normally waits until all service calls are terminated to invoke destroy, it may not be possible, and your destroy method should make sure that resources are not being used. |
This servlet returns a page to the web browser: |
import java.io.*; |
import javax.servlet.*; |
public SampleServlet implements Servlet |
{ private ServletConfig config; |
public void init (ServletConfig config) throws ServletException |
{ this.config = config; } |
public void destroy ( ) { } // do nothing |
public ServletConfig getServletConfig ( ) |
{ return config; } |
public String getServletInfo() |
{ return "A simple servlet"; } |
public void service (ServletRequest req, ServletResponse res) |
throws ServletException, IOException |
{ res.setContentType ( "text/html" ); |
PrintWriter out = res.getWriter ( ); |
out.println ( "<html>"); |
out.println ( "<head>"); |
out.println ( "<title> A Sample Servlet </title>"); |
out.println ( "</head>"); |
out.println ( "<body>"); |
out.println ( "<h1> A Sample Servlet </h1>"); |
out.println ( "</body>"); |
out.println ( "</html>"); |
} |
} |
This information is passed to the servlet via the ServletConfig object parameter of the init method. |
Each web server has its own information tags defined. |
The getInitParameterNames ( ) returns an Enumeration of all the parameters available to the servlet on that server. |
A particular parameter is obtained by config.getInitParameter ( "timezone"); |
This information is available at any time through the ServerConfig object. (This is why you should save the ServerConfig parameter from the init method.): Servlet Context c = config.getServletContext ( ) |
The ServletContext object has methods:
|
The servlet gets information from the ServletRequest parameter either by a Stream:
|
or by getting individual parameter names:
|
There are additional methods for getting request information, such as:
|
There are two exception classes in the servlet API.
|
This subclass of Servlet provides an init method that takes care of saving the ServletConfig parameter. |
It provides several methods that use the ServletConfig object to return information. |
It also provides a dummy destroy method. |
This subclass of GenericServlet provides automatic separation of requests according to the HTTP type of request:
|
The service method of HTTPLservlet checks the type of request and calls one of several special methods if they are present:
|
doGet and doPost methods should read request data, set response headers and write response data. |
The doPost method is required to open an InputStream to get the parameters, however, it can call a method parsePostData, that returns a Hashtable with the names and values of the parameters. |
To create a cookie on the browser, you create an object of type Cookie and use res.addCookie( c) to send it to the browser. Cookies are retrieved by using methods getCookies, and getName and getValue for individual cookies. |
There is also a special HPPTSession class that implicitly uses cookies to allow you to save session information as name/value pairs in between requests. |
Most servers allow the server administrator to associate different levels of "trust" to servlets, usually depending on where they came from. The levels usually control the access to the file system and networking.
|
Servers may also allow Access Control Lists to be created which can allow different users of groups of users access to some web pages (or not). |
A web server may call the applet's service method for more than one request at a time by creating multiple threads.
|
If your service method uses outside resources, such as instance data from the servlet object, files, or databases, you must carefully consider what might happen under multiple calls to the service method, and use synchronization where appropriate. |
Main servlet support page at Sun: http://java.sun.com/products/servlet |
List of environments supporting servlets at http://java.sun.com/products/servlet/runners.html |
Information on the HTTP protocol at the World Wide Web Consortium's web site http://www.w3.org/Protocols |
Servlet Central web magazine http://www.servletcentral.com |
Jrun Magazine http://www.jrunmag.com |
Books:
|