版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、<p><b> 重慶三峽學(xué)院</b></p><p> 畢業(yè)設(shè)計(jì)(論文)外文資料翻譯</p><p> 設(shè)計(jì) (論文)題目 圖書管理系統(tǒng) </p><p> 院 系 計(jì)算機(jī)科學(xué)與工程學(xué)院 </p><p> 專 業(yè)
2、 計(jì)算機(jī)科學(xué)與技術(shù) </p><p> 年 級(jí) 2009級(jí)1班 </p><p> 學(xué)生學(xué)號(hào) 2008908112 </p><p>
3、 學(xué)生姓名 管金鳳 </p><p> 外文出處CHINA-USA Business Review </p><p> Combining JSP and Servlets</p><p> The technology of JSP and Servlet is the most im
4、portant 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 alread
5、y 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 "one-p
6、repared everywhere realized</p><p> Early dynamic pages mainly CGI (Common Gateway Interface, public Gateway Interface) technology, you can use different languages of the CGI programs, such as VB, C / C + +
7、 or Delphi, and so on. Though the technology of CGI is developed and powerful, because of difficulties in programming, and low efficiency, modify complex shortcomings, it is gradually being replaced by the trend. Of all
8、the new technology, JSP / Servlet with more efficient and easy to program, more powerful, more secure and has </p><p> Similar to CGI, Servlet support request / response model. When a customer submit a requ
9、est to the server, the server presented the request Servlet, Servlet responsible for handling requests and generate a response, and then gave the server, and then from the server sent to the customer. And the CGI is diff
10、erent, Servlet not generate a new process, but with HTTP Server at the same process. It threads through the use of technology, reduce the server costs. Servlet handling of the request process i</p><p> Alth
11、ough such a complex, in fact, simply said to Servlet is a Java class. And the general category of the difference is that this type operating in a Servlet container, which can provide session management and targeted life-
12、cycle management. So that when you use the Servlet, you can get all the benefits of the Java platform, including the safety of the management, use JDBC access the database and cross-platform capability. Moreover, Servlet
13、 using thread, and can develop more efficient Web applic</p><p> JSP technology is a key J2EE technology, it at a higher level of abstraction of a Servlet. It allows conventional static and dynamic HTML con
14、tent generated by combining an HTML page looks like, but as a Servlet to run. There are many commercial application server support JSP technology, such as BEA WebLogic, IBM WebSphere, JRun, and so on. JSP and Servlet use
15、 more than simple. If you have a JSP support for Web servers, and a JSP document, you can put it Fangdao any static HTML files can be place</p><p> JSP document looks like an ordinary static HTML document,
16、but inside contains a number of Java code. It uses. Jsp the suffix, used to tell the server this document in need of special treatment. When we visit a JSP page, the document will first be translated into a JSP engine Ja
17、va source files, is actually a Servlet, and compiler, and then, like other Servlet, from Servlet engine to handle. Servlet engine of this type loading, handling requests from customers, and the results returned to the cu
18、st</p><p> After another visit this page to the customer, as long as the paper there have been no changes, JSP engine has been loaded directly call the Servlet. If you have already been modified, it will be
19、 once again the implementation of the above process, translate, compile and load. In fact, this is the so-called "first person to punishment." Because when the first visit to the implementation of a series of t
20、he above process, so will spend some time after such a visit would not. </p><p> Java servlets offer a powerful API that provides access to all the information about the request, the session, and the applic
21、ation. combining JSP with servlets lets you clearly separate the application logic from the presentation of the application; in other words, it lets you use the most appropriate component type for the roles of Model, Vi
22、ew and Controller.</p><p> Servlets, Filters, and Listeners </p><p> A servlet is a Java class that extends a server with functionality for processing a request and producing a response. It
23、9;s implemented using the classes and interfaces defined by the Servlet API. The API consists of two packages: the javax.servlet package contains classes and interfaces that are protocol-independent, while the javax.serv
24、let.http package provides HTTP-specific extensions and utility classes.</p><p> What makes a servlet a servlet is that the class implements an interface named javax.servlet.Servlet, either directly or by ex
25、tending one of the support classes. This interface defines the methods used by the web container to manage and interact with the servlet. A servlet for processing HTTP requests typically extends the javax.servlet.http.Ht
26、tpServlet class. This class implements the Servlet interface and provides additional methods suitable for HTTP processing. </p><p> Servlet Lifecycle</p><p> The web container manages all aspe
27、cts of the servlet's lifecycle. It creates an instance of the servlet class when needed, passes requests to the instance for processing, and eventually removes the instance. For an HttpServlet, the container calls th
28、e following methods at the appropriate times in the servlet lifecycle.</p><p> Besides the doGet( ) and doPost( ) methods, there are methods corresponding to the other HTTP methods: doDelete( ), doHead( ),
29、doOptions( ), doPut( ), and doTrace( ). Typically you don't implement these methods; the HttpServlet class already takes care of HEAD, OPTIONS, and TRACE requests in a way that's suitable for most servlets, and
30、the DELETE and PUT HTTP methods are rarely used in a web application. </p><p> It's important to realize that the container creates only one instance of each servlet. This means that the servlet must b
31、e 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 var
32、iables if you modify the referenced objects only in the init( ) and destroy( ) methods, and just read them in the request processing methods.</p><p> Compiling and Installing a Servlet</p><p>
33、 To compile 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
34、 a file called servlet.jar, located in the common/lib directory. On a Windows platform, you include the JAR file in the CLASSPATH.</p><p> . Reading a Request</p><p> One of the arguments pass
35、ed 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.</p><p>
36、; Generating a Response</p><p> Besides the request object, the container passes an object that implements the HttpServletResponse interface as an argument to the doGet( ) and doPost( ) methods. This inter
37、face defines methods for getting a writer or stream for the response body. It also defines methods for setting the response status code and headers.</p><p> Using Filters and Listeners</p><p>
38、 The servlet specification defines two component types beside servlets: filters and listeners. These two types were introduced in the Servlet 2.3 specification, so if you're using a container that doesn't yet sup
39、port this version of the specification, I'm afraid you're out of luck.</p><p><b> Filters</b></p><p> A filter is a component that can intercept a request targeted for a se
40、rvlet, JSP page, or static page, as well as the response before it'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 conten
41、t 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 compressing the response bo</p
42、><p> 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.</p><p> Liste
43、ners </p><p> Listeners allow your application to react to certain events. Prior to Servlet 2.3, you could handle only session attribute binding events (triggered when an object was added or removed from a
44、session). You could do this by letting the object saved as a sessionattribute(using the HttpSession.setAttribute() method)implement the HttpSessionBindingListener interface. With the new interfaces introduced in the 2.3
45、version of the specification, you can create listeners for servlet context and session l</p><p> The new types of listeners follow the standard Java event model. In other words, a listener is a class that i
46、mplements 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
47、 event methods at the appropriate times.</p><p> Initializing Shared Resources Using a Listener</p><p> Beans like this typically need to be initialized before they can be used. For instance,
48、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 initial
49、ization 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 </p><p> Picki
50、ng the Right Component Type for Each Task</p><p> The Project Billboard application introduced is a fairly complex application. Half the pages are pure controller and business logic processing, it accesses
51、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 pa
52、ges for administration. As the application evolves, it may become hard to maintain as a pure JSP application. It's easy to f</p><p> This is clearly an application that can benefit from using a combinat
53、ion of JSP pages and the component types defined by the servlet specification for the MVC roles. Let's look at the main requirements and see how we can map them to appropriate component types:</p><p> D
54、atabase access should be abstracted, to avoid knowledge of a specific data schema or database engine in more than one part of the application: beans in the role of Model can be used to accomplish this.</p><p&g
55、t; The database access beans must be made available to all other parts of the application when it starts: an application lifecycle event listener is the perfect component type for this task. </p><p> Only
56、 authenticated users must be allowed to use the application: a filter can perform access control to satisfy this requirement. </p><p> Request processing is best done with Java code: a servlet, acting as t
57、he Controller, fits the bill. </p><p> It must be easy to change the presentation: this is where JSP shines, acting as the View. </p><p> Adding servlets, listeners, and filters to the mix m
58、inimizes the need for complex logic in the JSP pages. Placing all this code in Java classes instead makes it possible to use a regular Java compiler and debugger to fix potential problems.</p><p> Centraliz
59、ed Request Processing Using a Servlet</p><p> With a servlet as the common entry point for all application requests, you gain control over the page flow of the application. The servlet can decide which type
60、 of response to generate depending on the outcome of the requested action, such as returning a common error page for all requests that fail, or different responses depending on the type of client making the request. With
61、 the help from some utility classes, it can also provide services such as input validation, I18N preparations, and in ge</p><p> When you use a servlet as a Controller, you must deal with the following basi
62、c requirements: </p><p> All requests for processing must be passed to the single Controller servlet.</p><p> The servlet must be able to distinguish requests for different types of processing
63、. </p><p> Here are other features you will want support for, even though they may not be requirements for all applications: </p><p> A strategy for extending the application to support new t
64、ypes of processing requests in a flexible manner.</p><p> A mechanism for changing the page flow of the application without modifying code.</p><p> Mapping Application Requests to the Servlet&
65、lt;/p><p> The first requirement for using a Controller servlet is that all requests must pass through it. This can be satisfied in many ways. If you have played around a bit with servlets previously, you'
66、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. Mos
67、t servlet containers support this convention today, even though it's not formally defi</p><p> 將Servlet和JSP組合使用</p><p> Servlet和JSP技術(shù)是用Java開發(fā)服務(wù)器端應(yīng)用的主要技術(shù),是開發(fā)商務(wù)應(yīng)用表示端的標(biāo)準(zhǔn)。Java開發(fā)者喜歡使用它有多種原因,其一是
68、對(duì)于已經(jīng)熟悉Java語言的開發(fā)者來說這個(gè)技術(shù)容易學(xué)習(xí);其二是Java把“一次編寫,到處運(yùn)行”的理念帶入到Web應(yīng)用中,實(shí)現(xiàn)了“一次編寫,到處實(shí)現(xiàn)”。而且更為重要的是,如果遵循一些良好的設(shè)計(jì)原則的話,就可以把表示和內(nèi)容相分離,創(chuàng)造出高質(zhì)量的、可以復(fù)用的、易于維護(hù)和修改的應(yīng)用程序。比方說,在HTML文檔中如果嵌入過多的Java代碼(scriptlet),就會(huì)導(dǎo)致開發(fā)出來的應(yīng)用非常復(fù)雜、難以閱讀、不容易復(fù)用,而且對(duì)以后的維護(hù)和修改也會(huì)造成困難
69、。事實(shí)上,在CSDN的JSP/Servlet論壇中,經(jīng)常可以看到一些提問,代碼很長,可以邏輯卻不是很清晰,大量的HTML和Java代碼混雜在一起,讓人看得一頭霧水。這就是隨意開發(fā)的弊端。</p><p> 早期的動(dòng)態(tài)網(wǎng)頁主要采用CGI(Common Gateway Interface,公共網(wǎng)關(guān)接口)技術(shù),你可以使用不同的語言編寫CGI程序,如VB、C/C++或Delphi等。雖然CGI技術(shù)發(fā)展成熟且功能強(qiáng)大,但
70、由于編程困難、效率低下、修改復(fù)雜等缺點(diǎn),所以有逐漸被取代的趨勢(shì)。在所有的新技術(shù)中,JSP/Servlet具備更高效、更容易編程、功能更強(qiáng)、更安全和具有良好的可移植性,因而被許多人認(rèn)為是未來最有發(fā)展前途的動(dòng)態(tài)網(wǎng)站技術(shù)。</p><p> 與CGI相似,Servlet支持請(qǐng)求/響應(yīng)模型。當(dāng)一個(gè)客戶向服務(wù)器遞交一個(gè)請(qǐng)求時(shí),服務(wù)器把請(qǐng)求送給Servlet,Servlet負(fù)責(zé)處理請(qǐng)求并生成響應(yīng),然后送給服務(wù)器,再由服務(wù)器
71、發(fā)送給客戶。與CGI不同的是,Servlet沒有生成新的進(jìn)程,而是與HTTP Server處于同一進(jìn)程中。它通過使用線程技術(shù),減小了服務(wù)器的開銷。Servlet處理請(qǐng)求的過程是這樣的:當(dāng)收到來自客戶端的請(qǐng)求后,調(diào)用service方法,該方法中Servlet先判斷到來的請(qǐng)求是什么類型的(GET/POST/HEAD…),然后調(diào)用相應(yīng)的處理方法(doGet/doPost/doHead…)并生成響應(yīng)。</p><p>
72、 別看這么復(fù)雜,其實(shí)簡(jiǎn)單說來Servlet就是一個(gè)Java類。與一般類的不同之處是,這個(gè)類運(yùn)行在一個(gè)Servlet容器內(nèi),可以提供session管理和對(duì)象生命周期管理。因而當(dāng)你使用Servlet的時(shí)候,你可以得到Java平臺(tái)的所有好處,包括安全性管理、使用JDBC訪問數(shù)據(jù)庫以及跨平臺(tái)的能力。而且,Servlet使用線程,因而可以開發(fā)出效率更高的Web應(yīng)用。</p><p> JSP技術(shù)是J2EE的一個(gè)關(guān)鍵技術(shù),
73、它在更高一級(jí)的層次上抽象Servlet。它可以讓常規(guī)靜態(tài)HTML與動(dòng)態(tài)產(chǎn)生的內(nèi)容相結(jié)合,看起來像一個(gè)HTML網(wǎng)頁,卻作為Servlet來運(yùn)行?,F(xiàn)在有許多商業(yè)應(yīng)用服務(wù)器支持JSP技術(shù),比如BEA WebLogic、IBM WebSphere、 JRun等等。使用JSP比用Servlet更簡(jiǎn)單。如果你有一個(gè)支持JSP的Web服務(wù)器,并且有一個(gè)JSP文件,你可以把它放倒任何靜態(tài)HTML文件可以放置的位置,不用編譯,不用打包,也不用進(jìn)行Clas
74、sPath的設(shè)置,就可以像訪問普通網(wǎng)頁那樣訪問它,服務(wù)器會(huì)自動(dòng)幫你做好其他的工作。</p><p> JSP 文件看起來就像一個(gè)普通靜態(tài)HTML文件,只不過里面包含了一些Java代碼。它使用.jsp的后綴,用來告訴服務(wù)器這個(gè)文件需要特殊的處理。當(dāng)我們?cè)L問一個(gè)JSP頁面的時(shí)候,這個(gè)文件首先會(huì)被JSP引擎翻譯為一個(gè)Java源文件,其實(shí)就是一個(gè)Servlet,并進(jìn)行編譯,然后像其他Servlet一樣,由Servlet
75、引擎來處理。Servlet引擎裝載這個(gè)類,處理來自客戶的請(qǐng)求,并把結(jié)果返回給客戶。</p><p> 以后再有客戶訪問這個(gè)頁面的時(shí)候,只要該文件沒有發(fā)生過更改,JSP引擎就直接調(diào)用已經(jīng)裝載的Servlet。如果已經(jīng)做過修改的話,那就會(huì)再次執(zhí)行以上過程,翻譯、編譯并裝載。其實(shí)這就是所謂的“第一人懲罰”。因?yàn)槭状卧L問的時(shí)候要執(zhí)行一系列以上的過程,所以會(huì)耗費(fèi)一些時(shí)間;以后的訪問就不會(huì)這樣了。</p>&
76、lt;p> Java servlet提供了一種強(qiáng)有力的API,用這個(gè)API可以訪問關(guān)于請(qǐng)求、會(huì)話和應(yīng)用程序的所有信息。將servlet和JSP頁面組合起來使用,可以把應(yīng)用程序的邏輯部分和外觀呈現(xiàn)部分清楚地分開;換句話,利用這個(gè)方式可以對(duì)模型、視圖和控制器這三種角色分別使用最合適的組件類型。</p><p> Servlet、過濾器和監(jiān)聽器</p><p> Servlet是一種
77、Java類,它使得服務(wù)器的功能可擴(kuò)展至處理請(qǐng)求和生成應(yīng)答。它是用Servlet API定義的類和接口實(shí)現(xiàn)的。API由兩個(gè)程序包組成:jvavax.servlet程序包包含獨(dú)立于協(xié)議的類和接口,而javax.servlet.http程序包則提供HTTP特定的擴(kuò)展的實(shí)用程序類。</p><p> Servlet的實(shí)質(zhì)是實(shí)現(xiàn)了接口javax.servlet.Servlet的類,實(shí)現(xiàn)是直接完成或通過擴(kuò)展某個(gè)支持類來完成
78、的。該接口定義了Web容器用來管理servlet和與之交互的方法。用于處理HTTP請(qǐng)求的servlet一般情況下都會(huì)擴(kuò)展javax.servlet.http.HttpServlet類。該類實(shí)現(xiàn)了Servlet接口,并提供了使用HTTP處理的附加方法。</p><p> Servlet的生命周期</p><p> Web容器管理servlet生命周期的所有方面。它根據(jù)需要?jiǎng)?chuàng)建servle
79、t類的實(shí)例、將請(qǐng)求傳遞給實(shí)例進(jìn)行處理,最終刪除實(shí)例。對(duì)于HttpServlet來說,容器會(huì)在servlet生命周期的適當(dāng)時(shí)間調(diào)用方法。</p><p> 除了doGet()和doPost()方法之外,還有一些對(duì)應(yīng)于其他HTTP方法的方法:doDelete()、doHead()、doOptiongs()、doPut()和doTrace()。一般情況下不用實(shí)現(xiàn)這些方法,因?yàn)镠ttpServlet類已經(jīng)用適用于大多數(shù)
80、servlet的方法考慮到了HEAD、OPTIONS和TRACE請(qǐng)求,而且DELETE和PUT這兩種HTTP方法很少用在Web應(yīng)用程序中。</p><p> 容器只為每個(gè)Servlet創(chuàng)建一個(gè)實(shí)例非常重要。這意味著servlet必須是線程安全的—即,能夠同時(shí)處理多個(gè)請(qǐng)求,每個(gè)處理都通過servlet代碼作為單獨(dú)的線程來執(zhí)行。如果只在init()和destroy()方法中修改參考的對(duì)象,而且只在請(qǐng)求處理方法中讀取
81、他們,那么不用喪失任何細(xì)節(jié)就可以滿足關(guān)于實(shí)例變量的這個(gè)要求。</p><p> 編譯和安裝servlet</p><p> 要編譯servlet,必須首先確保JAR文件包含著CLASSPATH環(huán)境變量中所有Servlet API類。該JAR文件將隨所有的Web容器一起發(fā)布。Tomcat中包含了一個(gè)名為servlet.jar的JAR文件,位于common/lib目錄中。在Windows平
82、臺(tái)中,應(yīng)在CLASSPATH中包含JAR文件。</p><p><b> 讀取請(qǐng)求</b></p><p> 傳遞到doGet()和doPost()方法的參數(shù)之一是實(shí)現(xiàn)了HttpServletRequest接口的對(duì)象。該接口定義的方法可提供對(duì)關(guān)于請(qǐng)求的許多信息的訪問。</p><p><b> 生成應(yīng)答</b><
83、;/p><p> 除應(yīng)答對(duì)象之外,容器還將實(shí)現(xiàn)HttpServletRequest接口的對(duì)象作為icanshu傳遞給doGet()和doPost()方法。該接口定義了為應(yīng)答行為體獲取數(shù)序程序或流的方法。它還定義了設(shè)置應(yīng)答狀態(tài)代碼和首部的方法。</p><p><b> 使用過濾器和監(jiān)聽器</b></p><p> Servlet規(guī)范servl
84、et內(nèi)定義了兩種組件類型:過濾器和監(jiān)聽器。這兩種類型是在Servlet 2.3規(guī)范中引入的,因此,如果你使用的是不支持該版本規(guī)范的容器,恐怕就不能繼續(xù)學(xué)習(xí)了。</p><p><b> 過濾器</b></p><p> 過濾器是一種組件,可以解釋對(duì)servlet、JSP頁面或靜態(tài)頁面的請(qǐng)求以及發(fā)送給客戶端之前的應(yīng)答。這樣可以很容易地將應(yīng)用于所有請(qǐng)求的任務(wù)集中在一起
85、,例如訪問控制、登錄和內(nèi)容的開銷或應(yīng)用提供的服務(wù)等。過濾器對(duì)請(qǐng)求與應(yīng)答的行為體和首部具有完全訪問權(quán)限,因此還可以執(zhí)行各種轉(zhuǎn)換。例如,如果Accept-Language請(qǐng)求首部指出客戶端可以處理壓縮的應(yīng)答,那么過濾器就可以壓縮應(yīng)答的行為體。</p><p> 過濾器可以應(yīng)用在特定servlet上,或匹配某種URL模式的所有請(qǐng)求上,例如以相同的路徑元素開頭或具有相同擴(kuò)展名的URL。</p><p
86、><b> 監(jiān)聽器</b></p><p> 監(jiān)聽器允許應(yīng)用程序?qū)μ囟ㄊ录龀龌貞?yīng)。Servlet 2.3之前,只能處理會(huì)話屬性綁定事件(在添加對(duì)象或從會(huì)話中刪除對(duì)象時(shí))。實(shí)現(xiàn)監(jiān)聽器的方式是用保存為會(huì)話屬性(使用HttpSession.setAttribute()方法)的對(duì)象實(shí)現(xiàn)HttpSessionBinding-Listener接口。隨著Servlet規(guī)范的2.3版本中新接口的
87、引入,可以為servlet環(huán)境和會(huì)話生命周期事件以及激活和鈍化事件(容器用來暫時(shí)將會(huì)話狀態(tài)保存在磁盤上或?qū)?huì)話移植到另一個(gè)服務(wù)器上)創(chuàng)建監(jiān)聽器。使用新的會(huì)話屬性事件監(jiān)聽器還可以在一個(gè)位置上處理所有會(huì)話的屬性綁定事件,而不是在每個(gè)會(huì)話中防止單獨(dú)的監(jiān)聽器對(duì)象。</p><p> 新類型的監(jiān)聽器遵循的是標(biāo)準(zhǔn)Java事件模型。換句話說,監(jiān)聽器是實(shí)現(xiàn)了一個(gè)或多個(gè)監(jiān)聽器接口的類。接口定義的是事件相應(yīng)的方法。當(dāng)應(yīng)用程序啟動(dòng)是
88、,容易會(huì)注冊(cè)監(jiān)聽器類,然后該容器會(huì)在合適的事件調(diào)用那些事件方法。</p><p> 使用監(jiān)聽器初始化共享資源</p><p> Bean一般都有需要在使用之前進(jìn)行初始化。例如,它們可能需要對(duì)數(shù)據(jù)庫或某些其他外部數(shù)據(jù)源的引用,還可能在內(nèi)存中創(chuàng)建一個(gè)初始消息緩存,以便即使是第一個(gè)請(qǐng)求數(shù)據(jù)也可以提供更快的訪問??梢栽谛枰蚕碣Y源的servlet和JSP頁面中包含初始化共享資源的代碼,但是更標(biāo)
89、準(zhǔn)的方法是在一個(gè)位置放置所有這些代碼,并在假設(shè)資源已經(jīng)初始化和可用的情況下,使應(yīng)用程序的其他部分可以正常工作。應(yīng)用程序生命周期監(jiān)聽器是此類資源初始化的絕好工具。此類監(jiān)聽器實(shí)現(xiàn)了javax.servlet.ServletContextListener接口,當(dāng)應(yīng)用程序啟動(dòng)和關(guān)閉時(shí)會(huì)由容器調(diào)用該接口的方法。</p><p> 為每個(gè)任務(wù)選擇正確的組件類型</p><p> 在之前介紹的項(xiàng)目公
90、告牌應(yīng)用程序是相當(dāng)復(fù)雜的應(yīng)用程序。頁面的一般都是純粹的控制器和商務(wù)邏輯處理,它訪問數(shù)據(jù)庫以對(duì)用戶進(jìn)行身份驗(yàn)證,而且多數(shù)頁面都需要訪問控制。在現(xiàn)實(shí)生活中,它可能會(huì)包含更多的頁面,例如,用于訪問共享文檔檔案、事件表的頁面和用于管理的一組頁面等。由于應(yīng)用程序在不斷地發(fā)展變化,因此可能變得很難作為純JSP應(yīng)用程序來維護(hù)。例如,很容易忘記在新頁面中包含訪問控制代碼。</p><p> 很明顯,這種應(yīng)用程序可以從使用JSP
91、頁面與組件類型的組合中受益,其中組件類型由用于MVC角色的servlet規(guī)范所定義。下面看一下主要的要求,并了解如何將他們映射到適當(dāng)?shù)慕M件類型上:</p><p> 數(shù)據(jù)庫訪問應(yīng)該是抽象的,從而避免料接應(yīng)用程序中多個(gè)部分的特定數(shù)據(jù)模式或數(shù)據(jù)庫引擎:模型角色中的bean可以用來完成這種認(rèn)知。</p><p> 數(shù)據(jù)庫訪問bean必須在應(yīng)用程序啟動(dòng)時(shí)可用于所有其他的部分:應(yīng)用程序生命周期時(shí)
92、間監(jiān)聽器是用了該任務(wù)的完美的組件類型。</p><p> 只有通過驗(yàn)證的用戶才允許使用應(yīng)用程序:過濾器可以完成訪問控制以滿足該要求。</p><p> 用Java代碼進(jìn)行請(qǐng)求處理效果最佳:servlet作為控制器正符合需要。</p><p> 必須很容易改編外觀呈現(xiàn):這正是JSP的反光點(diǎn),也就是作為視圖。</p><p> 將serv
93、let、監(jiān)聽器和過濾器混合起來,就將JSP頁面對(duì)復(fù)雜邏輯的需求降到了最低。將這些代碼放置到Java類中后,就可以使用普通的Java編譯程序和調(diào)試程序來修復(fù)潛在的問題。</p><p> 使用servlet集中處理請(qǐng)求</p><p> 將servlet作為所有應(yīng)用程序請(qǐng)求的公共入口時(shí),可以獲得對(duì)應(yīng)用程序頁面流的整體控制。Servlet可以根據(jù)所請(qǐng)求行為的結(jié)果來決定要生成的應(yīng)答類型,例如
94、,為所有失敗的請(qǐng)求返回公共的錯(cuò)誤頁面,或者根據(jù)發(fā)出請(qǐng)求的客戶端返回不同的應(yīng)答等。在某些使用程序類的幫助下,servlet還可以提供諸如輸入驗(yàn)證、J18N準(zhǔn)備之類的服務(wù),而且通常會(huì)鼓勵(lì)使用更有效率的方法來請(qǐng)求處理。</p><p> 當(dāng)使用servlet作為控制器時(shí),必須處理下列基本要求:</p><p> 所有處理請(qǐng)求必須傳遞到單獨(dú)的控制器servlet中。</p>&l
95、t;p> Servlet必須能夠區(qū)分請(qǐng)求,以便進(jìn)行不同類型的處理。</p><p> 下面是其他一些你可能希望支持的功能,即使并非所有應(yīng)用程序都要求:</p><p> 擴(kuò)展應(yīng)用程序以便以更靈活的方式支持新類型的請(qǐng)求處理。</p><p> 在不修改代碼的情況下改變應(yīng)用程序頁面流的機(jī)制。</p><p> 當(dāng)然,你可以自己開發(fā)
96、滿足這些要求的servlet,但是已經(jīng)有開源式servlet了,他們可以滿足所有這些要求,甚至還有更多的功能。</p><p> 將應(yīng)用程序請(qǐng)求映射到servlet</p><p> 使用控制器servlet的第一個(gè)要求是所有請(qǐng)求必須都經(jīng)過該servlet。該要求可以通過多種方式來滿足。如果你以前曾經(jīng)使用過servlet,那么你可能習(xí)慣于用以/myApp/servlet開頭的URI來調(diào)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 外文翻譯---將servlet和jsp組合使用
- 將servlet和jsp組合使用-畢業(yè)論文外文翻譯
- 將servlet和jsp組合使用-畢業(yè)設(shè)計(jì)外文翻譯
- 外文翻譯----servlet和jsp技術(shù)簡(jiǎn)述
- 外文翻譯---servlet和jsp技術(shù)簡(jiǎn)介
- 外文翻譯--- servlet和jsp技術(shù)概要
- 外文翻譯譯文-servlet和jsp技術(shù)簡(jiǎn)述
- 圖書管理系統(tǒng)外文資料翻譯
- 基于jsp的圖書管理系統(tǒng)
- jsp圖書管理系統(tǒng)論文
- 外文文獻(xiàn)翻譯---servlet和jsp技術(shù)簡(jiǎn)介
- jsp課程設(shè)計(jì)--圖書管理系統(tǒng)
- 基于jsp的圖書管理系統(tǒng)-前臺(tái)管理
- 基于jsp的圖書管理系統(tǒng)—后臺(tái)管理
- 基于-jsp(java)圖書管理系統(tǒng)的設(shè)計(jì)和實(shí)現(xiàn)
- jsp圖書管理系統(tǒng)課程設(shè)計(jì)
- 基于jsp(java)圖書管理系統(tǒng)的設(shè)計(jì)和實(shí)現(xiàn)
- 外文文獻(xiàn)及翻譯----servlet和jsp技術(shù)簡(jiǎn)述
- 使用JSP和Servlet技術(shù)構(gòu)建BBS論壇系統(tǒng).pdf
- 畢業(yè)論文--基于jsp的圖書管理系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)(含外文翻譯)
評(píng)論
0/150
提交評(píng)論