【正文】
servelt 效率更高、更易用、更強(qiáng)大、更容易移植、更安全、也更廉價。類似地,應(yīng)用傳統(tǒng)的 CGI 技術(shù),如果存在對同一 CGI 程序的 N個請求,那么 CGI 程序的代碼會載入內(nèi)存 N 次。這種方式難以緩存計算結(jié)果,保持 數(shù)據(jù)庫連接打開,或是執(zhí)行依靠持續(xù)性數(shù)據(jù)的其他優(yōu)化。另外,如果您已經(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)功能。 Servelt 還能維護(hù)請求之間的信息,使得諸如會話跟蹤和計算結(jié)果緩存等技術(shù)變得更 為簡單。因此。他們是 java2 平臺企業(yè)版的一 部分,所以對 servlet 的支持越來越普遍。 價格和可移植性在某種程度上是相互關(guān)聯(lián)的。 那么,為什么印度和菲律賓都對這項(xiàng)技術(shù)著呢感興趣呢?我們推測答案可能分兩部分。 但是,使用 servlet 和 JSP,他們能夠從免費(fèi)的服務(wù)器開始: Apache Tomcat。沒有問題:他們可以轉(zhuǎn)而使用 Macromedia JRun Professional,該服務(wù)器支持分布式應(yīng)用。同樣,不需要對 servlet和 jsp做出更改。因此 ,CGI 程序必須仔細(xì)地過濾掉那些可能被外殼特殊處理的字符,如反引導(dǎo)和分號。因而,如果程序員忘記執(zhí)行這項(xiàng)檢查,就會將系統(tǒng)暴露在蓄意或偶然的緩沖區(qū)溢出攻擊之下。 主流 雖然存在許多很好的技術(shù),但是,如果提供商助支持他們,或開發(fā)人員不知道如何使用這些技術(shù),那么它們的優(yōu)點(diǎn)又如何體現(xiàn)呢? servelt 和 jsp 技術(shù)得到服務(wù)器提供商的廣泛支持,包括 Apache, Oracle, IBM, Sybase, BEA, Maromedia, Causho, Sun/iPla,New Atlanta, ATG, Fujitsu, Lutris, Silverstream, World Wide Web Consortinrm ,以及其他服務(wù)器。很多泛美的例子。t speak HTTP or return results in HTML, so the Web browser can39。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。t know how to use them, what good are they? Servlet and JSP technology is supported by servers from Apache, Oracle, IBM, Sybase, BEA, Macromedia, Caucho, Sun/iPla, New Atlanta, ATG, Fujitsu, Lutris, Silverstream, the World Wide Web Consortium (W3C), and many others. Several lowcost plugins add support to Microsoft IIS and Zeus as well. They run on Windows, Unix/Linux, MacOS, VMS, and IBM mainframe operating systems. They are the single most popular application of the Java programming language. They are arguably the most popular choice for developing medium to large Web applications. They are used by the airline industry (most United Airlines and Delta Airlines Web sites), emerce (), online banking (First USA Bank, Banco Popular de Puerto Rico), Web search engines/portals (), large financial sites (American Century Investments), and hundreds of other sites that you visit every day. Of course, popularity alone is no proof of good technology. Numerous counterexamples abound. But our point is that you are not experimenting with a new and unproven technology when you work with serverside Java.