第2章jsp腳本元素指令和動(dòng)作_第1頁
第2章jsp腳本元素指令和動(dòng)作_第2頁
第2章jsp腳本元素指令和動(dòng)作_第3頁
第2章jsp腳本元素指令和動(dòng)作_第4頁
第2章jsp腳本元素指令和動(dòng)作_第5頁
已閱讀5頁,還剩32頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第二章

JSP腳本元素、指令和動(dòng)作回顧WEB應(yīng)用程序動(dòng)態(tài)頁面什么是JSPTOMCAT的部署本章目標(biāo)腳本元素指令動(dòng)作注釋本章結(jié)構(gòu)指令Scripletpage指令taglib指令JSP腳本元素、指令和動(dòng)作表達(dá)式動(dòng)作JSP腳本元素include動(dòng)作param動(dòng)作forward動(dòng)作聲明include指令注釋JSP腳本元素就是在JSP頁面中嵌入的腳本語言代碼向容器聲明要將什么樣的Java語句添加到servlet類中如果可能的話,盡量減少在JSP頁面中使用JSP腳本元素三種類型:表達(dá)式:<%=Expressions%>Scriplet:<%Code%>聲明:<%!Declarations%>表達(dá)式在執(zhí)行階段表達(dá)式求值后轉(zhuǎn)換成一個(gè)字符串字符串被直接插入到servlet的輸出流結(jié)果輸出方法為out.print(表達(dá)式)表達(dá)式中可以輸出變量或隱式對(duì)象格式<%=表達(dá)式%>表達(dá)式中不允許有分號(hào)expSample.jsp<html><head><title>ExpressionSample</title></head><body><h2>Currenttime:<%=newjava.util.Date()%><br/>Randomnumber:<%=Math.random()%><br/>Simplestring:<%=“Hello,world”%><br/>Simplestatement:1+1=<%=1+1%><br/>Visitimplicitobject:remotehostis

<%=request.getRemoteHost()%></h2></body></html>expSample_jsp.java代碼片段

publicvoid_jspService(HttpServletRequestrequest,HttpServletResponseresponse)throwsjava.io.IOException,ServletException{…out.write("Currenttime:");

out.print(newjava.util.Date());out.write("<br/>\r\n");out.write("Randomnumber:");

out.print(Math.random());out.write("<br/>\r\n");out.write("Simplestring:");

out.print("Hello,world");out.write("<br/>\r\n");out.write("Simplestatement:1+1=");

out.print(1+1);out.write("<br/>\r\n");out.write("Visitimplicitobject:remotehostis");

out.print(request.getRemoteHost());out.write("\r\n");…}Scriplet可使用任意Java代碼插入到servlet的_jspService()方法可以完成表達(dá)式不能單獨(dú)完成的功能執(zhí)行的代碼包含循環(huán),條件分支執(zhí)行業(yè)務(wù)邏輯或數(shù)據(jù)訪問邏輯:更新數(shù)據(jù)庫(kù),記錄服務(wù)器日志,等等??稍O(shè)置響應(yīng)頭和狀態(tài)代碼可使用預(yù)定義變量,包括隱式對(duì)象格式<%Java代碼%>通常以分號(hào)結(jié)束scrSample.jsp<%response.setContentType(“text/html;charset=\“gb2312\””);%><html><head><title>ScripletSample</title></head><body><h2>DisplayweekdaysinChinese:<br/>

<%java.text.DateFormatSymbolszhDfs=newjava.text.DateFormatSymbols(java.util.Locale.CHINA);String[]weekDays=zhDfs.getWeekdays();for(inti=1;i<weekDays.length;i++)out.println(weekDays[i]+"<br/>");%></h2></body></html>scrSample_jsp.java代碼片段

publicvoid_jspService(HttpServletRequestrequest, HttpServletResponseresponse)throwsjava.io.IOException,ServletException{...

response.setContentType("text/html;charset=gb2312");

…java.text.DateFormatSymbolszhDfs=newjava.text.DateFormatSymbols(java.util.Locale.CHINA);String[]weekDays=zhDfs.getWeekdays();for(inti=1;i<weekDays.length;i++)out.println(weekDays[i]+"<br/>");

...}聲明

聲明可在servlet類的主體里定義變量和方法

在_jspService()方法外不可聲明隱式對(duì)象聲明的變量和方法通常使用在表達(dá)式或Scriplet中聲明可重寫jspInit()和jspDestroy()方法來初始化和結(jié)束JSP頁面格式

