【正文】
如果你以前曾經(jīng)使用過(guò) servlet,那么你 可能習(xí)慣于用以/myApp/servlet開(kāi)頭的 URI來(lái)調(diào)用 servlet。在某些使用程序類的幫助下, servlet 還可以提供諸如輸入驗(yàn)證、 J18N 準(zhǔn)備之類的服務(wù),而且通常會(huì)鼓勵(lì)使用更有效率的方法來(lái)請(qǐng)求處理。 ? 數(shù)據(jù)庫(kù)訪問(wèn) bean 必須在 應(yīng)用程序啟動(dòng)時(shí)可用于所有其他的部分:應(yīng)用程序生命周期時(shí)間監(jiān)聽(tīng)器是用了該任務(wù)的完美的組件類型。此類監(jiān)聽(tīng)器實(shí)現(xiàn)了 接口,當(dāng)應(yīng)用程序啟動(dòng)和關(guān)閉時(shí)會(huì)由容器調(diào)用該接口的方法 。 新類型的監(jiān)聽(tīng)器遵循的是標(biāo)準(zhǔn) Java 事件模型。過(guò)濾器對(duì)請(qǐng)求與應(yīng)答的行為體和首部具有完全訪問(wèn)權(quán)限,因此還可以執(zhí)行各種轉(zhuǎn)換。該接口定義的方法可提供對(duì)關(guān)于請(qǐng)求的許多信息的訪問(wèn)。 沈陽(yáng)航空 航天大學(xué) 北方科技學(xué)院畢業(yè)設(shè)計(jì) (外文翻譯 ) 15 容器只為每個(gè) Servlet 創(chuàng)建一個(gè)實(shí)例非常重要。該接口定義了 Web容器用來(lái)管理 servlet和與之交互的方法。其實(shí)這就是所謂的“第一人懲罰”?,F(xiàn)在有許多商業(yè)應(yīng)用服務(wù)器支持 JSP技術(shù),比如 BEA WebLogic、 IBM WebSphere、 JRun 等等。它通過(guò)使用線程技術(shù),減小了服 務(wù)器的開(kāi)銷。事實(shí)上,在 CSDN 的 JSP/Servlet 論壇中,經(jīng)??梢钥吹揭恍┨釂?wèn),代碼很長(zhǎng),可以邏輯卻不是很清晰,大量的 HTML和 Java代碼混雜在一起,讓人看得一頭霧水。s sent to the client. This makes it easy to centralize tasks that apply to all requests, such as access control, logging, and charging for the content or the services offered by the application. A filter has full access to the body and headers of the request and response, so it can also perform various transformations. One example is pressing the response body if the AcceptLanguage request header indicates that the client can handle a pressed response. A filter can be applied to either a specific servlet or to all requests matching a URL pattern, such as URLs starting with the same path elements or having the same extension. Listeners Listeners allow your application to react to certain events. Prior to Servlet , you could handle only session attribute binding events (triggered when an object was added or removed from a session). You could do this by letting the object saved as a sessionattribute(using the () method)implement the HttpSessionBindingListener interface. With the new interfaces introduced in the version of the specification, you can create listeners for servlet context and session lifecycle events as well as session activation and passivation events (used by a container that temporarily saves session state to disk or migrates a session to another server). A new 8 session attribute event listener also makes it possible to deal with attribute binding events for all sessions in one place, instead of placing individual listener objects in each session. The new types of listeners follow the standard Java event model. In other words, a listener is a class that implements one or more of the listener interfaces. The interfaces define methods that correspond to events. The listener class is registered with the container when the application starts, and the container then calls the event methods at the appropriate times. Initializing Shared Resources Using a Listener Beans like this typically need to be initialized before they can be used. For instance, they may need a reference to a database or some other external data source and may create an initial information cache in memory to provide fast access even to the first request for data. You can include code for initialization of the shared resources in the servlet and JSP pages that need them, but a more modular approach is to place all this code in one place and let the other parts of the application work on the assumption that the resources are already initialized and available. An application lifecycle listener is a perfect tool for this type of resource initialization. This type of listener implements the interface, with methods called by the container when the application starts and when it shuts down. Picking the Right Component Type for Each Task The Project Billboard application introduced is a fairly plex application. Half the 9 pages are pure controller and business logic processing, it accesses a database to authenticate users, and most pages require access control. In real life, it would likely contain even more pages, for instance, pages for access to a shared document archive, time schedules, and a set of pages for administration. As the application evolves, it may bee hard to maintain as a pure JSP application. It39。t implement these methods。 in other words, it lets you use the most appropriate ponent type for the roles of Model, View and Controller. Servlets, Filters, and Listeners A servlet is a Java class that extends a server with functionality for processing a request and producing a response. It39。t yet support this version of the sp