【正文】
dling requests from customers, and the results returned to the customer, as shown below: Figure 1: Calling the process of JSP pages 4 After another visit this page to the customer, as long as the paper there have been no changes, JSP engine has been loaded directly call the Servlet. If you have already been modified, it will be once again the implementation of the above process, translate, pile and load. In fact, this is the socalled first person to punishment. Because when the first visit to the implementation of a series of the above process, so will spend some time after such a visit would not. Java servlets offer a powerful API that provides access to all the information about the request, the session, and the application. bining JSP with servlets lets you clearly separate the application logic from the presentation of the application。 in other words, it lets you use the most appropriate ponent type for the roles of Model, View and Controller. Servlets, Filters, and Listeners A servlet is a Java class that extends a server with functionality for processing a request and producing a response. It39。s implemented using the classes and interfaces defined by the Servlet API. The API consists of two packages: the package contains classes and interfaces that are protocolindependent, while the package provides HTTPspecific extensions and utility classes. What makes a servlet a servlet is that the class implements an interface named , either directly or by extending one of the support classes. This interface defines the methods used by the web container to manage and interact with the 5 servlet. A servlet for processing HTTP requests typically extends the class. This class implements the Servlet interface and provides additional methods suitable for HTTP processing. Servlet Lifecycle The web container manages all aspects of the servlet39。s lifecycle. It creates an instance of the servlet class when needed, passes requests to the instance for processing, and eventually removes the instance. For an HttpServlet, the container calls the following methods at the appropriate times in the servlet lifecycle. Besides the doGet( ) and doPost( ) methods, there are methods corresponding to the other HTTP methods: doDelete( ), doHead( ), doOptions( ), doPut( ), and doTrace( ). Typically you don39。t implement these methods。 the HttpServlet class already takes care of HEAD, OPTIONS, and TRACE requests in a way that39。s suitable for most servlets, and the DELETE and PUT HTTP methods are rarely used in a web application. It39。s important to realize that the container creates only one instance of each servlet. This means that the servlet must be thread safe able to handle multiple requests at the same time, each executing as a separate thread through the servlet code. Without getting lost in details, you satisfy this requirement with regards to instance variables if you modify the referenced objects only in the init( ) and destroy( ) methods, and just read them in the request processing methods. 6 Compiling and Installing a Servlet To pile a servlet, you must first ensure that you have the JAR file containing all Servlet API classes in the CLASSPATH environment variable. The JAR file is distributed with all web containers. Tomcat includes it in a file called , located in the mon/lib directory. On a Windows platform, you include the JAR file in the CLASSPATH. . Reading a Request One of the arguments passed to the doGet( ) and doPost( ) methods is an object that implements the HttpServletRequest interface. This interface defines methods that provide access to a wealth of information about the request. Generating a Response Besides the request object, the container passes an object that implements the HttpServletResponse interface as an argument to the doGet( ) and doPost( ) methods. This interface defines methods for getting a writer or stream for the response body. It also defines methods for setting the response status code and headers. Using Filters and Listeners The servlet specification defines two ponent types beside servlets: filters and listeners. These two types were introduced in the Servlet specificatio