<%!方法或變量聲明代碼%>decSample.jsp<%!Stringprompt=“DisplayweekdaysinChinese:”;String[]getChineseWeekdays(){

java.text.DateFormatSymbolszhDfs=newjava.text.DateFormatSymbols(java.util.Locale.CHINA);returnzhDfs.getWeekdays();}%><%response.setContentType(“text/html;charset=gb2312”);%><html><head><title>DeclarationSample</title></head><body><h2><%=prompt%><br/>

<%String[]weekDays=getChineseWeekdays();for(inti=1;i<weekDays.length;i++)out.println(weekDays[i]+"<br/>");%></h2></body></html>decSample_jsp.java代碼片段publicfinalclassdecSample_jspextendsorg.apache.jasper.runtime.HttpJspBaseimplementsorg.apache.jasper.runtime.JspSourceDependent{

Stringprompt="DisplayweekdaysinChinese:";

String[]getChineseWeekdays(){java.text.DateFormatSymbolszhDfs=newjava.text.DateFormatSymbols(java.util.Locale.CHINA);returnzhDfs.getWeekdays();}publicvoid_jspService(HttpServletRequestrequest, HttpServletResponseresponse)throwsjava.io.IOException,ServletException{...}}指令

指令是發(fā)送給JSP編譯器的信息告訴JSP編譯器如何處理JSP頁面它不直接生成輸出語法<%@directive{attr=“value”}*%>三種類型的指令page:導(dǎo)入頁面所依賴的屬性和把這些傳遞給JSP編譯器<%@pageimport=“java.util.*”%>include:JSP頁面翻譯時(shí)用來包含文本或代碼<%@includefile=“banner.html”%>taglib:描述了JSP編譯器所使用的標(biāo)簽庫(kù)<%@tagliburi=“”prefix=“c”%>page指令定義若干頁面所依賴的屬性并把這些傳遞給JSP編譯器一個(gè)編譯單元允許存在多個(gè)page指令實(shí)例頁面編碼方式和內(nèi)容類型屬性應(yīng)該在頁面的開始部分,其他屬性的位置獨(dú)立語法<%@page{attr=“value”}*%>page指令屬性(重要)設(shè)置文件類型和字符集<%@pagecontentType=“text/html;charset=GBK”%>JSP頁面的字符編碼方式<%@pagepageEncoding=“GBK”%>引用的類<%@pageimport=“java.util.*”%>是否參與一個(gè)(HTTP)會(huì)話<%@pagesession=“true|false”%><%--默認(rèn)為”true”-%>page指令屬性處理錯(cuò)誤頁面的URL<%@pageerrorPage=“error.jsp”%>是否成為另一個(gè)JSP頁面的errorPage的URL目標(biāo)<%@pageisErrorPage=“true|false”%><%--默認(rèn)為”false”--%>JSP頁面使用哪種腳本語言<%@pagelanguage=“java”%><%--只有java語言被允許--%>在JSP頁面繼承servlet超類<%@pageextends=“className”%><%--不推薦使用--%>page指令屬性頁面初始輸出的JspWriter處理內(nèi)容的輸出緩沖模型<%@pagebuffer=“none|xxkb”%><%--默認(rèn)為8kb--%>緩存內(nèi)容是否被自動(dòng)刷新<%@pageautoFlush=“true|false”%><%--默認(rèn)為”true”--%>返回Servlet.getServletInfo()方法內(nèi)容<%@pageinfo=“YourfirstJSPpage”%>是否忽略EL表達(dá)式<%@pageisELIgnored=“true|false”%><%--默認(rèn)值依賴于web.xml版本--%>pageDireSample.jsp<%@pagesession="false"buffer="4kb"autoFlush="false"contentType="text/html;charset=gb2312"import="java.util.*,.*"errorPage="error.jsp“%>

