




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、jsp基礎(chǔ)學(xué)習(xí)資料一、 jsp 技術(shù)概述 在 sun 正式發(fā)布 jsp(javaserver pages) 之后,這種新的 web 應(yīng)用開發(fā)技術(shù)很快引起了人們的關(guān)注。 jsp 為創(chuàng)建高度動態(tài)的 web 應(yīng)用提供了一個獨(dú)特的開發(fā)環(huán)境。按照 sun 的說法, jsp 能夠適應(yīng)市場上包括 apache webserver 、 iis4.0 在內(nèi)的 85% 的服務(wù)器產(chǎn)品。即使您對 asp “一往情深”,我們認(rèn)為,關(guān)注 jsp 的發(fā)展仍舊很有必要。 jsp 與 asp 的簡單比較 jsp 與 microsoft 的 asp 技術(shù)非常相似。兩者都提供在 html 代碼中混合某種程序代碼、由語言引擎解釋執(zhí)行
2、程序代碼的能力。在 asp 或 jsp 環(huán)境下, html 代碼主要負(fù)責(zé)描述信息的顯示樣式,而程序代碼則用來描述處理邏輯。普通的 html 頁面只依賴于 web 服務(wù)器,而 asp 和 jsp 頁面需要附加的語言引擎分析和執(zhí)行程序代碼。程序代碼的執(zhí)行結(jié)果被重新嵌入到 html 代碼中,然后一起發(fā)送給瀏覽器。 asp 和 jsp 都是面向 web 服務(wù)器的技術(shù),客戶端瀏覽器不需要任何附加的軟件支持。 asp 的編程語言是 vbscript 之類的腳本語言, jsp 使用的是 java ,這是兩者最明顯的區(qū)別。此外, asp 與 jsp 還有一個更為本質(zhì)的區(qū)別:兩種語言引擎用完全不同的方式處理頁面
3、中嵌入的程序代碼。在 asp 下, vbscript 代碼被 asp 引擎解釋執(zhí)行;在 jsp 下,代碼被編譯成 servlet 并由 java 虛擬機(jī)執(zhí)行,這種編譯操作僅在對 jsp 頁面的第一次請求時發(fā)生。 運(yùn)行環(huán)境 sun 公司的 jsp 主頁在 ,從這里還可以下載 jsp 規(guī)范,這些規(guī)范定義了供應(yīng)商在創(chuàng)建 jsp 引擎時所必須遵從的一些規(guī)則。 執(zhí)行 jsp 代碼需要在服務(wù)器上安裝 jsp 引擎。此處我們使用的是 sun 的 javaserver web development kit ( jswdk )。為便于學(xué)習(xí),這個軟件包提供了大量可供修改的示例。安裝 jswdk 之后,只需執(zhí)行
4、startserver 命令即可啟動服務(wù)器。在默認(rèn)配置下服務(wù)器在端口 8080 監(jiān)聽,使用 http:/localhost:8080 即可打開缺省頁面。 在運(yùn)行 jsp 示例頁面之前,請注意一下安裝 jswdk 的目錄,特別是“ work ”子目錄下的內(nèi)容。執(zhí)行示例頁面時,可以在這里看到 jsp 頁面如何被轉(zhuǎn)換成 java 源文件,然后又被編譯成 class 文件(即 servlet )。 jswdk 軟件包中的示例頁面分為兩類,它們或者是 jsp 文件,或者是包含一個表單的 html 文件,這些表單均由 jsp 代碼處理。與 asp 一樣, jsp 中的 java 代碼均在服務(wù)器端執(zhí)行。因此
5、,在瀏覽器中使用“查看源文件”菜單是無法看到 jsp 源代碼的,只能看到結(jié)果 html 代碼。所有示例的源代碼均通過一個單獨(dú)的“ examples ”頁面提供。 jsp 頁面示例 下面我們分析一個簡單的 jsp 頁面。您可以在 jswdk 的 examples 目錄下創(chuàng)建另外一個目錄存放此文件,文件名字可以任意,但擴(kuò)展名必須為 .jsp 。從下面的代碼清單中可以看到, jsp 頁面除了比普通 html 頁面多一些 java 代碼外,兩者具有基本相同的結(jié)構(gòu)。 java 代碼是通過 符號加入到 html 代碼中間的,它的主要功能是生成并顯示一個從 0 到 9 的字符串。在這個字符串的前面和后面都是
6、一些通過 html 代碼輸出的文本。 jsp 頁面 % for (int i=1; i jsp 輸出之前。 jsp 輸出之后。 這個 jsp 頁面可以分成幾個部分來分析。 首先是 jsp 指令。它描述的是頁面的基本信息,如所使用的語言、是否維持會話狀態(tài)、是否使用緩沖等。 jsp 指令由 結(jié)束。在本例中,指令“ ”只簡單地定義了本例使用的是 java 語言(當(dāng)前,在 jsp 規(guī)范中 java 是唯一被支持的語言)。 接下來的是 jsp 聲明。 jsp 聲明可以看成是定義類這一層次的變量和方法的地方。 jsp 聲明由 結(jié)束。如本例中的“ ”定義了一個字符串變量。在每一項聲明的后面都必須有一個分號,
7、就象在普通 java 類中聲明成員變量一樣。 位于 之間的代碼塊是描述 jsp 頁面處理邏輯的 java 代碼,如本例中的 for 循環(huán)所示。 最后,位于 之間的代碼稱為 jsp 表達(dá)式,如本例中的“ ”所示。 jsp 表達(dá)式提供了一種將 jsp 生成的數(shù)值嵌入 html 頁面的簡單方法。 二、會話狀態(tài)管理 會話狀態(tài)維持是 web 應(yīng)用開發(fā)者必須面對的問題。有多種方法可以用來解決這個問題,如使用 cookies 、隱藏的表單輸入域,或直接將狀態(tài)信息附加到 url 中。 java servlet 提供了一個在多個請求之間持續(xù)有效的會話對象,該對象允許用戶存儲和提取會話狀態(tài)信息。 jsp 也同樣支
8、持 servlet 中的這個概念。 在 sun 的 jsp 指南 中可以看到許多有關(guān)隱含對象的說明(隱含的含義是,這些對象可以直接引用,不需要顯式地聲明,也不需要專門的代碼創(chuàng)建其實(shí)例)。例如 request 對象,它是 httpservletrequest 的一個子類。該對象包含了所有有關(guān)當(dāng)前瀏覽器請求的信息,包括 cookies , html 表單變量等等。 session 對象也是這樣一個隱含對象。這個對象在第一個 jsp 頁面被裝載時自動創(chuàng)建,并被關(guān)聯(lián)到 request 對象上。與 asp 中的會話對象相似, jsp 中的 session 對象對于那些希望通過多個頁面完成一個事務(wù)的應(yīng)用是
9、非常有用的。 為說明 session 對象的具體應(yīng)用,接下來我們用三個頁面模擬一個多頁面的 web 應(yīng)用。第一個頁面( q1.html )僅包含一個要求輸入用戶名字的 html 表單,代碼如下: 請輸入您的姓名: 第二個頁面是一個 jsp 頁面( q2.jsp ),它通過 request 對象提取 q1.html 表單中的 thename 值,將它存儲為 name 變量,然后將這個 name 值保存到 session 對象中。 session 對象是一個名字 / 值對的集合,在這里,名字 / 值對中的名字為“ thename ”,值即為 name 變量的值。由于 session 對象在會話期間
10、是一直有效的,因此這里保存的變量對后繼的頁面也有效。 q2.jsp 的另外一個任務(wù)是詢問第二個問題。下面是它的代碼: 您的姓名是: 您喜歡吃什么 ? 第三個頁面也是一個 jsp 頁面( q3.jsp ),主要任務(wù)是顯示問答結(jié)果。它從 session 對象提取 thename 的值并顯示它,以此證明雖然該值在第一個頁面輸入,但通過 session 對象得以保留。 q3.jsp 的另外一個任務(wù)是提取在第二個頁面中的用戶輸入并顯示它: 您的姓名是: 您喜歡吃: 三、引用 javabean 組件 javabean 是一種基于 java 的軟件組件。 jsp 對于在 web 應(yīng)用中集成 javabean
11、 組件提供了完善的支持。這種支持不僅能縮短開發(fā)時間(可以直接利用經(jīng)測試和可信任的已有組件,避免了重復(fù)開發(fā)),也為 jsp 應(yīng)用帶來了更多的可伸縮性。 javabean 組件可以用來執(zhí)行復(fù)雜的計算任務(wù),或負(fù)責(zé)與數(shù)據(jù)庫的交互以及數(shù)據(jù)提取等。如果我們有三個 javabean ,它們分別具有顯示新聞、股票價格、天氣情況的功能,則創(chuàng)建包含所有這三種功能的 web 頁面只需要實(shí)例化這三個 bean ,使用 html 表格將它們依次定位就可以了。 為說明在 jsp 環(huán)境下 javabean 的應(yīng)用,我們創(chuàng)建了一個名為 taxrate 的 bean 。它有兩個屬性,即 product (產(chǎn)品)和 rate (
12、稅率)。兩個 set 方法分別用來設(shè)置這兩個屬性,兩個 get 方法則用于提取這兩個屬性。在實(shí)際應(yīng)用中,這種 bean 一般應(yīng)當(dāng)從數(shù)據(jù)庫提取稅率值,此處我們簡化了這個過程,允許任意設(shè)定稅率。下面是這個 bean 的代碼清單: package tax; public class taxrate string product; double rate; public taxrate() this.product = a001; this.rate = 5; public void setproduct (string productname) this.product = productname;
13、 public string getproduct() return (this.product); public void setrate (double ratevalue) this.rate = ratevalue; public double getrate () return (this.rate); 在 jsp 頁面中應(yīng)用上述 bean 要用到 標(biāo)記。依賴于具體使用的 jsp 引擎的不同,在何處配置以及如何配置 bean 的方法也可能略有不同。本文將這個 bean 的 .class 文件放在 c:jswdk-1.0examplesweb-infjspeans ax 目錄下,這里的
14、 tax 是一個專門存放該 bean 的目錄。下面是一個應(yīng)用上述 bean 的示例頁面: 使用方法 1 : 產(chǎn)品 : 稅率 : 使用方法 2 : 產(chǎn)品 : 稅率 : 在 標(biāo)記內(nèi)定義了幾個屬性,其中 id 是整個 jsp 頁面內(nèi)該 bean 的標(biāo)識, scope 屬性定義了該 bean 的生存時間, class 屬性說明了該 bean 的類文件(從包名開始)。 這個 jsp 頁面不僅使用了 bean 的 set 和 get 方法設(shè)置和提取屬性值,還用到了提取 bean 屬性值的第二種方法,即使用 標(biāo)記。 中的 name 屬性即為 中定義的 bean 的 id ,它的 property 屬性指定的
15、是目標(biāo)屬性的名字。 事實(shí)證明, java servlet 是一種開發(fā) web 應(yīng)用的理想構(gòu)架。 jsp 以 servlet 技術(shù)為基礎(chǔ),又在許多方面作了改進(jìn)。 jsp 頁面看起來象普通 html 頁面,但它允許嵌入執(zhí)行代碼,在這一點(diǎn)上,它和 asp 技術(shù)非常相似。利用跨平臺運(yùn)行的 javabean 組件, jsp 為分離處理邏輯與顯示樣式提供了卓越的解決方案。 jsp 必將成為 asp 技術(shù)的有力競爭者。the jsp basic learning material1. jsp technology overviewin from the official launch jsp (javase
16、rver web), then the new web application development skills quickly to cause the attention of people. the jsp for creating highly dynamic web application provides a unique development environment. according to the statement from can adapt to the market, the jsp webserver, including i can with apache
17、iis4.0, 85% of server products. even if you the asp madly, we believe, paying attention to the development of jsp are still very be necessary.(1)jsp simple compared with aspthe jsp and microsoft of asp technology are very similar. both offer in html code mixed some code, by the language engine inter
18、pretive execution codes ability. in asp and jsp environment, html code is mainly responsible for describe information display, and program code is used to describe handling logic. normal html page only depends on the web server and the asp and jsp page need additional language engine analysis and im
19、plementation program code. the program code to be executing embedded into html code, then the message to all browsers. asp and jsp are facing the web server technology, the client browser does not need any additional software support.asp programming language is vbscript such scripting language, jsp
20、use is java, this is both one of the most significant differences. in addition, asp and jsp more essential difference: two languages in a totally different way engine handling page embedded program code. in the asp, vbscript code is asp engines interpret execution; in the jsp, code has been compiled
21、 into java virtual machine by servlet and execution, the compiler operation is only on the jsp page first request happen.(2)dimension of running environmentfrom the jsp page in from here can also download the jsp specification, these standard defines the supplier in creating jsp engine when must com
22、ply to some rules.execute jsp code needed on the server installation jsp engine. here we use is from the development kit (javaserver web jswdk). to facilitate learning, this package offers a lot for modification examples. jswdk after installation, just need to perform startserver command can server.
23、 the default configuration server in the port 8080 surveillance, use http:/localhost:8080 can open default page.in running the jsp sample page before installation, please pay attention to the jswdk directory, especially schools subdirectories of content. execute the sample pages, here can see how th
24、e jsp page are converted into java source file, then compiled into scale-up file (i.e. servlet). jswdk packages examples in the page is divided into two categories, they or jsp files, or is included in a form of html files, these forms all by jsp code processing. and as the java, jsp asp code are ex
25、ecuted on the server. therefore, in the browser use the view source menu is unable to see the jsp code, can see the results html code. all the source code examples are by a single provide examples page.eclipse is an open source, based on a java extensible development platform. eclipse it just a fram
26、ework and a set of service, used to construct the development environment through plug-ins components, the key is eclipse comes in a standard plugin sets, including java development tools (java development tools, jdt). the eclipse is developed by ibm alternative commercial software for the next gene
27、ration of java visual age-related ide development environment, november 2001 contribution to the open source community, now by a non-profit software vendors alliance eclipse foundation (eclipse foundation) management.(3)jsp page examples below we analyze a simple jsp page. you can jswdk examples in
28、the directory create another directory store this file, the file name can be arbitrary, but extensions must serve. jsp. from the code below the list can see, except the jsp page than ordinary html page more java code outside, both has the same basic structure. java code is through symbols to join in
29、 the middle of the html code, its main function is to generate and display a from 0 to 9 string. in the string in the front and rear of the html code that some are through the text output. jsp page % for (int i=1; i jsp before out。 jsp after out。 the jsp page can be divided into several parts for an
30、alysis.first is the jsp instructions. it describes the basic information of the page, such as the use of language, whether to maintain conversation status, whether to use cushion etc. the jsp instructions by over. in this example, directive simply defines this example is using java language (at pres
31、ent, in the jsp specification in java is the only support language).next came the jsp statement. the jsp statement may be regarded as the definition of this level of variables and method of place. the jsp statement by over. if the cases of defines a string variable. in every statement behind must ha
32、ve a semicolon, just like in ordinary java class declaration in the same member variables.located in between the code block is to describe the jsp page handling logic of java code, such as the example shown the seas cycle.finally, located in between code is called the jsp expression, such as the exa
33、mple of below. the jsp expression provides a will jsp generated numerical embedded html pages of simple method.2. the session state managementthe session state maintain is the web application developers must face the problem. there are various ways can be used to solve this problem, if use cookies,
34、hidden form input domain, or directly to the url in additional status information. java servlet provides a continuous effectively in multiple requests the conversation between objects, the object allows users to store and retrieve the session state information. the jsp also support servlet of this c
35、oncept.in the jsp from guidelines can see many relevant implied object instructions (implicit meaning is that these objects can directly referenced, do not need explicit statement, also do not need special code to create actually cases). for example that object, it is the httpservletrequest a derive
36、d class. the object contains all the related current browser requests information, including cookies, html form variables, etc. session object is also such a hidden objects. this object in the first jsp page is loaded, and automatically created by related to that objects. the conversation with asp o
37、bject of similar, jsp session object for those who hope that through multiple pages to complete a affairs application is very useful.to illustrate the session object concrete application, next we use three pages more than a page of simulation web application. the first page (q1. html) contain only a
38、 requirement to enter your user name html forms, the html code is as follows: please write your name: the second page is a jsp page (q2. jsp), it through that object extraction in q1. html form thename value, it will be stored for name variable, then will the name value saved to the session objects.
39、 session object is a name/value pairs set, here, name/value pairs of the name is thename, namely for name values of the value of the variable. due in session during the session object is effective until, so here preserved variables on subsequent page as well. q2. jsp another task is to ask the secon
40、d question. below is its code: what is your name: what do you want to eat? the third page is a jsp page (percentile. jsp), main task is to show the q&a results. it from the value of the thename session object extraction and display it, prove the value in the first though page input, but through sess
41、ion object is maintained. percentile. jsp another task is to extract the user input at the second page and displays it: your name is: your favorite food is: 3. citing javabean componentsbased on a java javabean is a kind of software component. in web applications for the jsp integrated javabean comp
42、onent provides the perfect support. this support can not only shorten time (can use directly by test and trustworthy of existing components, to avoid the repeated development), but also for the jsp application has brought more scalability. javabean components can be used to execute complex computing
43、 tasks, or responsible and database of interaction and data extraction, etc. if we have three javabean, they respectively display news, stock price, weather conditions function, they create contain all these three functions of web page only need to instantiate the three bean, using html forms will b
44、e they in turn positioning is ok.to illustrate the jsp javabean application environment, we created a name of taxrate bean. it has two attributes that our product (products) and rate (tax). two set separately used to set the two properties, and two the get method is used to extract the two attribute
45、s. in practical applications, this kind of bean shall generally be from a database abstraction tax rate, here we simplify the value of this process, allows any setting rate. below is the bean code list: package tax; public class taxrate string product; double rate; public taxrate() this.product = a001; this.rate = 5; public void setproduct (string productname) this.product = productname; public string getproduct() return (this.product); public void setrate (double ratevalue) this.rate = ratevalue; public double getrate () return (this.rate); in applying the above jsp
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國手提式工作燈市場調(diào)查研究報告
- 2025年中國全毛強(qiáng)縮絨布市場調(diào)查研究報告
- 2025年中國休閑茶臺市場調(diào)查研究報告
- 出租大巴車合同范例
- 光明美甲店轉(zhuǎn)讓合同范例
- 事業(yè)人員合同范例
- 冷鏈裝卸工合同范例
- 云服務(wù) 合同范例
- app復(fù)制合同范例
- 公路人工合同范例
- 新能源汽車概論課件 3.1認(rèn)知純電動汽車
- 河北美術(shù)出版社小學(xué)六年級下冊書法練習(xí)指導(dǎo)教案
- 《食品標(biāo)準(zhǔn)與法規(guī)》知識考試題庫300題(含答案)
- 基本樂理及音樂常識類知識考試題及答案
- 人教版(2024)七年級上冊英語各單元短文填空練習(xí)題匯編(含答案解析)
- DB61∕T 1165-2018 高速公路服務(wù)區(qū)服務(wù)規(guī)范
- 《環(huán)境保護(hù)與國家安全》高效備課課件
- 建筑竣工驗(yàn)收表格資料大全
- 主題二生活物品小改造(教學(xué)設(shè)計)遼師大版四年級下冊綜合實(shí)踐活動
- 中醫(yī)內(nèi)科學(xué)智慧樹知到答案2024年浙江中醫(yī)藥大學(xué)
- 2025屆陜西省西安市碑林區(qū)鐵一中學(xué)高三最后一模生物試題含解析
評論
0/150
提交評論