【正文】
便,因?yàn)樗恍韬唵蔚氐却脩舻恼?qǐng)求。 理論上講, servelt 并非只用于處理 HTTP 請(qǐng)求的 Web 服務(wù)器或應(yīng)用服務(wù)器,它同樣可以用于其他類型的服務(wù)器。同樣的情況,如果使用servlet 則啟動(dòng) N 個(gè)線程,單僅僅載入 servlet 類的單一副本。 Servlet 能夠直接于 Web 服務(wù)器對(duì)話,而常規(guī)的 CGI 程序做不到這一點(diǎn),至少在不使用服務(wù)器專 有 API 的情況下是這樣。為 Macromedia JRun 編寫的 servlet,可以不經(jīng)過任何修改地在 Apache Tomcat, Microsoft Inter Information Server,IBM WebSphere 。例如, Marty 記錄了所有通過電子郵件向他發(fā)送問題的讀者的所在國。項(xiàng)目取得成功之后,他們可以轉(zhuǎn)移到性能更高、管理更容易,但需要付費(fèi)的服務(wù)器。最后,如果他們的項(xiàng)目變得更龐大,他們或許將他從 Linux 轉(zhuǎn)移到運(yùn)行 IBM WebSphere 的 IBM 大型機(jī)上。 Servelt 不存在這些問題。但我們的立場是:服務(wù)器端 Java 本非一項(xiàng)新的、為經(jīng)證實(shí)的技術(shù)。存在幾種低廉的插件,通過應(yīng)用這些插件,Microsoft IIS 和 Zeus 也同樣支持 servlet 和 jsp 技術(shù),它們運(yùn)行在 Windows,Unix/Linus,MacOS,VMS,和 IBM 大型機(jī)操作系統(tǒng)之上。實(shí)現(xiàn)這項(xiàng)預(yù)防措施的難度可能超出我們的想象,在廣泛應(yīng)用的 CGI 庫中,不斷發(fā)現(xiàn)由這類問題引發(fā)的弱點(diǎn)。同樣,他們的 servelt 和 jsp 沒有任何部分需要重寫。首先,這兩個(gè)國家都擁有大量訓(xùn)練有素的軟件開發(fā)人員。 廉價(jià) 對(duì)于開發(fā)用的網(wǎng)站、低容量或中等容量網(wǎng)站的部署,有大量免費(fèi)或極為廉價(jià)的 Web 服務(wù)器可供選擇。 可移植性 Servelt 使用 Java 編程語言,并且遵循標(biāo)準(zhǔn)的 API。然而, servelt 會(huì)一直停留在內(nèi)存中(即使請(qǐng)求處理完畢),因而可以直接存儲(chǔ)客戶請(qǐng)求之間的任意復(fù)雜數(shù)據(jù)。 效率 應(yīng)用傳統(tǒng)的 CGI,針對(duì)每個(gè) HTTP 請(qǐng)求都用啟動(dòng)一個(gè)新的進(jìn)程。想象以下,如果一個(gè)搜索引擎網(wǎng)站完全使用 applet,那么用戶將會(huì)看到: “正在下載 50TB 的 applet,請(qǐng)等待! ”?;?cookie 值針對(duì)具體用戶構(gòu)建頁面的情況尤其普遍。 HTTP 響應(yīng)數(shù)據(jù)的發(fā)送過程涉及告知瀏覽器或其他客戶程序所返回 文檔的 類型(如 HTML),設(shè)置 cookie 和緩存參數(shù),以及其他類似的任務(wù)。因此,我們需要Web 中間層從 HTTP 流中提取輸入數(shù)據(jù),與應(yīng)用程序會(huì)話,并將結(jié)果嵌入到文檔中。兩種數(shù)據(jù)都很重要。t support them and developers don39。s Job Servlets are Java programs that run on Web or application servers, acting as a middle layer between requests ing from Web browsers or other HTTP clients and databases or applications on the HTTP server. Their job is to perform the following tasks, as illustrated in Figure 11. Figure 11 1. Read the explicit data sent by the client. The end user normally enters this data in an HTML form on a Web page. However, the data could also e from an applet or a custom HTTP client program. 2. Read the implicit HTTP request data sent by the browser. Figure 11 shows a single arrow going from the client to the Web server (the layer where servlets and JSP execute), but there are really two varieties of data: the explicit data that the end user enters in a form and the behindthescenes HTTP information. Both varieties are critical. The HTTP information includes cookies, information about media types and pression schemes the browser understands, and so on. 3. Generate the results. This process may require talking to a database, executing an RMI or EJB call, invoking a Web service, or puting the response directly. Your real data may be in a relational database. Fine. But your database probably doesn39。ll only be discussing HTTP servlets. The Advantages of Servlets Over Traditional CGI Java servlets are more efficient, easier to use, more powerful, more portable, safer, and cheaper than traditional CGI and many alternative CGIlike technologies. 1. Efficient With traditional CGI, a new process is started for each HTTP request. If the CGI program itself is relatively short, the overhead of starting the process can dominate the execution time. With servlets, the Java virtual machine stays running and handles each request with a lightweight Java thread, not a heavyweight operating system process. Similarly, in traditional CGI, if there are N requests to the same CGI program, the code for the CGI program is loaded into memory N times. With servlets, however, there would be N threads, but only a single copy of the servlet class would be loaded. This approach reduces server memory requirements and saves time by instantiating fewer objects. Finally, when a CGI program finishes handling a request, the program terminates. This approach makes it difficult to cache putations, keep database connections open, and perform other optimizations that rely on persistent data. Servlets, however, remain in memory even after they plete a response, so it is straightforward to store arbitrarily plex data between cli