计算机类文献翻译:HTTP和Servlet的基础知识
原站著录信息
摘要
摘要:让我们从定义Web应用程序开始。我们都看到过一般的客户端应用软件,但怎么样才是一个真正的Web应用程序?然而,它可以被定义为一个用于用户接入的运行在服务器上的程序,通过一个简单,一般用途的客户。今天,最常见的客户端是一种运行在pc机上的网页浏览器或工作站系统,但其他类型的用户正在迅速加入,如无线PDA ,手机,和其他专门设备。 最崇高目标是从你面前的任何类型的设备上获得你所要的信息和服务。这意味着同样的简单客户端程序必须能够与许多不同的服务器应用通话,以及应用程序必须能够适用于许多不同类型的客户。为了满足这一…
摘要:让我们从定义Web应用程序开始。我们都看到过一般的客户端应用软件,但怎么样才是一个真正的Web应用程序?然而,它可以被定义为一个用于用户接入的运行在服务器上的程序,通过一个简单,一般用途的客户。今天,最常见的客户端是一种运行在pc机上的网页浏览器或工作站系统,但其他类型的用户正在迅速加入,如无线PDA ,手机,和其他专门设备。
最崇高目标是从你面前的任何类型的设备上获得你所要的信息和服务。这意味着同样的简单客户端程序必须能够与许多不同的服务器应用通话,以及应用程序必须能够适用于许多不同类型的客户。为了满足这一需要,必须制定详细的客户端和服务器相互通信的协义。这正是超文本传输协议( HTTP )的目的。
HTTP and Servlet Basics
Abstract: Let's start off this part by defining the term web application. We've all seen regular client-side applications, but what exactly is a web application? Loosely, it can be defined as an application running on a server a user accesses through a thin, general-purpose client. Today, the most common client is a web browser on a PC or workstation, but other kinds of clients are rapidly joining the party, such as wireless PDAs, cell phones, and other specialized devices.
The lofty goal here is to access all the information and services you need from any type of device that happens to be in front of you. This means that the same simple client program must be able to talk to many different server applications, and the applications must be able to work with many different types of clients. To satisfy this need, the protocol of how a client and a server talk to each other must be defined in detail. That's exactly what the HyperText Transport Protocol (HTTP) is for.
1 The HTTP Request/Response Model
HTTP and all extended protocols based on HTTP are based on a very simple communications model. Here's how it works: a client, typically a web browser, sends a request for a resource to a server, and the server sends back a response corresponding to the resource (or a response with an error message if it can't process the request for some reason). A resource can be a number of things, such as a simple HTML file returned verbatim to the browser or a program that generates the response dynamically.
This simple model implies three important facts you need to be aware of:
HTTP is a stateless protocol. This means that the server doesn't keep any information about the client after it sends its response, and therefore it can't recognize that multiple requests from the same client may be related.
Web applications can't easily provide the kind of immediate feedback typically found in standalone GUI applications such as word processors or traditional client/server applications. Every interaction between the client and the server requires a request/response exchange. Performing a request/response exchange when a user selects an item in a list box or fills out a form element is usually too taxing on the bandwidth available to most Internet users.
There's nothing in the protocol that tells the server how a request is made; consequently, the server can't distinguish between various methods of triggering the request on the client. For example, HTTP doesn't allow a web server to differentiate between an explicit request caused by clicking a link or submitting a form and an implicit request caused by resizing the browser window or using the browser's Back button. In addition, HTTP doesn't contain any means for the server to invoke client specific functions, such as going back in the browser history list or sending the response to a certain frame. Also, the server can't detect when the user closes the browser.
Over the years, people havewww.51lunwen.org developed various tricks to overcome the first problem; HTTP's stateless nature. The other two problems—no immediate feedback and no details about how the request is made—are harder to deal with, but some amount of interactivity can be achieved by generating a response that includes client-side code (code executed by the browser), such as JavaScript or a Java applet.
1.1 Requests in Detail
Let's take a closer look at requests. A user sends a request to the server by clicking a link on a web page, submitting a form, or typing in a web page address in the browser's address field. To send a request, the browser needs to know which server to talk to and which resource to ask for. This information is specified by an HTTP Uniform Resource Locator (URL):
http://www.gefionsoftware.com/index.html
The first part of the URL shown specifies that the request is made using the HTTP protocol. This is followed by the name of the server, in this case www.gefionsoftware.com. The web server waits for requests to come in on a specific TCP/IP port. Port number 80 is the standard port for HTTP requests. If the web server uses another port, the URL must specify the port number in addition to the server name. For example:
http://www.gefionsoftware.com:8080/index.html
This request is sent to a server that uses port 8080 instead of 80. The last part of the URL, /index.html, identifies the resource that the client is requesting.
A URL is actually a specialization of a Uniform Resource Identifier (URI, defined in the RFC-2396 specification). A URL identifies a resource partly by its location, for instance the server that contains the resource. Another type of URI is a Uniform Resource Name (URN), which is a globally unique identifier that is valid no matter where the resource is located. HTTP deals only with the URL variety. The terms URI and URL are often used interchangeable, and unfortunately, they have slightly different definitions in different specifications. I'm trying to use the terms as defined by the HTTP/1.1 specification (RFC-2616), which is pretty close to how they are also used in the servlet and JSP specifications. Hence, I use the term URL only when the URI must start with http (or https, for HTTP over an encrypted connection) followed by a server name and possibly a port number, as in the previous examples. I use URI as a generic term for any string that identifies a resource, where the location can be deduced from the context and isn't necessarily part of the URI. For example, when the request has been delivered to the server, the location is a given, and only the resource identifier is important.
The browser uses the URL information to create the request message it sends to the specified server using the specified protocol. An HTTP request message consists of three things: a request line, request headers, and possibly a request body.
The request line starts with the request method name, followed by a resource identifier and the protocol version used by the browser:
GET /index.html HTTP/1.1
The most commonly used request method is named GET. As the name implies, a GET request is used to retrieve a resource from the server. It's the default request method, so if you type a URL in the browser's address field, or click on a link, the request is sent as a GET request to the server.
The request headers provide additional information the server may use to process the request. The message body is included only in some types of requests, like the POST request discussed later.
Here's an example of a valid HTTP request message:
GET /index.html HTTP/1.1
Host: www.gefionsoftware.com
User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv: 1.0.2)
Accept: image/gif, image/jpeg, image/pjpeg, image/png, */*
Accept-Language : en
Accept-Charset : iso-8859-1,*,utf-8
The request line specifies the GET method and asks for the reso
