【正文】
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 9 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 client requests. 2. Convenient Servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions, and many other such highlevel utilities. In CGI, you have to do much of this yourself. Besides, if you already know the Java programming language, why learn Perl too? You39。很多泛美的例子。因而,如果程序員忘記執(zhí)行這項(xiàng)檢查,就會(huì)將系統(tǒng)暴露在蓄意或偶然的緩沖區(qū)溢出攻擊之下。同樣,不需要對(duì) servlet和 jsp做出更改。 但是,使用 servlet 和 JSP,他們能夠從免費(fèi)的服務(wù)器開始: Apache Tomcat。 價(jià)格和可移植性在某種程度上是相互關(guān)聯(lián)的。因此。另外,如果您已經(jīng)了解了 Java 編程語言,為什么還有學(xué)校 Perl呢?您已經(jīng)承認(rèn)應(yīng)用 Java 技術(shù)編寫的代碼要比 Visual Basic, VBScript 或 C++ 編寫的代碼更可靠,且更易重用,為什么還有倒退回去選擇那些語言來開發(fā)服務(wù)器端的程序呢? 強(qiáng)大 Servlet 支持常規(guī) CGI 難以實(shí)現(xiàn)或根本不能實(shí)現(xiàn)的幾項(xiàng)功能。類似地,應(yīng)用傳統(tǒng)的 CGI 技術(shù),如果存在對(duì)同一 CGI 程序的 N個(gè)請(qǐng)求,那么 CGI 程序的代碼會(huì)載入內(nèi)存 N 次。畢竟數(shù)據(jù)庫調(diào)用通常是對(duì)速度影響最大的步驟,因而,經(jīng)過中間層可以執(zhí)行高速緩存和連接共享。但是,如果頁面周期性地改 變,我們可以用兩種方式來處理它:周期性地在服務(wù)器上構(gòu)建新的頁面(和客戶請(qǐng)求無關(guān)),或者僅僅在用戶請(qǐng)求該頁面時(shí)再構(gòu)建。實(shí)時(shí)構(gòu)建頁面的理由有很多種: 網(wǎng)頁基于客戶發(fā)送的數(shù)據(jù)。但是,到目前為止, HTML 式最常用的格式,故而 servelt 和 JSP 的重要任務(wù)之一就式將結(jié)果包裝到 HTML中。 這個(gè)過程可能需要訪問數(shù)據(jù)庫、執(zhí)行 RMI 或 EJB 調(diào)用、調(diào)用 Web 服務(wù),或者直接計(jì)算得出對(duì)應(yīng)的響應(yīng)。 圖 中間件的作用 ( 1) 讀取客戶發(fā)送的顯式數(shù)據(jù)。 最終用戶一般在頁面的 HTML 表單中輸入這些數(shù)據(jù)。實(shí)際的數(shù)據(jù)可能存儲(chǔ)在關(guān)系型數(shù)據(jù)庫中。 ( 5) 發(fā)送隱式的 HTTP 響應(yīng)數(shù)據(jù)。 例如,搜索引擎生成的頁面,以及在線商店的訂單確認(rèn)頁面,都要針對(duì)特定的用戶請(qǐng)求而產(chǎn)生。具體應(yīng)該采用哪種方式要根據(jù)具體情況而定,但后一種方式常常更為方便,因?yàn)樗恍韬?jiǎ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 不存在這些問題。但我們的立場(chǎng)是:服務(wù)器端 Java 本非一項(xiàng)新的、為經(jīng)證實(shí)的技術(shù) 。re already convinced that Java technology makes for more reliable and reusable code than does Visual Basic, VBScript, or C++. Why go back to those languages for serverside programming? 3. Powerful Servlets support several capabilities that are difficult or impossible to acplish with regular CGI. Servlets can talk directly to the Web server, whereas regular CGI programs cannot, at least not without using a serverspecific API. Communicating with the Web server makes it easier to translate relative URLs into concrete path names, for instance. Multiple servlets can also share data, making it easy to implement database connection pooling and similar resourcesharing optimizations. Servlets can also maintain information from request to request, simplifying techniques like s