pageDireSample_jsp.java代碼片段importjava.util.*;import.*;publicfinalclasspageDireExample_jspextendsorg.apache.jasper.runtime.HttpJspBaseimplementsorg.apache.jasper.runtime.JspSourceDependent{publicvoid_jspService(HttpServletRequestrequest,HttpServletResponseresponse)throwsjava.io.IOException,ServletException{…._jspxFactory=JspFactory.getDefaultFactory();response.setContentType("text/html;charset=gb2312");pageContext=_jspxFactory.getPageContext(this,request,response, "error.jsp",false,4096,false);…}比較這兩個(gè)示例importjava.util.*;import.*;publicfinalclasspageDireExample_jspextendsorg.apache.jasper.runtime.HttpJspBaseimplementsorg.apache.jasper.runtime.JspSourceDependent{publicvoid_jspService(HttpServletRequestrequest,HttpServletResponseresponse)throwsjava.io.IOException,ServletException{…._jspxFactory=JspFactory.getDefaultFactory();response.setContentType("text/html;charset=gb2312");pageContext=_jspxFactory.getPageContext(this,request,response, "error.jsp",false,4096,false);…}<%@pagesession="false"buffer="4kb"autoFlush="false"contentType="text/html;charset=gb2312"import="java.util.*,.*"errorPage="error.jsp"%>include指令JSP頁面翻譯成servlet時(shí)處理該指令的作用是插入在其他文件中所含的文本

無論是靜態(tài)內(nèi)容或JSP頁面都包括當(dāng)前JSP頁面中用于包括橫幅內(nèi)容,版權(quán)信息,或你可能要在其他頁面中重用任何內(nèi)容塊語法和范例<%@includefile=“relativeURLspec”%><jsp:directive.includefile=“relativeURLspec”/><%@includefile=“banner.html”><%--參照helloWorld.jsp--%>include動(dòng)作

執(zhí)行JSP時(shí)處理讓您在JSP文件中包含靜態(tài)或動(dòng)態(tài)資源靜態(tài)資源:其內(nèi)容被插入到調(diào)用JSP文件動(dòng)態(tài)資源:頁面的請(qǐng)求被發(fā)送到所包含的資源,包含的頁面執(zhí)行和調(diào)用JSP頁面響應(yīng)結(jié)果語法<jsp:includepage=“urlSpec”flush=“true|false”/><jsp:includepage=“urlSpec”flush=“true|false”>{<jsp:param…/>}*</jsp:include><%--flush的默認(rèn)值是“false”--%>inclActSample.jsp<%@pagecontentType="text/html;charset=gb2312"%><html><head><title>IncludeActionSample</title></head><body>

<jsp:includepage="banner.html"/><jsp:includepage="date.jsp"><jsp:paramname="user"value="George"/></jsp:include></body></html>date.jsp<%@pageimport="java.util.*"%><h2><center>Hello,<%=request.getParameter("user")%>,<%Calendarcal=Calendar.getInstance();if(cal.get(Calendar.AM_PM)==Calendar.AM)out.println("goodmorning!");elseout.println("goodafternoon!");%></center></h2>

inclActSample_jsp.java代碼片段importorg.apache.jasper.runtime.JspRuntimeLibrary;...publicvoid_jspService(HttpServletRequestrequest,HttpServletResponseresponse)throwsjava.io.IOException,ServletException{...out.write("<html>\r\n");out.write("<head><title>YourfirstJSP</title></head>\r\n");out.write("<body>\r\n");out.write("");

JspRuntimeLibrary.include(request,response,"banner.html",out,false);out.write("\r\n");out.write("");

JspRuntimeLibrary.include(request,response,"date.jsp"+(("date.jsp").indexOf('?')>0?'&':'?')+JspRuntimeLibrary.URLEncode("user",request.getCharacterEncoding())+"="+JspRuntimeLibrary.URLEncode("George",request.getCharacterEncoding()),out,false);out.write("\r\n");out.write("</body>\r\n");out.write("</html>\r\n");...}forward動(dòng)作控制權(quán)從一個(gè)JSP頁面轉(zhuǎn)移到另一個(gè)Web組件如果頁面有輸出緩沖,在轉(zhuǎn)發(fā)之前清除緩沖區(qū)語法<jsp:forwardpage=“urlSpec”/><jsp:forwardpage=“urlSpec”>{<jsp:param…/>}*</jsp:forward>fwdSample.jsp<jsp:forwardpage=“helloWorld.jsp”/><jsp:forward><jsp:attributename=“page”>helloWorld.jsp</jsp:attribute></jsp:forward>fwdSample_jsp.java代碼片段publicvoid_jspService(HttpServletRequestrequest, HttpServletResponseresponse)throwsjava.io.IOException,ServletException{...out.write('\r');

溫馨提示

  • 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. 人人文庫(kù)網(wǎng)僅提供信息存儲(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論