Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»


A servlet container might be implemented as a separate operating-system process from the web server it communicates with, or it might be configured to run inside the web server’s process (figure 2.6). The model of communication between web servers and servlet containers is fairly general; as long as the container can manage servlets appropriately, hand them requests, and deliver their responses, servlet and JSP developers can be shielded from the details of the backend interaction between servlet containers and the generic web-server logic that drives them.

2.2.2 The anatomy of a servlet


Like HTTP, servlets are based on the request/response model. In fact, a servlet is essentially a Java class that implements a particular, formal interface for producing responses based on requests. The Java interface javax.serviet.serviet, which defines how servlets function, has a method that looks like


public void service(ServletRequest req, ServletResponse res)


The ServletRequest and ServletResponse interfaces, both in the javax.servlet package, represent the requests that a servlet processes and the responses that a servlet generates.


Because all servlets implement the javax.servlet.Servlet interface, each provides an implementation of the service() method. (Implementing a Java interface requires that a class provide implementations of all of the methods declared in that interface.) Therefore, all servlets have logic for processing a request and producing a response.


The Servlet interface has other methods that servlets must implement. JSP developers do not typically need to deal with these methods directly, but it may be useful to know that servlets can provide logic to handle initialization in an init() method and cleanup in a destroy() method.

Скачать в pdf «Web Development with JavaServer Pages»