【正文】
當然,僅僅是流行并不能證明技術的優(yōu)越性。例如,在 C 和 C++中,可以分配一個 100 個元素的數(shù)組,然后向第 999 個 “元素 “寫入數(shù)據(jù) ——實際上是程序內存的隨機部分,這完全合法。因此,他們可以切換到 BEA WebLogic 或Oracle9i AS。因此,從美國公司那里購買專用 Web服務器會消耗掉項目的大部分前期資金。這與其他 CGI 方案形成鮮明的對比,這些 CGI 方案在初期都需要為購買專利軟件包投入大量的資金。實際上都直接或通過插件支持 servlet。而在CGI 中,大部分工作都需要我們資金完成。而使用servelt, Java 虛擬機會一直運行,并用輕量級的 Java 線程處理每個請求,而非重量級的操作系統(tǒng)進程。從客戶端到 Web 層再到數(shù)據(jù)庫(三層結構),要比從 applet 直接到數(shù)據(jù)庫(二層結構)更靈活,也更安全,而性能上的損 失很少甚至沒有。 如果頁面需要根據(jù)每個具體的請求做出相應的改變,當然需要在請求發(fā)生時構建響應。然而,許多情況下靜態(tài)的結果不能滿足要求,我們需要針對每個請求生成一個頁面。 這個文檔可以用各種格式發(fā)送,包括文本( HTML 或 XML),二進制( GIF圖),甚至可以式建立在其他底層格式之上的壓縮格式,如 gzip。 ( 3) 生成結果。 Servlet 的工作是執(zhí)行西門的任務,如圖 所示 。t talk directly to the database. Even if it could, for security reasons, you probably would not want it to. The same argument applies to most other applications. You need the Web middle layer to extract the ining data from the HTTP stream, talk to the application, and embed the results inside a document. 4. Send the explicit data (., the document) to the client. This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), or even a pressed format like gzip that is layered on top of some other underlying format. But, HTML is by far the most mon format, so an important servlet/JSP task is to wrap the results inside of HTML. 5. Send the implicit HTTP response data. Figure 11 shows a single arrow going from the Web middle layer (the servlet or JSP page) to the client. But, there are really two varieties of data sent: the document itself and the behindthescenes HTTP information. Again, both varieties are critical to effective development. Sending HTTP response data involves telling the browser or other client what type of document is being returned (., HTML), setting cookies and caching parameters, and other such tasks. Why Build Web Pages Dynamically? many client requests can be satisfied by prebuilt documents, and the server would handle these requests without invoking servlets. In many cases, however, a static result is not sufficient, and a page needs to be generated for each request. There are a number of reasons why Web pages need to be built onthefly: 1. The Web page is based on data sent by the client. For instance, the results page from search engines and orderconfirmation pages at online stores are specific to particular user requests. You don39。t know what to display until you read the data that the user submits. Just remember that the user submits two kinds of data: explicit (., HTML form data) and implicit (., HTTP request headers). Either kind of input can be used to build the output page. In particular, it is quite mon to build a userspecific page based on a cookie value. 2. The Web page is derived from data that changes frequently. If the page changes for every request, then you certainly need to build the response at request time. If it changes only periodically, however, you could do it two ways: you could periodically build a new Web page on the server (independently of client requests), or you could wait and only build the page when the user requests it. The right approach depends on the situation, but sometimes it is more convenient to do the latter: wait for the user request. For example, a weather report or news headlines site might build the pages dynamically, perhaps returning a previously built page if that page is still up to date. 3. The Web page uses information from corporate databases or other serverside sources. If the information is in a database, you need serverside processing even if the client is using dynamic Web content such as an applet. Imagine using an applet by itself for a search engine site: Downloading 50 terabyte applet, please wait! Obviously, that is silly。