




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Combining JSP and ServletsThe 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 fami
2、liar 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-prepared everywhere realized." And more importantly, if followed some of the princ
3、iples of good design, it can be said of separating and content to create high-quality, 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 complex, difficult to read, it is
4、not easy reuse, but also for future maintenance and modification will also cause difficulties. In fact, CSDN the JSP / Servlet forum, can often see some questions, the code is very long, can logic is not very clear, a large number of HTML and Java code mixed together. This is the random development
5、of the defects. 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 + + or Delphi, and so on. Though the technology of CGI is developed and powerful, because of difficulties in programm
6、ing, and low efficiency, modify complex shortcomings, it is gradually being replaced by the trend. Of all the new technology, JSP / Servlet with more efficient and easy to program, more powerful, more secure and has a good portability, they have been many people believe that the future is the most d
7、ynamic site of the future development of technology. Similar to CGI, Servlet support request / response model. When a customer submit a request to the server, the server presented the request Servlet, Servlet responsible for handling requests and generate a response, and then gave the server, and th
8、en from the server sent to the customer. And the CGI is different, 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 is this: When received from the client's req
9、uest, calling service methods, the method of Servlet arrival of the first judgement is what type of request (GET / POST / HEAD), then calls the appropriate treatment (DoGet / doPost / doHead) and generate a response. Although such a complex, in fact, simply said to Servlet is a Java class. And the g
10、eneral category of the difference is that this type operating in a Servlet container, which can provide session management and targeted life-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
11、the database and cross-platform capability. Moreover, Servlet using thread, and can develop more efficient Web applications. JSP technology is a key J2EE technology, it at a higher level of abstraction of a Servlet. It allows conventional static and dynamic HTML content generated by combining an HTM
12、L 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 more than simple. If you have a JSP support for Web servers, and a JSP document, you can put it Fangdao any stati
13、c HTML files can be placed, do not have to compile, do not have to pack, do not have to ClassPath settings, you can visit as ordinary Web It did visit, the server will automatically help you to do other work. JSP document looks like an ordinary static HTML document, but inside contains a number of J
14、ava 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 Java source files, is actually a Servlet, and compiler, and then, like other Servlet, from Servlet engine to handle
15、. Servlet engine of this type loading, handling requests from customers, and the results returned to the customer, as shown below: Figure 1: Calling the process of JSP pagesAfter another visit this page to the customer, as long as the paper there have been no changes, JSP engine has been loaded dire
16、ctly call the Servlet. If you have already been modified, it will be 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 the above pro
17、cess, so will spend some time after such a visit would not. Java servlets offer a powerful API that provides access to all the information about the request, the session, and the application. combining JSP with servlets lets you clearly separate the application logic from the presentation of the app
18、lication; in other words, it lets you use the most appropriate component 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. It's implemented using
19、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.servlet.http package provides HTTP-specific extensions and utility classes.What makes a servlet a servlet
20、is that the class implements an interface named javax.servlet.Servlet, either directly or by extending 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.
21、servlet.http.HttpServlet class. This class implements the Servlet interface and provides additional methods suitable for HTTP processing. Servlet LifecycleThe web container manages all aspects of the servlet's lifecycle. It creates an instance of the servlet class when needed, passes requests to
22、 the instance for processing, and eventually removes the instance. For an HttpServlet, the container calls the following methods at the appropriate times in the servlet lifecycle.Besides the doGet( ) and doPost( ) methods, there are methods corresponding to the other HTTP methods: doDelete( ), doHea
23、d( ), 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 the DELETE and PUT HTTP methods are rarely used in a web application. It
24、9;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
25、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.Compiling and Installing a ServletTo compile a servlet, you must first ensure that you have the JAR file containing all
26、 Servlet API classes in the CLASSPATH environment variable. The JAR file is distributed with all web containers. Tomcat includes it in a file called servlet.jar, located in the common/lib directory. On a Windows platform, you include the JAR file in the CLASSPATH. Reading a RequestOne of the argumen
27、ts 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 ResponseBesides the request object, the container passes an object that implemen
28、ts 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 ListenersThe servlet specificatio
29、n 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 support this version of the specification, I'm afraid you're out of luck.FiltersA filter is a compon
30、ent that can intercept a request targeted for a servlet, 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 content or the services offered by t
31、he 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 body if the Accept-Language request header indicates that the client can handle a compressed response. A filter can be
32、 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 2.3, you could handle only session attribute binding
33、 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 HttpSession.setAttribute() method)implement the HttpSessionBindingListener interface. With the new interfaces introduced in the 2.3 version of the spe
34、cification, 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 session attribute event listener also makes it
35、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.
36、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 ListenerBeans like this typically need to be initial
37、ized 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 t
38、he 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
39、 resource initialization. This type of listener implements the javax.servlet.ServletContextListener interface, with methods called by the container when the application starts and when it shuts down.Picking the Right Component Type for Each TaskThe Project Billboard application introduced is a fairl
40、y complex application. Half the 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 sched
41、ules, and a set of pages for administration. As the application evolves, it may become hard to maintain as a pure JSP application. It's easy to forget to include the access control code in new pages. This is clearly an application that can benefit from using a combination of JSP pages and the co
42、mponent 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:l Database access should be abstracted, to avoid knowledge of a specific data schema or database engine in more than one part of th
43、e application: beans in the role of Model can be used to accomplish this.l 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. l Only authenticated users must be al
44、lowed to use the application: a filter can perform access control to satisfy this requirement. l Request processing is best done with Java code: a servlet, acting as the Controller, fits the bill. l It must be easy to change the presentation: this is where JSP shines, acting as the View. Adding serv
45、lets, listeners, and filters to the mix minimizes 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.Centralized Request Processing Using a ServletWith a servlet as the com
46、mon entry point for all application requests, you gain control over the page flow of the application. The servlet can decide which type 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 d
47、epending on the type of client making the request. With the help from some utility classes, it can also provide services such as input validation, I18N preparations, and in general, encourage a more streamlined approach to request handling. When you use a servlet as a Controller, you must deal with
48、the following basic requirements: l All requests for processing must be passed to the single Controller servlet.l The servlet must be able to distinguish requests for different types of processing. Here are other features you will want support for, even though they may not be requirements for all ap
49、plications: l A strategy for extending the application to support new types of processing requests in a flexible manner.l A mechanism for changing the page flow of the application without modifying code.Mapping Application Requests to the ServletThe first requirement for using a Controller servlet i
50、s 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'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
51、product to support servlets before the API was standardized. Most servlet containers support this convention today, even though it's not formally defined in the servlet specification.將Servlet和JSP組合使用Servlet和JSP技術(shù)是用Java開發(fā)服務(wù)器端應(yīng)用的主要技術(shù),是開發(fā)商務(wù)應(yīng)用表示端的標(biāo)準(zhǔn)。Java開發(fā)者喜歡使用它有多種原因,其一是對(duì)于已經(jīng)熟悉Java語言的開發(fā)者來說這個(gè)技術(shù)容易學(xué)習(xí);其二
52、是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ì)造成困難。事實(shí)上,在CSDN的JSP/Servlet論壇中,經(jīng)??梢钥吹揭恍┨釂?,代碼很長,可以邏輯卻不是很清晰,大量的HTML和Java代碼混雜在一起,讓人看得一頭霧水。這就是隨意開發(fā)的弊端。早期的動(dòng)態(tài)網(wǎng)頁主要采用CGI(C
53、ommon Gateway Interface,公共網(wǎng)關(guān)接口)技術(shù),你可以使用不同的語言編寫CGI程序,如VB、C/C+或Delphi等。雖然CGI技術(shù)發(fā)展成熟且功能強(qiáng)大,但由于編程困難、效率低下、修改復(fù)雜等缺點(diǎn),所以有逐漸被取代的趨勢。在所有的新技術(shù)中,JSP/Servlet具備更高效、更容易編程、功能更強(qiáng)、更安全和具有良好的可移植性,因而被許多人認(rèn)為是未來最有發(fā)展前途的動(dòng)態(tài)網(wǎng)站技術(shù)。與CGI相似,Servlet支持請求/響應(yīng)模型。當(dāng)一個(gè)客戶向服務(wù)器遞交一個(gè)請求時(shí),服務(wù)器把請求送給Servlet,Servlet負(fù)責(zé)處理請求并生成響應(yīng),然后送給服務(wù)器,再由服務(wù)器發(fā)送給客戶。與CGI不同的是,S
54、ervlet沒有生成新的進(jìn)程,而是與HTTP Server處于同一進(jìn)程中。它通過使用線程技術(shù),減小了服務(wù)器的開銷。Servlet處理請求的過程是這樣的:當(dāng)收到來自客戶端的請求后,調(diào)用service方法,該方法中Servlet先判斷到來的請求是什么類型的(GET/POST/HEAD),然后調(diào)用相應(yīng)的處理方法(doGet/doPost/doHead)并生成響應(yīng)。別看這么復(fù)雜,其實(shí)簡單說來Servlet就是一個(gè)Java類。與一般類的不同之處是,這個(gè)類運(yùn)行在一個(gè)Servlet容器內(nèi),可以提供session管理和對(duì)象生命周期管理。因而當(dāng)你使用Servlet的時(shí)候,你可以得到Java平臺(tái)的所有好處,包括安
55、全性管理、使用JDBC訪問數(shù)據(jù)庫以及跨平臺(tái)的能力。而且,Servlet使用線程,因而可以開發(fā)出效率更高的Web應(yīng)用。JSP技術(shù)是J2EE的一個(gè)關(guān)鍵技術(shù),它在更高一級(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更簡單。如果你有一個(gè)支持JSP的Web服務(wù)器,并且有一個(gè)JSP文件,你可以把它放倒任何靜態(tài)HTML文件可以放置的位置,不用編譯,不用打包,也不用進(jìn)行ClassPat
56、h的設(shè)置,就可以像訪問普通網(wǎng)頁那樣訪問它,服務(wù)器會(huì)自動(dòng)幫你做好其他的工作。JSP 文件看起來就像一個(gè)普通靜態(tài)HTML文件,只不過里面包含了一些Java代碼。它使用.jsp的后綴,用來告訴服務(wù)器這個(gè)文件需要特殊的處理。當(dāng)我們訪問一個(gè)JSP頁面的時(shí)候,這個(gè)文件首先會(huì)被JSP引擎翻譯為一個(gè)Java源文件,其實(shí)就是一個(gè)Servlet,并進(jìn)行編譯,然后像其他Servlet一樣,由Servlet引擎來處理。Servlet引擎裝載這個(gè)類,處理來自客戶的請求,并把結(jié)果返回給客戶,如下圖所示:圖1: 調(diào)用JSP頁面的流程以后再有客戶訪問這個(gè)頁面的時(shí)候,只要該文件沒有發(fā)生過更改,JSP引擎就直接調(diào)用已經(jīng)裝載的Se
57、rvlet。如果已經(jīng)做過修改的話,那就會(huì)再次執(zhí)行以上過程,翻譯、編譯并裝載。其實(shí)這就是所謂的“第一人懲罰”。因?yàn)槭状卧L問的時(shí)候要執(zhí)行一系列以上的過程,所以會(huì)耗費(fèi)一些時(shí)間;以后的訪問就不會(huì)這樣了。Java servlet提供了一種強(qiáng)有力的API,用這個(gè)API可以訪問關(guān)于請求、會(huì)話和應(yīng)用程序的所有信息。將servlet和JSP頁面組合起來使用,可以把應(yīng)用程序的邏輯部分和外觀呈現(xiàn)部分清楚地分開;換句話,利用這個(gè)方式可以對(duì)模型、視圖和控制器這三種角色分別使用最合適的組件類型。Servlet、過濾器和監(jiān)聽器Servlet是一種Java類,它使得服務(wù)器的功能可擴(kuò)展至處理請求和生成應(yīng)答。它是用Servlet
58、 API定義的類和接口實(shí)現(xiàn)的。API由兩個(gè)程序包組成:jvavax.servlet程序包包含獨(dú)立于協(xié)議的類和接口,而javax.servlet.http程序包則提供HTTP特定的擴(kuò)展的實(shí)用程序類。Servlet的實(shí)質(zhì)是實(shí)現(xiàn)了接口javax.servlet.Servlet的類,實(shí)現(xiàn)是直接完成或通過擴(kuò)展某個(gè)支持類來完成的。該接口定義了Web容器用來管理servlet和與之交互的方法。用于處理HTTP請求的servlet一般情況下都會(huì)擴(kuò)展javax.servlet.http.HttpServlet類。該類實(shí)現(xiàn)了Servlet接口,并提供了使用HTTP處理的附加方法。Servlet的生命周期Web容器
59、管理servlet生命周期的所有方面。它根據(jù)需要?jiǎng)?chuàng)建servlet類的實(shí)例、將請求傳遞給實(shí)例進(jìn)行處理,最終刪除實(shí)例。對(duì)于HttpServlet來說,容器會(huì)在servlet生命周期的適當(dāng)時(shí)間調(diào)用方法。除了doGet()和doPost()方法之外,還有一些對(duì)應(yīng)于其他HTTP方法的方法:doDelete()、doHead()、doOptiongs()、doPut()和doTrace()。一般情況下不用實(shí)現(xiàn)這些方法,因?yàn)镠ttpServlet類已經(jīng)用適用于大多數(shù)servlet的方法考慮到了HEAD、OPTIONS和TRACE請求,而且DELETE和PUT這兩種HTTP方法很少用在Web應(yīng)用程序中。容器只為每個(gè)Servlet創(chuàng)建一個(gè)實(shí)例非常重要。這意味著servlet必須是線程安全的即,能夠同時(shí)處理多個(gè)請求,每個(gè)處理都通過servlet代碼作為單獨(dú)的線程來執(zhí)行。如果只在init()和destroy()方法中修改參考的對(duì)象,而且只在請求處理方法中讀取他們,那么不用喪失任何細(xì)節(jié)就可以滿足關(guān)于實(shí)例變量的這個(gè)要求。編譯和安裝servlet要編譯servlet,必須首先確保JAR文件
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 促銷活動(dòng)促銷活動(dòng)效果對(duì)消費(fèi)者購買決策的影響分析考核試卷
- 風(fēng)險(xiǎn)評(píng)估框架構(gòu)建考核試卷
- 部編人教版一年級(jí)語文上冊全冊各單元知識(shí)點(diǎn)單元復(fù)習(xí)卡
- 部分醫(yī)療服務(wù)項(xiàng)目價(jià)格調(diào)整表
- 部編版中考道德與法治一輪復(fù)習(xí)|七年級(jí)下冊第四單元 走進(jìn)法治天地 復(fù)習(xí)學(xué)案+試卷
- 2025年中國L型單主梁吊鉤門式起重機(jī)數(shù)據(jù)監(jiān)測報(bào)告
- 2025年中國EVT扭力尺數(shù)據(jù)監(jiān)測報(bào)告
- 2025年中國BOPP雙向拉伸印刷膜數(shù)據(jù)監(jiān)測研究報(bào)告
- 2025至2030年中國GPS衛(wèi)星導(dǎo)航定位電子板市場分析及競爭策略研究報(bào)告
- 2025至2030年中國隱蔽式鉸鏈?zhǔn)袌龇治黾案偁幉呗匝芯繄?bào)告
- 中意紙質(zhì)文物脫酸技術(shù)應(yīng)用與思考
- 中央民族大學(xué)強(qiáng)基校測面試題
- 2025年安徽省中考生物試卷真題(含答案)
- 2024年中國陜西省煤炭工業(yè)行業(yè)調(diào)查研究報(bào)告
- 兩金占用管理制度
- 2025年 中國南水北調(diào)集團(tuán)新能源投資公司第一批中層及考試筆試試卷附答案
- 敘事護(hù)理學(xué)智慧樹知到答案2024年中國人民解放軍海軍軍醫(yī)大學(xué)
- 六年級(jí)主題班隊(duì)會(huì)記錄表(6個(gè)表)
- 租賃房屋交接清單
- 吊頂檢驗(yàn)報(bào)告(共5頁)
- (word完整版)山西省普通高中畢業(yè)生登記表
評(píng)論
0/150
提交評(píng)論