Monday, May 16, 2011

Architecture of the Servlet Package


The javax.servlet package provides a set of interfaces and classes for writing servlets.


These classes and interfaces are normally found in 'Enterprise Edition' versions of the Java SDK. They are also included within TJI's ide.jar so that servlets can be developed by those without an EE SDK.
The architecture of the servlet package is described below.

The Servlet Interface

The central abstraction in the Servlet API is the Servlet interface. The Servlet interface declares, but does not implement, methods that manage the servlet and its communications with clients. All servlets implement this interface, either directly or, more commonly, by extending a class that does implement it, such asGenericServlet or HttpServlet.
For class GenericServlet, or a class extending it, the main method involved in client interaction is the service(ServletRequest, ServletResponse) method. For class HttpServlet, the default Http request type is 'GET' and the main method called to handle this is the method doGet(HttpServletRequest, HttpServletResponse).

An Example Servlet
A servlet can be as simple as this :
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
response.getWriter().println("Hello World !");
}
}

Client Interaction

When a servlet receives a call from a client, it receives two objects (provided by the Java web server):
  • A ServletRequest, which encapsulates the communication from the client to the server, and
     
  • A ServletResponse, which encapsulates the communication from the servlet back to the client.
ServletRequest and ServletResponse are interfaces defined by the javax.servlet package.

The ServletRequest Interface

The ServletRequest interface allows the servlet access to:
  • Information, such as the names of the parameters passed from the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it.
     
  • The input stream, ServletInputStream. Servlets can use this input stream to get data from the client when using an application protocol such as the http POST method.
Interfaces that extend the ServletRequest interface allow the servlet to retrieve more protocol-specific data. For example, the HttpServletRequest interface contains methods for accessing HTTP-specific header information, such as any cookies found in the request and the HTTP method type with which the request was made.

The ServletResponse Interface

The ServletResponse interface defines the servlet methods for replying to a client. It:
  • Allows the servlet to set the content length and MIME type of the reply.
     
  • Provides an output stream, ServletOutputStream, and a Writer through which the servlet can send the reply data. Using the writer is the simplest choice when the reply data is of MIME type 'text/html'.
Interfaces that extend the ServletResponse interface give the servlet more protocol-specific capabilities. For example, the HttpServletResponse interface contains methods that allow the servlet to set HTTP-specific header information.

The Additional Capabilities of HTTP Servlets

The classes and interfaces described above make up a basic Servlet. HTTP servlets have some additional objects that provide Session-tracking capabilities. The servlet writer can use these APIs to maintain state between the servlet and the client that persists across multiple connections during some time period. HTTP servlets also have objects that provide cookies. The servlet writer can use the Cookie API to save data with the client and to retrieve this data. See the 'Sessions Tracking' and 'Using Cookies' sections for more information on these topics.

No comments:

Post a Comment