Java Servlet API Description Document (2.1A) (1)

xiaoxiao2021-03-06  37

introduction

This is a documentation about version 2.1 Java Servlet API, as a supplement to this document, you can download the javadoc formatted documentation in http://java.sun.com/products/serve/index.html.

Who needs to read this document

This document describes the latest version version 2.1 of the Java Servlet API. Therefore, this book applies to the developers of servlets and servlet engines.

Composition of Java Servlet API

The Java Servlet API is composed of two packages: one is a package corresponding to HTTP, and the other is a universal package that does not correspond to HTTP. These two packages exist while the Java Servlet API can adapt to future other requests-responding protocols.

This document and documents that have just mentioned Javadoc formats describe these two packages, and the documentation in Javadoc format also describes how you should use all methods in these two packages.

Specification

You may also be interested in the following Internet specifications, which will directly affect the development and implementation of the Servlet API. You can find all of these RFC specifications mentioned below from http://info.internet.isi.edu/7c/in-notes/rfc/.cache.

RFC 1738 Unified Resource Locator (URL)

RFC 1808 related unified resource locator

RFC 1945 Hypertext Transfer Protocol - HTTP / 1.0

RFC 2045 Multipurpose Internet Mail Extension (Multi-Used Internet Save Extension Protocol (MIME)) Part 1: Internet Information Format

RFC 2046 Multipurpose Internet Mail Extension (Multi-Used Internet Mail Expansion Protocol (MIME)) Part 2: Media Types

RFC 2047 Multi-Used Internet Mail Extension Protocol (MIME) Part III: Information Title Extensions For non-ASCII text

