【正文】
ecification, I39。 Java 開(kāi)發(fā)者喜歡使用它有多種原因,其一是對(duì)于已經(jīng)熟悉 Java 語(yǔ)言的開(kāi)發(fā)者來(lái)說(shuō)這個(gè)技術(shù)容易學(xué)習(xí);其二是 Java 把“一次編寫(xiě),到處運(yùn)行”的理念帶入到 Web應(yīng)用中,實(shí)現(xiàn)了“一次編寫(xiě),到處實(shí)現(xiàn)”。 與 CGI相似, Servlet支持請(qǐng)求 /響應(yīng)模型。而且, Servlet使用線程,因而可以開(kāi)發(fā)出效率更高的 Web應(yīng)用。當(dāng)我們?cè)L問(wèn)一個(gè)JSP頁(yè)面的時(shí)候,這個(gè)文件首先會(huì)被 JSP引擎翻譯為一個(gè) Java源文件,其實(shí)就是一個(gè)Servlet,并進(jìn)行編譯,然后像其他 Servlet一樣,由 Servlet引擎來(lái)處理。它是用 Servlet API定義的類和接口實(shí)現(xiàn)的。對(duì)于 HttpServlet來(lái)說(shuō),容器會(huì)在 servlet生命周期的適當(dāng)時(shí)間調(diào)用方法。 Tomcat中包含了一個(gè)名為 的 JAR 文件,位于 mon/lib 目錄中。這兩種類型是在Servlet ,因此,如果你使用的是不支持該版本規(guī)范的容器,恐怕就不能繼續(xù)學(xué)習(xí)了 。實(shí)現(xiàn)監(jiān)聽(tīng)器的方式是用保存為會(huì)話屬性(使用 ()方法)的對(duì)象實(shí)現(xiàn) HttpSessionBindingListener 接口。例如,它們可能需要對(duì)數(shù)據(jù)庫(kù)或某些其他外部數(shù)據(jù)源的引用,還可能在內(nèi)存中創(chuàng)建一個(gè)初始消息緩存,以便即使是第一個(gè)請(qǐng)求數(shù)據(jù)也可以提供更快的訪問(wèn)。例如,很容易忘記在新頁(yè)面中包含訪問(wèn)控制代碼。將這些代碼放置到 Java類中后,就可以使用普通的 Java編譯程序和調(diào)試程序來(lái)修復(fù)潛在的問(wèn)題。 當(dāng)然,你可以自己開(kāi)發(fā)滿足這些要求的 servlet,但是已經(jīng)有開(kāi)源式 servlet了,他們可以滿足所有這些要求,甚至還有更多的功能。 。 下面是其他一些你可能希望支持的功能,即使并非所有應(yīng)用程序都要求: ? 擴(kuò)展應(yīng)用程序以便以更靈活的方式支持新類型的請(qǐng)求處理。 ? 必須很容易改編外觀呈現(xiàn):這正是 JSP的反光點(diǎn),也就是作為視圖。在現(xiàn)實(shí)生活中,它可能會(huì)包含更多的頁(yè)面,例如, 用于訪問(wèn)共享文檔檔案、事件表的頁(yè)面和用于管理的一組頁(yè)面等。當(dāng)應(yīng)用程序啟動(dòng)是,容易會(huì)注冊(cè)監(jiān)聽(tīng)器類,然后該容器會(huì)在合適的事件調(diào)用那些事件方法 。 監(jiān)聽(tīng)器 監(jiān)聽(tīng)器允許應(yīng)用程序?qū)μ囟ㄊ录龀龌貞?yīng)。它還定義了設(shè)置應(yīng)答狀態(tài)代碼和首部的方法。 編譯和安裝 servlet 要編譯 servlet,必須首先確保 JAR 文件包含著 CLASSPATH 環(huán)境變量中所有Servlet API類。 Servlet的生命周期 Web 容器管理 servlet 生命周期的所有方面。將 servlet 和 JSP 頁(yè)面組合起來(lái)使用,可以把應(yīng)用程序的邏輯部分和外觀呈現(xiàn)部分清楚地分開(kāi);換句話,利用這個(gè)方式可以對(duì)模型、視圖和控制器這三種角色分別使用最合適的組件類型 。 JSP 文件看起來(lái)就像一個(gè)普通靜態(tài) HTML 文件,只不過(guò)里面包含了一些 Java 代碼。與一般類的不同之處是,這個(gè)類運(yùn)行在一個(gè) Servlet容器內(nèi),可以提供 session管理和對(duì)象生命周期管理。雖然 CGI技術(shù)發(fā)展成熟且功能強(qiáng)大,但由于編程困難、效率 低下、修改復(fù)雜等缺點(diǎn),所以有逐漸被取代的趨勢(shì)。re probably used to invoking a servlet with a URI that starts with /myApp/servlet. This is a convention introduced by Suns Java Web Server (JWS), the first product to support servlets before the API was standardized. Most servlet containers support this convention today, even though it39。s important to realize that the container creates only one instance of each servlet. This means that the servlet must be thread safe able to handle multiple requests at the same time, each executing as a separate thread through the servlet code. Without getting lost in details, you satisfy this requirement with regards to instance variables if you modify the referenced objects only in the init( ) and destroy( ) methods, and just read them in the request processing methods. 6 Compiling and Installing a Servlet To pile a servlet, you must first ensure that you have the JAR file containing all Servlet API classes in the CLASSPATH environment variable. The JAR file is distributed with all web containers. Tomcat includes it in a file called , located in the mon/lib directory. On a Windows platform, you include the JAR file in the CLASSPATH. . Reading a Request One of the arguments passed to the doGet( ) and doPost( ) methods is an object that implements the HttpServletRequest interface. This interface defines methods that provide access to a wealth of information about the request. Generating a Response Besides the request object, the container passes an object that implements the HttpServletResponse interface as an argument to the doGet( ) and doPost( ) methods. This interface defines methods for getting a writer or stream for the response body. It also defines methods for setting the response status code and headers. Using Filters and Listeners The servlet specification defines two ponent types beside servlets: filters and listeners. These two types were introduced in the Servlet specification, so if you39。1 Combining JSP and Servlets The technology of JSP and Servlet is the most important technology which use Java technology to exploit request of server, and it is also the standard which exploit business application .Java developers prefer to use it for a variety of reasons, one of which is already familiar with the Java language for the development of this technology are easy to learn Java to the other is a preparation, run everywhere to bring the concept of Web applications, To achieve a oneprepared everywhere realized. And more importantly, if followed some of the principles of good design, it can be said of separating and content to create highquality, reusable, easy to maintain and modify the application. For example, if the document in HTML embedded Java code too much (script), will lead the developed application is extremely plex, difficult to read, it is not easy reuse, but also for future ma