Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
部分包括: JSP, EL, JSTL, My Tag, I18N, FileUpDown
1.jsp有哪些內(nèi)置對(duì)象?作用分別是什么?
答:JSP共有以下9個(gè)內(nèi)置的對(duì)象:
request: 用戶端請(qǐng)求,此請(qǐng)求會(huì)包含來自GET/POST請(qǐng)求的參數(shù)
response: 網(wǎng)頁傳回用戶端的回應(yīng)
pageContext: 網(wǎng)頁的屬性是在這里管理
session: 與請(qǐng)求有關(guān)的會(huì)話期
application: 與當(dāng)前應(yīng)用對(duì)應(yīng)的ServletContext對(duì)象, 應(yīng)用中只有一個(gè)
out: 用來傳送回應(yīng)的輸出 {}<%=%>
config: 與jsp配置對(duì)象的對(duì)象, 一般無用
page: jsp對(duì)應(yīng)的Servlet對(duì)象
exception: 針對(duì)錯(cuò)誤網(wǎng)頁,未捕捉的異常對(duì)象
2. jsp有哪些動(dòng)作?作用分別是什么?
答:JSP共有以下6種基本動(dòng)作
jsp:include:在頁面被請(qǐng)求的時(shí)候引入一個(gè)文件。
jsp:forward:把請(qǐng)求轉(zhuǎn)到一個(gè)新的頁面。
jsp:useBean:尋找或者實(shí)例化一個(gè)JavaBean。
jsp:setProperty:設(shè)置JavaBean的屬性。
jsp:getProperty:輸出某個(gè)JavaBean的屬性。
jsp:plugin:根據(jù)瀏覽器類型為Java插件生成OBJECT或EMBED標(biāo)記
3. JSP的常用指令
答:主要有下面3種指令
contentType=”text/html; charset=utf-8″ //向?yàn)g覽器端輸出數(shù)據(jù)的編碼
pageEncoding=”utf-8″ //jsp文件被編譯成java文件時(shí)所用的編碼
session=”true” //是否自動(dòng)創(chuàng)建session
4. JSP中動(dòng)態(tài)INCLUDE與靜態(tài)INCLUDE的區(qū)別?
答:
5. JSP和Servlet有哪些相同點(diǎn)和不同點(diǎn),他們之間的聯(lián)系是什么?
答:
JSP的優(yōu)點(diǎn)是擅長(zhǎng)于網(wǎng)頁制作,生成動(dòng)態(tài)頁面比較直觀,缺點(diǎn)是不容易跟蹤與排錯(cuò)。
Servlet是純Java語言,擅長(zhǎng)于處理流程和業(yè)務(wù)邏輯,缺點(diǎn)是生成動(dòng)態(tài)網(wǎng)頁不直觀。
6. EL的功能, 為什么要用EL?
在頁面中用jsp腳本和jsp表達(dá)式來獲取數(shù)據(jù)顯示比較麻煩
7. JSTL的功能, 為什么要用JSTL?
JSTL全名為JavaServer Pages Standard Tag Library, 主要用于基本輸入輸出、流程控制、循環(huán)、XML文件剖析、數(shù)據(jù)庫查詢及國(guó)際化和文字格式標(biāo)準(zhǔn)化的應(yīng)用等
在jsp頁面做條件判斷或循環(huán)操作并輸出時(shí), 比較費(fèi)力
8. 為什么要用自定義標(biāo)簽?, MyTag如何實(shí)現(xiàn)?
1.編寫標(biāo)簽處理器類(SimpleTagSupport的實(shí)現(xiàn)類)
2.編寫標(biāo)簽庫文件(WEB-INF/xxx.tld)
3.在jsp頁面使用標(biāo)簽:
SP指令是指:用于設(shè)置JSP頁面相關(guān)屬性的一個(gè)語法命令,例如:設(shè)置頁面編碼字符集、導(dǎo)入其他包等等。JSP中提供了三個(gè)指令,分別是:page指令、include指令、taglib指令。其中page指令用于設(shè)置JSP頁面屬性,include指令用于引入其他的JSP文件,taglib指令用于引入標(biāo)簽庫。這一小節(jié)內(nèi)容介紹page指令的使用。
JSP中page指令的語法規(guī)則如下所示:
<%@ page 屬性名稱="屬性值" %>
注意:一個(gè)page指令中,可以有多個(gè)【屬性名稱=屬性值】,也可以多次使用page指令。
page指令中,提供了很多個(gè)屬性,常見的屬性有這幾個(gè):contentType、pageEncoding、errorPage、isErrorPage、import、language、session、isELIgnored,下面我們就介紹每一個(gè)屬性的作用。
這一小節(jié)介紹errorPage、isErrorPage、session、isELIgnored四個(gè)屬性的作用。
session屬性作用:設(shè)置當(dāng)前JSP頁面中是否可以使用session對(duì)象,取值:true、false。默認(rèn)是true,設(shè)置成false,那么當(dāng)前JSP頁面里面就不能使用session對(duì)象。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page session="true" %>
<html>
<head>
<title>JSP指令之Page</title>
</head>
<body>
<h3>JSP指令之Page</h3>
<%
// 使用session對(duì)象
Object username=session.getAttribute("username");
System.out.println(username);
%>
</body>
</html>
如果設(shè)置session="fasle",那么在JSP頁面中使用session,編譯會(huì)報(bào)錯(cuò),如下所示:
這是因?yàn)?,?dāng)我們?cè)O(shè)置session="false"的時(shí)候,JSP編譯之后,對(duì)應(yīng)的java源代碼中,都不會(huì)定義session變量,來看下session設(shè)置成true和false兩種情況下,對(duì)應(yīng)的源代碼如下圖所示:
isELIgnored屬性作用:這個(gè)屬性的作用是設(shè)置當(dāng)前JSP頁面是否忽略EL表達(dá)式,取值:true、false,默認(rèn)值是true。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="true" %>
<html>
<head>
<title>JSP指令之Page</title>
</head>
<body>
<h3>JSP指令之Page</h3>
使用EL表達(dá)式獲取參數(shù): ${"輸出EL表達(dá)式內(nèi)容"}
</body>
</html>
運(yùn)行結(jié)果如下所示:
當(dāng)我們?cè)O(shè)置成isELIgnored="false"的時(shí)候,再次訪問jsp頁面,此時(shí)結(jié)果如下所示:
isErrorPage屬性作用:指定當(dāng)前JSP頁面是否作為錯(cuò)誤處理界面,取值:true、false,默認(rèn)值是false。設(shè)置成true之后,那么當(dāng)其他的JSP頁面發(fā)生報(bào)錯(cuò)的時(shí)候,通過errorPage屬性,就會(huì)轉(zhuǎn)發(fā)到這個(gè)錯(cuò)誤頁面。
注意:isErrorPage屬性一般是和errorPage屬性結(jié)合使用的。
errorPage屬性作用:指定當(dāng)前JSP頁面的錯(cuò)誤頁面地址,一般是和isErrorPage屬性結(jié)合使用。errorPage設(shè)置的相對(duì)路徑,源代碼上就是轉(zhuǎn)發(fā)到對(duì)應(yīng)的錯(cuò)誤頁面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 指定當(dāng)前頁面是錯(cuò)誤頁面 --%>
<%@ page isErrorPage="true" %>
<html>
<head>
<title>JSP錯(cuò)誤顯示頁面</title>
</head>
<body>
<h3>
Sorry,你訪問的頁面報(bào)錯(cuò)啦?。?!
</h3>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 指定錯(cuò)誤頁面的路徑地址 --%>
<%@ page errorPage="error.jsp" %>
<html>
<head>
<title>errorPage屬性</title>
</head>
<body>
<%-- 模擬報(bào)錯(cuò) --%>
<%
int i=10 / 0;
%>
</body>
</html>
啟動(dòng)Tomcat容器,瀏覽器訪問http://localhost:8080/servlet/page.jsp,結(jié)果如下所示:
查看page.jsp編譯之后對(duì)應(yīng)的源代碼,可以看到有一個(gè)handlePageException()方法,這個(gè)方法就是處理JSP頁面異常的。
點(diǎn)進(jìn)去查看源代碼,如下所示:
private void doHandlePageException(Throwable t) throws IOException, ServletException {
if (this.errorPageURL !=null && !this.errorPageURL.equals("")) {
this.request.setAttribute("javax.servlet.jsp.jspException", t);
this.request.setAttribute("javax.servlet.error.status_code", 500);
this.request.setAttribute("javax.servlet.error.request_uri", ((HttpServletRequest)this.request).getRequestURI());
this.request.setAttribute("javax.servlet.error.servlet_name", this.config.getServletName());
try {
this.forward(this.errorPageURL);
} catch (IllegalStateException var3) {
this.include(this.errorPageURL);
}
Object newException=this.request.getAttribute("javax.servlet.error.exception");
if (newException !=null && newException==t) {
this.request.removeAttribute("javax.servlet.error.exception");
}
this.request.removeAttribute("javax.servlet.error.status_code");
this.request.removeAttribute("javax.servlet.error.request_uri");
this.request.removeAttribute("javax.servlet.error.servlet_name");
this.request.removeAttribute("javax.servlet.jsp.jspException");
} else if (t instanceof IOException) {
throw (IOException)t;
} else if (t instanceof ServletException) {
throw (ServletException)t;
} else if (t instanceof RuntimeException) {
throw (RuntimeException)t;
} else {
Throwable rootCause=null;
if (t instanceof JspException || t instanceof ELException || t instanceof javax.servlet.jsp.el.ELException) {
rootCause=t.getCause();
}
if (rootCause !=null) {
throw new ServletException(t.getClass().getName() + ": " + t.getMessage(), rootCause);
} else {
throw new ServletException(t);
}
}
}
從上面源代碼就可以看到,有一個(gè)this.forward()方法,這個(gè)方法就是轉(zhuǎn)發(fā)的作用。到此,page指令的常用屬性都介紹完啦。
今天就到這里,未完待續(xù)~~
sp中四種傳遞參數(shù)的方法,我覺得總結(jié)一下,挺好的,以備后用!
1、form表單
2、request.setAttribute();和request.getAttribute();
3、超鏈接:<a herf="index.jsp"?a=a&b=b&c=c>name</a>
1、form表單
form.jsp:
<%@page contentType="text/html; charset=GB2312"%> <html> <head> <title> form.jsp file </title> </head> <body style="background-color:lightblue"> <h2 style="font-family:arial;color:red;font-size:25px;text-align:center">登錄頁面</h2> <form action="result.jsp" method="get" align="center"> 姓名:<input type="text" name="name" size="20" value="" maxlength="20"><br/> 密碼:<input type="password" name="password" size="20" value="" maxlength="20"><br/> <!--在愛好前空一個(gè)空格,是為了排版好看些--> 愛好:<input type="checkbox" name="hobby" value="唱歌">唱歌 <input type="checkbox" name="hobby" value="足球">足球 <input type="checkbox" name="hobby" value="籃球">籃球<br/><br/> <input type="submit" name="submit" value="登錄"> <input type="reset" name="reset" value="重置"><br/> </form> </body> </html>
result.jsp:
1 <%@page language="java" import="java.util.*" pageEncoding="GB2312"%> 2 <html> 3 <head> 4 <title> 5 result.jsp file 6 </title> 7 </head> 8 9 <body bgcolor="ffffff"> 10 <% 11 request.setCharacterEncoding("GB2312"); 12 13 String name=request.getParameter("name"); 14 name=new String(name.getBytes("iso-8859-1"),"GB2312"); 15 16 String pwd=request.getParameter("password"); 17 String[] hobby=request.getParameterValues("hobby");//注意這里的函數(shù)是getParameterValues()接受一個(gè)數(shù)組的數(shù)據(jù) 18 19 %> 20 21 <% 22 if(!name.equals("") && !pwd.equals("")) 23 { 24 %> 25 26 您好!登錄成功!<br/> 27 姓名:<%=name%><br/> 28 密碼:<%=pwd%><br/> 29 愛好:<% 30 for(String ho: hobby) 31 { 32 ho=new String(ho.getBytes("iso-8859-1"),"GB2312"); 33 out.print(ho+" "); 34 } 35 %> 36 <% 37 } 38 else 39 { 40 %> 41 請(qǐng)輸入姓名或密碼! 42 <% 43 } 44 %> 45 </body> 46 </html>
注意:form表單的提交方式為get,在參數(shù)傳遞時(shí)會(huì)遇到中文亂碼的問題,一個(gè)簡(jiǎn)單的解決方法是,將接受到的字符串先轉(zhuǎn)換成一個(gè)byte數(shù)組,再用String構(gòu)造一個(gè)新的編碼格式的String,如:
1 String name=request.getParameter("name"); 2 name=new String(name.getBytes("iso-8859-1"),"GB2312");
如果form表單的提交方式為post,解決亂碼問題的簡(jiǎn)單辦法是,使用 request.setCharacterEncoding("GB2312");設(shè)置request的編碼方式。
為什么會(huì)出現(xiàn)中文亂碼問題呢?因?yàn)門omcat服務(wù)器默認(rèn)的系統(tǒng)編碼方式為iso- 8859-1,你傳遞參數(shù)給服務(wù)器時(shí),使用的是默認(rèn)的iso-8859-1的編碼方式,但是服務(wù)器向你返回信息時(shí),是按page指令中設(shè)置的編碼方式, 如:<%@page language="java" import="java.util.*" pageEncoding="GB2312"%>,這樣就混合了兩種編碼方式,所以會(huì)出現(xiàn)亂碼,所以解決之道就是統(tǒng)一傳遞和接收的編碼方式。
2、request.setAttribute()和request.getAttribute()
set.jsp:
<%@page contentType="text/html; charset=GB2312"%> <html> <head> <title> set.jsp file </title> </head> <body style="background-color:lightblue"> <% request.setAttribute("name","心雨"); %> <jsp:forward page="get.jsp"/> </body> </html>
get.jsp:
<%@page contentType="text/html; charset=GB2312"%> <html> <head> <title> get.jsp file </title> </head> <body style="background-color:lightblue"> <% out.println("傳遞過來的參數(shù)是:"+request.getAttribute("name")); %> </body> </html>
request.setAttribute()和request.getAttribute()是配合<jsp:forward>或是include指令來實(shí)現(xiàn)的。
3、超鏈接:<a herf="index.jsp?a=a&b=b&c=c">name</a>
href.jsp:
<%@page contentType="text/html; charset=GB2312"%> <html> <head> <title> href.jsp file </title> </head> <body style="background-color:lightblue"> <a href="getHerf.jsp?name=心雨&password=123">傳遞參數(shù)</a> </body> </html>
getHref.jsp:
<%@page contentType="text/html; charset=GB2312"%> <html> <head> <title> getHref.jsp file </title> </head> <body style="background-color:lightblue"> <% String name=request.getParameter("name"); name=new String(name.getBytes("iso-8859-1"),"gb2312"); out.print("name:"+name); %> <br/> <% out.print("password:"+request.getParameter("password")); %> </body> </html>
這種傳遞參數(shù)的方法和form表單的get方式類似,是通過地址欄傳遞的參數(shù),其亂碼解決方法也和form 的get方式一樣。
4、<jsp:param>
param.jsp:
<%@page contentType="text/html; charset=GB2312"%> <html> <head> <title> param.jsp file </title> </head> <body style="background-color:lightblue"> <%request.setCharacterEncoding("GB2312");%> <jsp:forward page="getParam.jsp"> <jsp:param name="name" value="心雨"/> <jsp:param name="password" value="123"/> </jsp:forward> </body> </html>
getParam.jsp:
<%@page contentType="text/html; charset=GB2312"%> <html> <head> <title> getParam.jsp file </title> </head> <body style="background-color:lightblue"> <% String name=request.getParameter("name"); out.print("name:"+name); %> <br/> <% out.print("password:"+request.getParameter("password")); %> </body> </html>
這里發(fā)現(xiàn)了一個(gè)奇怪的問題,還是在中文亂碼的問題上,在form表單的例子中,如果傳遞方式為post,則只需要在接收參數(shù)的頁面設(shè)置request的編 碼方式就可以了,即request.setCharacterEncoding("GB2312");,注意是在接收參數(shù)的頁面,如果將該句放到form 表單里,那么不起作用,仍然是亂碼。而在本例中,為了使傳遞的參數(shù)不出現(xiàn)亂碼,卻是將 request.setCharacterEncoding("GB2312");放在發(fā)送參數(shù)的頁面中,才會(huì)正常顯示中文,放在接收參數(shù)的頁面中,不起 作用。也許這就是<jsp:param>和form表單傳遞參數(shù)不同的地方。為什么會(huì)有這個(gè)不同呢?可能是因?yàn)閒orm表單中的參數(shù)是由客戶 端傳送到服務(wù)端上的,需要經(jīng)過一個(gè)request的打包過程,但是<jsp:param>傳遞的參數(shù)本身就是在服務(wù)器端的,不需要經(jīng)歷由客戶 端到服務(wù)端這么一個(gè)過程,但是服務(wù)器里的參數(shù)傳遞是這么回事呢?這個(gè)問題,我不知道了!真是知識(shí)是一個(gè)擴(kuò)大的圓圈,你知道的越多,那么不知道的就越多!努 力吧!
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。