RFC 2048 Multipurpose Internet Mail Extension (Multi-Used Internet Mail Expansion Protocol (MIME) Part 4: Registration steps

RFC 2049 Multipurpose Internet Mail Extension (Multi-Used Internet Mail Expansion Protocol (MIME)) Part 5: Consistency Standards and Examples

RFC 2068 Hypertext Transmission Protocol - HTTP / 1.1

RFC 2069 A extended HTTP: Summary Access Identification

RFC 2109 HTTP Status Management Mechanism

Use and explanation of RFC 2145 HTTP version number

RFC 2324 Super Text Coffee Pot Control Protocol (HTCPCP / 1.0)

The World Wide Web Association (http://www.w3.org) manages the specifications and execution of these protocols.

Java servlets

JavaTM Servlets is a Java applet that is not subject to platform, which can be used to expand a web server with a variety of ways. You can understand the Applets on the Server, which is compiled into a zona code so that it can be dynamically loaded and efficiently expanded to extend the processing power of the host.

Different parts of Servlets and Applets are that it does not run on a web browser or other graphical user interface. The servlet is running in the web server through the servlet engine to perform a request and response, a typical example of the request, and a response is an HTTP protocol.

A client program can be a web browser, or non-other programs that can connect to the Internet, which will access the web server and issue a request. This request is running the servlet engine on the web server and returns a response to servlet. The servlet forwards this response to the client via HTTP.

In terms of function, servlet is a bit similar to CGI, NSAPI, but different from them is that servlet has platform independent.

Java Servlet Introduction

Servlet has the following advances with other ordinary Server extension:

Because it uses different process processing modes, it is faster than CGI. It uses the standard APIs supported by many web servers.

It inherits all the advantages of Java, including easy upgrade, and platform-independent.

It can call the functional modules of a large number of APIs provided by Java.

This document illustrates the method of the class and interface of the Java Servlet API. For more information, see the API description below.

SERVLET lifecycle

A Java Servlet has a lifecycle. This lifecycle defines how a servlet is loaded and initialized, how to receive requests and make a response to requests, how to be removed from the service. The lifecycle of the servlet is defined by the interface of javax.servlet.servlet.

All Java servlets will execute the javax.servlet.servlet interface directly or indirectly so that it can run in a servlet engine. The servlet engine is a Web server that is customized by the Java Servlet API. The Servlet engine provides network services that can understand the MIME request and provide a container running servlet.

The Javax.Servlet.Servlet interface defines the method of a specific time in the lifecycle of the servlet and the specific order.

Servlet analysis and loading / R

The servlet engine is analyzed and loaded into a servlet, which can occur when the engine is started, requiring a servlet to respond to requests, and at any time there.

The servlet engine uses the Java class load tool to load a servlet, and the servlet engine can load the servlet from a local file system, a remote file system, and the network.

SERVLET initialization

After the servlet engine loads Servlet, the Servlet engine must initialize the servlet. In this process, you can read some fixed storage data, initialize the JDBC connection, and establish a connection to other resources.

In the initialization process, the init () method of the Javax.serveT.Servlet interface provides the initialization information of the servlet. This way, servlet can configure it yourself.

The init () method obtains a servletconfig. This object is executed in the servlet engine and allows servlets to be related parameters from it. This object allows the servlet to access the ServletContext object.

Servlet Process Request / R

After servlet is initialized, it can handle a request from the client, each request from the client being described as a servletRequest object, and the SERVLET response is described as a servletResponse object.

When the client issues a request, the Servlet engine passes a servletRequest object and a servletResponse object, which is passed as a parameter into the service () method.

The servlet can also perform the servletRequest interface and servletResponse interface. The servletRequest interface allows servlets to use the requests issued by the client. The servlet can read the request information through the ServletInputStream object.

The servletResponse interface allows the servlet to establish a response header and status code. By performing this interface, servlet has the right to use the ServletOutputStream class to return data to the client.

Multi-threaded and mapping / R

In multi-threaded environments, the servlet must handle many simultaneous requests. The exception is that this servlet executes a SingLethReadModel interface. If so, servlet can only process a request at the same time.

The servlet responds to the client's request in accordance with the mapping of the Servlet engine. A mapping pair includes a servlet instance and a URL that returns data, for example: helloservlet with /hello/index.html. However, a mapping may be composed of a URL and a number of servlet instances, such as: a distributed servlet engine may run in more than one server, in which, there may be a servlet instance in each server to balance the process Load. As a servlet developer, you can't assume that a servlet has only one instance.

Servlet's uninstall

The Servlet engine does not have to ensure that a servlet is loaded at any time or at any time on the service. The servlet engine can freely use or clear a servlet at any time. Therefore, we cannot rely on a class or instance to store important information.

When the Servlet engine decides to uninstall a servlet (for example, this engine must allow the servlet to release the resources being used and store the relevant information. In order to complete the above work, the engine calls the servlet's Destroy () method.

Before uninstalling a servlet, the Servlet engine must wait for all service () methods to complete or timeout (the servlet engine will define the timeout). When a servlet is uninstalled, the engine will not send any requests to the servlet. The engine must release servlet and complete the collection of useless storage units.

Servlet Mapping Technology / R

As a developer of a servlet engine, you must have a lot of adaptability to how to map the client's request to servlet. This document does not specify how the mapping occurs. However, you must be able to freely use all of the following techniques:

Map a servlet to a URL

For example, you can specify a special servlet it is only called only from /FEEDBACK/index.html.

Map a servlet to all URLs starting with a designated directory name

For example, you can map a servlet to / catalog, so requested from / catalog /, / catalog / garden and /catalog/HouseWares/index.html will be mapped to this servlet. But requests from / catalogtwo or /catalog.html are not mapped.

Map a servlet to all URLs ending with a specific field

For example, you can map a request from all in IN.THTML to a specific servlet.

Map a servlet to a special URL / servlet / servlet_name.

For example, if you have established a servlet called Listtributes, you can access this servlet by using / servlet / listattributes.

Call servlet by class name

For example, if the Servlet engine receives a request from the /Servlet/com.foo.servlet.mailServlet, the Servlet engine will load this com.foo.servlet.mailServlet class, create an instance, and processes the request through this servlet.

Servlet environment

The ServletContext interface defines a servlet environment object, which defines a view of a servlet on the Servlet engine. By using this object, the servlet can log events to get resources and get classes from the Servlet engine (for example, the RequestDispatcher object). A servlet can only run in a servlet environment, but different servlets can have different views on the Servlet engine. If the Servlet engine supports virtual hosts, each virtual host has a servlet environment. A servlet environment cannot be shared between the virtual host.

The Servlet engine allows a servlet environment to have its own range of activities.

For example, a servlet environment is a Bank application, which will be mapped to the / Bank directory. In this case, a call to the GetContext method returns / bank's servlet environment.

HTTP session

HTTP is an unstormal agreement. To create an effective web service application, you must be able to identify a unique request from the remote client from the remote. Over time, many session tracking technologies have been developed, but it is more troublesome.

The Java Servlet API provides a simple interface that allows the servlet engine to track the user's session.

Establish a session

Because HTTP is a request-response protocol, a session is considered a new session before the client is joined. Joining means returning session tracking information into the server, pointing out that the session has been established. Before the client is joined, we cannot judge that the next client request is part of the current session.

In the case, Session will be considered a new session.

The client's session does not know before this.

Client selection does not add session, for example, if the client refuses to receive cookie from the server

As a servlet developer, you must decide whether your web application handles the client does not join or cannot join the session. The server maintains a session object within the time specified by the web server or servlet. When the session terminates, the server releases the session object and all objects that bind on the session.

Binding objects to session

If you help you handle the data requirements of your application, you may need to bind your object to the session, you can bind any objects through a unique name to Session, then you need to use HttpSession objects. Any object that binds to the session can be handled as a servlet call to the same session.

Some objects may need you know when it will be placed in the session or remove from the session. You can get this information by using the HttpSessionBindingListener interface. When your application stores data into the session, or clear data from the session, servlet will be bind or cancelly bound by httpsessionBindingListener. The method of this interface will notify the object that is bound or canceled.

转载请注明原文地址:https://www.9cbs.com/read-61167.html

New Post(0)