




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、an overview of servlet and jsp technologygildas avoine and philippe oechslinepfl, lausanne, switzerland1.1 a servlets jobservlets are java programs that run on web or application servers, acting as a middle layer between requests coming from web browsers or other http clients and databases or applic
2、ations on the http server. their job is to perform the following tasks, as illustrated in figure 1-1.figure 1-11read the explicit data sent by the client.the end user normally enters this data in an html form on a web page. however, the data could also come from an applet or a custom http client pro
3、gram.2read the implicit http request data sent by the browser.figure 1-1 shows a single arrow going from the client to the web server (the layer where servlets and jsp execute), but there are really two varieties of data: the explicit data that the end user enters in a form and the behind-the-scenes
4、 http information. both varieties are critical. the http information includes cookies, information about media types and compression schemes the browser understands, and so on.3generate the results.this process may require talking to a database, executing an rmi or ejb call, invoking a web service,
5、or computing the response directly. your real data may be in a relational database. fine. but your database probably doesnt speak http or return results in html, so the web browser cant talk directly to the database. even if it could, for security reasons, you probably would not want it to. the same
6、 argument applies to most other applications. you need the web middle layer to extract the incoming data from the http stream, talk to the application, and embed the results inside a document.4send the explicit data (i.e., the document) to the client.this document can be sent in a variety of formats
7、, including text (html or xml), binary (gif images), or even a compressed format like gzip that is layered on top of some other underlying format. but, html is by far the most common format, so an important servlet/jsp task is to wrap the results inside of html.5send the implicit http response data.
8、figure 1-1 shows a single arrow going from the web middle layer (the servlet or jsp page) to the client. but, there are really two varieties of data sent: the document itself and the behind-the-scenes http information. again, both varieties are critical to effective development. sending http respons
9、e data involves telling the browser or other client what type of document is being returned (e.g., html), setting cookies and caching parameters, and other such tasks. 1.2 why build web pages dynamically?many client requests can be satisfied by prebuilt documents, and the server would handle these r
10、equests without invoking servlets. in many cases, however, a static result is not sufficient, and a page needs to be generated for each request. there are a number of reasons why web pages need to be built on-the-fly:1 the web page is based on data sent by the client.for instance, the results page f
11、rom search engines and order-confirmation pages at online stores are specific to particular user requests. you dont know what to display until you read the data that the user submits. just remember that the user submits two kinds of data: explicit (i.e., html form data) and implicit (i.e., http requ
12、est headers). either kind of input can be used to build the output page. in particular, it is quite common to build a user-specific page based on a cookie value.2the web page is derived from data that changes frequently.if the page changes for every request, then you certainly need to build the resp
13、onse at request time. if it changes only periodically, however, you could do it two ways: you could periodically build a new web page on the server (independently of client requests), or you could wait and only build the page when the user requests it. the right approach depends on the situation, bu
14、t sometimes it is more convenient to do the latter: wait for the user request. for example, a weather report or news headlines site might build the pages dynamically, perhaps returning a previously built page if that page is still up to date.3the web page uses information from corporate databases or
15、 other server-side sources.if the information is in a database, you need server-side processing even if the client is using dynamic web content such as an applet. imagine using an applet by itself for a search engine site:downloading 50 terabyte applet, please wait! obviously, that is silly; you nee
16、d to talk to the database. going from the client to the web tier to the database (a three-tier approach) instead of from an applet directly to a database (a two-tier approach) provides increased flexibility and security with little or no performance penalty. after all, the database call is usually t
17、he rate-limiting step, so going through the web server does not slow things down. in fact, a three-tier approach is often faster because the middle tier can perform caching and connection pooling.in principle, servlets are not restricted to web or application servers that handle http requests but ca
18、n be used for other types of servers as well. for example, servlets could be embedded in ftp or mail servers to extend their functionality. and, a servlet api for sip (session initiation protocol) servers was recently standardized (see /en/jsr/detail?id=116). in practice, however, this
19、use of servlets has not caught on, and well only be discussing http servlets.1.3 the advantages of servlets over traditional cgijava servlets are more efficient, easier to use, more powerful, more portable, safer, and cheaper than traditional cgi and many alternative cgi-like technologies.1efficient
20、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, no
21、t 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 a single copy of the servlet class would be loaded. this ap
22、proach 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 computations, keep database connections open, and perform other optimizations that rely
23、on persistent data. servlets, however, remain in memory even after they complete a response, so it is straightforward to store arbitrarily complex data between client requests.2convenientservlets have an extensive infrastructure for automatically parsing and decoding html form data, reading and sett
24、ing http headers, handling cookies, tracking sessions, and many other such high-level utilities. in cgi, you have to do much of this yourself. besides, if you already know the java programming language, why learn perl too? youre already convinced that java technology makes for more reliable and reus
25、able code than does visual basic, vbscript, or c+. why go back to those languages for server-side programming?3powerfulservlets support several capabilities that are difficult or impossible to accomplish with regular cgi. servlets can talk directly to the web server, whereas regular cgi programs can
26、not, at least not without using a server-specific api. communicating with the web server makes it easier to translate relative urls into concrete path names, for instance. multiple servlets can also share data, making it easy to implement database connection pooling and similar resource-sharing opti
27、mizations. servlets can also maintain information from request to request, simplifying techniques like session tracking and caching of previous computations.4portableservlets are written in the java programming language and follow a standard api. servlets are supported directly or by a plugin on vir
28、tually every major web server. consequently, servlets written for, say, macromedia jrun can run virtually unchanged on apache tomcat, microsoft internet information server (with a separate plugin), ibm websphere, iplanet enterprise server, oracle9i as, or starnine webstar. they are part of the java
29、2 platform, enterprise edition (j2ee; see /j2ee/), so industry support for servlets is becoming even more pervasive.5inexpensivea number of free or very inexpensive web servers are good for development use or deployment of low- or medium-volume web sites. thus, with servlets and js
30、p you can start with a free or inexpensive server and migrate to more expensive servers with high-performance capabilities or advanced administration utilities only after your project meets initial success. this is in contrast to many of the other cgi alternatives, which require a significant initia
31、l investment for the purchase of a proprietary package.price and portability are somewhat connected. for example, marty tries to keep track of the countries of readers that send him questions by email. india was near the top of the list, probably #2 behind the u.s. marty also taught one of his jsp a
32、nd servlet training courses (see /) in manila, and there was great interest in servlet and jsp technology there.now, why are india and the philippines both so interested? we surmise that the answer is twofold. first, both countries have large pools of well-educated soft
33、ware developers. second, both countries have (or had, at that time) highly unfavorable currency exchange rates against the u.s. dollar. so, buying a special-purpose web server from a u.s. company consumed a large part of early project funds.but, with servlets and jsp, they could start with a free se
34、rver: apache tomcat (either standalone, embedded in the regular apache web server, or embedded in microsoft iis). once the project starts to become successful, they could move to a server like caucho resin that had higher performance and easier administration but that is not free. but none of their
35、servlets or jsp pages have to be rewritten. if their project becomes even larger, they might want to move to a distributed (clustered) environment. no problem: they could move to macromedia jrun professional, which supports distributed applications (web farms). again, none of their servlets or jsp p
36、ages have to be rewritten. if the project becomes quite large and complex, they might want to use enterprise javabeans (ejb) to encapsulate their business logic. so, they might switch to bea weblogic or oracle9i as. again, none of their servlets or jsp pages have to be rewritten. finally, if their p
37、roject becomes even bigger, they might move it off of their linux box and onto an ibm mainframe running ibm websphere. but once again, none of their servlets or jsp pages have to be rewritten.6secureone of the main sources of vulnerabilities in traditional cgi stems from the fact that the programs a
38、re often executed by general-purpose operating system shells. so, the cgi programmer must be careful to filter out characters such as backquotes and semicolons that are treated specially by the shell. implementing this precaution is harder than one might think, and weaknesses stemming from this prob
39、lem are constantly being uncovered in widely used cgi libraries.a second source of problems is the fact that some cgi programs are processed by languages that do not automatically check array or string bounds. for example, in c and c+ it is perfectly legal to allocate a 100-element array and then wr
40、ite into the 999th element, which is really some random part of program memory. so, programmers who forget to perform this check open up their system to deliberate or accidental buffer overflow attacks.servlets suffer from neither of these problems. even if a servlet executes a system call (e.g., wi
41、th runtime.exec or jni) to invoke a program on the local operating system, it does not use a shell to do so. and, of course, array bounds checking and other memory protection features are a central part of the java programming language.7mainstreamthere are a lot of good technologies out there. but i
42、f vendors dont support them and developers dont 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/iplanet, new atlanta, atg, fujitsu, lutris, silverstream, the world wide web consortium (w3c), a
43、nd many others. several low-cost 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 devel
44、oping medium to large web applications. they are used by the airline industry (most united airlines and delta airlines web sites), e-commerce (), online banking (first usa bank, banco popular de puerto rico), web search engines/portals (), large financial sites (american century i
45、nvestments), and hundreds of other sites that you visit every day.of course, popularity alone is no proof of good technology. numerous counter-examples abound. but our point is that you are not experimenting with a new and unproven technology when you work with server-side java.servlet和jsp技術(shù)簡述gildas
46、 avoine and philippe oechslinepfl, lausanne, switzerland1.1 servlet的功能servlets是運行在web或應(yīng)用服務(wù)器上的java程序,它是一個中間層,負(fù)責(zé)連接來自web瀏覽器或其他http客戶程序的請求和http服務(wù)器上的數(shù)據(jù)庫或應(yīng)用程序。servlet的工作是執(zhí)行西門的任務(wù),如圖1.1所示 。圖1.1web中間件的作用(1) 讀取客戶發(fā)送的顯式數(shù)據(jù)。最終用戶一般在頁面的html表單中輸入這些數(shù)據(jù)。然而,數(shù)據(jù)還有可能來自applet或定制的http客戶程序。(2) 讀取由瀏覽器發(fā)送的隱式請求數(shù)據(jù)。圖1.1中顯示了一條從客戶端到w
47、eb服務(wù)器的單箭頭,但實際上從客戶端傳送到web服務(wù)器的數(shù)據(jù)有兩種,它們分別為用戶在表單中輸入的顯式數(shù)據(jù),以及后臺的http信息。兩種數(shù)據(jù)都很重要。http信息包括cookie、瀏覽器所能識別的媒體類型和壓縮模式等。(3) 生成結(jié)果。這個過程可能需要訪問數(shù)據(jù)庫、執(zhí)行rmi或ejb調(diào)用、調(diào)用web服務(wù),或者直接計算得出對應(yīng)的響應(yīng)。實際的數(shù)據(jù)可能存儲在關(guān)系型數(shù)據(jù)庫中。該數(shù)據(jù)庫可能不理解http,或者不能返回html形式的結(jié)果,所有web瀏覽器不能直接與數(shù)據(jù)庫進行會話。即使它能夠做到這一點,為了安全上的考慮,我們也不希望讓它這么做。對應(yīng)大多數(shù)其他應(yīng)用程序,也存在類似的問題。因此,我們需要web中間層
48、從http流中提取輸入數(shù)據(jù),與應(yīng)用程序會話,并將結(jié)果嵌入到文檔中。(4) 向客戶發(fā)送顯式數(shù)據(jù)(即文檔)。這個文檔可以用各種格式發(fā)送,包括文本(html或xml),二進制(gif圖),甚至可以式建立在其他底層格式之上的壓縮格式,如gzip。但是,到目前為止,html式最常用的格式,故而servelt和jsp的重要任務(wù)之一就式將結(jié)果包裝到html中。(5) 發(fā)送隱式的http響應(yīng)數(shù)據(jù)。圖1.1中顯示了一條從web中間層到客戶端的單箭頭。但是,實際發(fā)送的數(shù)據(jù)有兩種:文檔本身,以及后臺的http信息。同樣,兩種數(shù)據(jù)對開發(fā)來說都式至關(guān)重要的。http響應(yīng)數(shù)據(jù)的發(fā)送過程涉及告知瀏覽器或其他客戶程序所返回文
49、檔的類型(如html),設(shè)置cookie和緩存參數(shù),以及其他類似的任務(wù)。1.2 動態(tài)構(gòu)建網(wǎng)頁的原因預(yù)先建立的文檔可以滿足客戶的許多請求,服務(wù)器無需調(diào)用servlet就可以處理這些請求。然而,許多情況下靜態(tài)的結(jié)果不能滿足要求,我們需要針對每個請求生成一個頁面。實時構(gòu)建頁面的理由有很多種:1、網(wǎng)頁基于客戶發(fā)送的數(shù)據(jù)。例如,搜索引擎生成的頁面,以及在線商店的訂單確認(rèn)頁面,都要針對特定的用戶請求而產(chǎn)生。在沒有讀取到用戶提交的數(shù)據(jù)之前,我們不知道應(yīng)該顯示什么。要記住,用戶提交兩種類型的數(shù)據(jù):顯示(即html表單的數(shù)據(jù))和隱式(即http請求的報頭)。兩種輸入都可用來構(gòu)建輸出頁面。基于cookie值針對具
50、體用戶構(gòu)建頁面的情況尤其普遍。2、頁面由頻繁改變的數(shù)據(jù)導(dǎo)出。如果頁面需要根據(jù)每個具體的請求做出相應(yīng)的改變,當(dāng)然需要在請求發(fā)生時構(gòu)建響應(yīng)。但是,如果頁面周期性地改變,我們可以用兩種方式來處理它:周期性地在服務(wù)器上構(gòu)建新的頁面(和客戶請求無關(guān)),或者僅僅在用戶請求該頁面時再構(gòu)建。具體應(yīng)該采用哪種方式要根據(jù)具體情況而定,但后一種方式常常更為方便,因為它只需簡單地等待用戶的請求。例如,天氣預(yù)報或新聞網(wǎng)站可能會動態(tài)地構(gòu)建頁面,也有可能會返回之前構(gòu)建的頁面(如果它還是最新的話)。3、頁面中使用了來自公司數(shù)據(jù)庫或其他數(shù)據(jù)庫斷數(shù)據(jù)源的信息。如果數(shù)據(jù)存儲在數(shù)據(jù)庫中,那么,即使客戶端使用動態(tài)web內(nèi)容,比如app
51、let,我們依舊需要執(zhí)行服務(wù)器端處理。想象以下,如果一個搜索引擎網(wǎng)站完全使用applet,那么用戶將會看到:“正在下載50tb的applet,請等待!”。顯然,這樣很愚蠢;這種情況下,我們需要與數(shù)據(jù)庫進行會話。從客戶端到web層再到數(shù)據(jù)庫(三層結(jié)構(gòu)),要比從applet直接到數(shù)據(jù)庫(二層結(jié)構(gòu))更靈活,也更安全,而性能上的損失很少甚至沒有。畢竟數(shù)據(jù)庫調(diào)用通常是對速度影響最大的步驟,因而,經(jīng)過中間層可以執(zhí)行高速緩存和連接共享。理論上講,servelt并非只用于處理http請求的web服務(wù)器或應(yīng)用服務(wù)器,它同樣可以用于其他類型的服務(wù)器。例如,servlet能夠嵌入到ftp或郵件服務(wù)器中,擴展他們的功
52、能。而且,用于會話啟動協(xié)議服務(wù)器的servlet api最近已經(jīng)被標(biāo)準(zhǔn)化(參見/en/jsr/detail?id=116)。但在實踐中,servelt的這種用法尚不流行,在此,我們只論述http servlet。1.3 servlet相對于“傳統(tǒng)”cgi的優(yōu)點和傳統(tǒng)cgi及許多類cgi技術(shù)相比,java servelt效率更高、更易用、更強大、更容易移植、更安全、也更廉價。1、效率 應(yīng)用傳統(tǒng)的cgi,針對每個http請求都用啟動一個新的進程。如果cgi程序自身相對比較簡短,那么啟動進程的開銷會占用大部分執(zhí)行時間。而使用servelt,java虛擬機會一直運行,并用輕量
53、級的java線程處理每個請求,而非重量級的操作系統(tǒng)進程。類似地,應(yīng)用傳統(tǒng)的cgi技術(shù),如果存在對同一cgi程序的n個請求,那么cgi程序的代碼會載入內(nèi)存n次。同樣的情況,如果使用servlet則啟動n個線程,單僅僅載入servlet類的單一副本。這種方式減少了服務(wù)器的內(nèi)存需求,通過實例化更少的對象從而節(jié)省了時間。最后,當(dāng)cgi程序結(jié)束對請求的處理之后,程序結(jié)束。這種方式難以緩存計算結(jié)果,保持?jǐn)?shù)據(jù)庫連接打開,或是執(zhí)行依靠持續(xù)性數(shù)據(jù)的其他優(yōu)化。然而,servelt會一直停留在內(nèi)存中(即使請求處理完畢),因而可以直接存儲客戶請求之間的任意復(fù)雜數(shù)據(jù)。2、便利servelt提供大量的基礎(chǔ)構(gòu)造,可以自動分
54、析和解碼html的表單數(shù)據(jù),讀取和設(shè)置http報頭,處理cookie,跟蹤會話,以及其他次類高級功能。而在cgi中,大部分工作都需要我們資金完成。另外,如果您已經(jīng)了解了java編程語言,為什么還有學(xué)校perl呢?您已經(jīng)承認(rèn)應(yīng)用java技術(shù)編寫的代碼要比visual basic,vbscript或c編寫的代碼更可靠,且更易重用,為什么還有倒退回去選擇那些語言來開發(fā)服務(wù)器端的程序呢?3、強大 servlet支持常規(guī)cgi難以實現(xiàn)或根本不能實現(xiàn)的幾項功能。servlet能夠直接于web服務(wù)器對話,而常規(guī)的cgi程序做不到這一點,至少在不使用服務(wù)器專有api的情況下是這樣。例如,與web服務(wù)器的通信使得講相對url轉(zhuǎn)換成具體的路徑名變得更為容易。多個servelt還可以共享數(shù)據(jù),從而易于實現(xiàn)數(shù)據(jù)庫連接共享和類似的資源共享優(yōu)化。servelt還能維護請求之間的信息,使得諸如會話跟蹤和計算結(jié)果緩存等技術(shù)變得更為簡單。4、可移植性servelt使用java編程語言,并且遵循標(biāo)準(zhǔn)的api。所有主要的web服務(wù)器。實際上都直接或通過插件支持servlet。因此。
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 山東滕州市八年級政治上冊 第1單元 讓愛駐我家 第2課 我們共有一個家教學(xué)實錄與檢測 魯教版
- 達州市通川區(qū)楊家溝煤業(yè)有限公司楊家溝煤礦礦山地質(zhì)環(huán)境保護與土地復(fù)墾方案情況
- 四川化工職業(yè)技術(shù)學(xué)院
- 肝膿腫護理相關(guān)知識
- 【人教PEP版英語四年級下冊】期中測試卷6
- 人教版小學(xué)四年級語文下冊2024-2025學(xué)年度第二學(xué)期第一單元質(zhì)量檢測試卷含參考答案
- 人教版小學(xué)四年級語文下冊2024-2025學(xué)年度第二學(xué)期第八單元質(zhì)量檢測試卷
- 第5單元 第14課 新年賀卡-綜合制作-教學(xué)設(shè)計2023-2024學(xué)年清華大學(xué)版(2012)初中信息技術(shù)八年級上冊001
- 網(wǎng)絡(luò)安全運維專家簡歷
- 安徽省部分地市2024-2025學(xué)年高三下學(xué)期2月聯(lián)合考試物理試題(解析版)
- 2025年懷化職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫必考題
- 2025年第六屆(中小學(xué)組)國家版圖知識競賽測試題庫及答案
- 2025年中國床墊機械行業(yè)市場發(fā)展監(jiān)測及投資戰(zhàn)略咨詢報告
- C小學(xué)一起諾如病毒胃腸炎疫情的調(diào)查與處置課件
- 2025年鎵礦采選項目投資可行性研究分析報告
- 歐泰科-吊掛軟件使用教程
- 公安局網(wǎng)安大隊工作總結(jié)
- 2025年裝備制造創(chuàng)新中心北京石油機械有限公司招聘筆試參考題庫附帶答案詳解
- 教科版六年級下冊科學(xué)全冊教學(xué)設(shè)計教案
- 2025年哈爾濱鐵道職業(yè)技術(shù)學(xué)院高職單招高職單招英語2016-2024年參考題庫含答案解析
- 病理學(xué)與病理生理學(xué)考試題
評論
0/150
提交評論