部分包括: JSP, EL, JSTL, My Tag, I18N, FileUpDown
1.jsp有哪些內置對象?作用分別是什么?
答:JSP共有以下9個內置的對象:
request: 用戶端請求,此請求會包含來自GET/POST請求的參數
response: 網頁傳回用戶端的回應
pageContext: 網頁的屬性是在這里管理
session: 與請求有關的會話期
application: 與當前應用對應的ServletContext對象, 應用中只有一個
out: 用來傳送回應的輸出 {}<%=%>
config: 與jsp配置對象的對象, 一般無用
page: jsp對應的Servlet對象
exception: 針對錯誤網頁,未捕捉的異常對象
2. jsp有哪些動作?作用分別是什么?
答:JSP共有以下6種基本動作
jsp:include:在頁面被請求的時候引入一個文件。
jsp:forward:把請求轉到一個新的頁面。
jsp:useBean:尋找或者實例化一個JavaBean。
jsp:setProperty:設置JavaBean的屬性。
jsp:getProperty:輸出某個JavaBean的屬性。
jsp:plugin:根據瀏覽器類型為Java插件生成OBJECT或EMBED標記
3. JSP的常用指令
答:主要有下面3種指令
contentType=”text/html; charset=utf-8″ //向瀏覽器端輸出數據的編碼
pageEncoding=”utf-8″ //jsp文件被編譯成java文件時所用的編碼
session=”true” //是否自動創建session
4. JSP中動態INCLUDE與靜態INCLUDE的區別?
答:
5. JSP和Servlet有哪些相同點和不同點,他們之間的聯系是什么?
答:
JSP的優點是擅長于網頁制作,生成動態頁面比較直觀,缺點是不容易跟蹤與排錯。
Servlet是純Java語言,擅長于處理流程和業務邏輯,缺點是生成動態網頁不直觀。
6. EL的功能, 為什么要用EL?
在頁面中用jsp腳本和jsp表達式來獲取數據顯示比較麻煩
7. JSTL的功能, 為什么要用JSTL?
JSTL全名為JavaServer Pages Standard Tag Library, 主要用于基本輸入輸出、流程控制、循環、XML文件剖析、數據庫查詢及國際化和文字格式標準化的應用等
在jsp頁面做條件判斷或循環操作并輸出時, 比較費力
8. 為什么要用自定義標簽?, MyTag如何實現?
1.編寫標簽處理器類(SimpleTagSupport的實現類)
2.編寫標簽庫文件(WEB-INF/xxx.tld)
3.在jsp頁面使用標簽:
SP指令是指:用于設置JSP頁面相關屬性的一個語法命令,例如:設置頁面編碼字符集、導入其他包等等。JSP中提供了三個指令,分別是:page指令、include指令、taglib指令。其中page指令用于設置JSP頁面屬性,include指令用于引入其他的JSP文件,taglib指令用于引入標簽庫。這一小節內容介紹page指令的使用。
JSP中page指令的語法規則如下所示:
<%@ page 屬性名稱="屬性值" %>
注意:一個page指令中,可以有多個【屬性名稱=屬性值】,也可以多次使用page指令。
page指令中,提供了很多個屬性,常見的屬性有這幾個:contentType、pageEncoding、errorPage、isErrorPage、import、language、session、isELIgnored,下面我們就介紹每一個屬性的作用。
這一小節介紹errorPage、isErrorPage、session、isELIgnored四個屬性的作用。
session屬性作用:設置當前JSP頁面中是否可以使用session對象,取值:true、false。默認是true,設置成false,那么當前JSP頁面里面就不能使用session對象。
<%@ 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對象
Object username=session.getAttribute("username");
System.out.println(username);
%>
</body>
</html>
如果設置session="fasle",那么在JSP頁面中使用session,編譯會報錯,如下所示:
這是因為,當我們設置session="false"的時候,JSP編譯之后,對應的java源代碼中,都不會定義session變量,來看下session設置成true和false兩種情況下,對應的源代碼如下圖所示:
isELIgnored屬性作用:這個屬性的作用是設置當前JSP頁面是否忽略EL表達式,取值:true、false,默認值是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表達式獲取參數: ${"輸出EL表達式內容"}
</body>
</html>
運行結果如下所示:
當我們設置成isELIgnored="false"的時候,再次訪問jsp頁面,此時結果如下所示:
isErrorPage屬性作用:指定當前JSP頁面是否作為錯誤處理界面,取值:true、false,默認值是false。設置成true之后,那么當其他的JSP頁面發生報錯的時候,通過errorPage屬性,就會轉發到這個錯誤頁面。
注意:isErrorPage屬性一般是和errorPage屬性結合使用的。
errorPage屬性作用:指定當前JSP頁面的錯誤頁面地址,一般是和isErrorPage屬性結合使用。errorPage設置的相對路徑,源代碼上就是轉發到對應的錯誤頁面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 指定當前頁面是錯誤頁面 --%>
<%@ page isErrorPage="true" %>
<html>
<head>
<title>JSP錯誤顯示頁面</title>
</head>
<body>
<h3>
Sorry,你訪問的頁面報錯啦!!!
</h3>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 指定錯誤頁面的路徑地址 --%>
<%@ page errorPage="error.jsp" %>
<html>
<head>
<title>errorPage屬性</title>
</head>
<body>
<%-- 模擬報錯 --%>
<%
int i=10 / 0;
%>
</body>
</html>
啟動Tomcat容器,瀏覽器訪問http://localhost:8080/servlet/page.jsp,結果如下所示:
查看page.jsp編譯之后對應的源代碼,可以看到有一個handlePageException()方法,這個方法就是處理JSP頁面異常的。
點進去查看源代碼,如下所示:
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);
}
}
}
從上面源代碼就可以看到,有一個this.forward()方法,這個方法就是轉發的作用。到此,page指令的常用屬性都介紹完啦。
今天就到這里,未完待續~~
sp中四種傳遞參數的方法,我覺得總結一下,挺好的,以備后用!
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/> <!--在愛好前空一個空格,是為了排版好看些--> 愛好:<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");//注意這里的函數是getParameterValues()接受一個數組的數據 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 請輸入姓名或密碼! 42 <% 43 } 44 %> 45 </body> 46 </html>
注意:form表單的提交方式為get,在參數傳遞時會遇到中文亂碼的問題,一個簡單的解決方法是,將接受到的字符串先轉換成一個byte數組,再用String構造一個新的編碼格式的String,如:
1 String name=request.getParameter("name"); 2 name=new String(name.getBytes("iso-8859-1"),"GB2312");
如果form表單的提交方式為post,解決亂碼問題的簡單辦法是,使用 request.setCharacterEncoding("GB2312");設置request的編碼方式。
為什么會出現中文亂碼問題呢?因為Tomcat服務器默認的系統編碼方式為iso- 8859-1,你傳遞參數給服務器時,使用的是默認的iso-8859-1的編碼方式,但是服務器向你返回信息時,是按page指令中設置的編碼方式, 如:<%@page language="java" import="java.util.*" pageEncoding="GB2312"%>,這樣就混合了兩種編碼方式,所以會出現亂碼,所以解決之道就是統一傳遞和接收的編碼方式。
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("傳遞過來的參數是:"+request.getAttribute("name")); %> </body> </html>
request.setAttribute()和request.getAttribute()是配合<jsp:forward>或是include指令來實現的。
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">傳遞參數</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>
這種傳遞參數的方法和form表單的get方式類似,是通過地址欄傳遞的參數,其亂碼解決方法也和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>
這里發現了一個奇怪的問題,還是在中文亂碼的問題上,在form表單的例子中,如果傳遞方式為post,則只需要在接收參數的頁面設置request的編 碼方式就可以了,即request.setCharacterEncoding("GB2312");,注意是在接收參數的頁面,如果將該句放到form 表單里,那么不起作用,仍然是亂碼。而在本例中,為了使傳遞的參數不出現亂碼,卻是將 request.setCharacterEncoding("GB2312");放在發送參數的頁面中,才會正常顯示中文,放在接收參數的頁面中,不起 作用。也許這就是<jsp:param>和form表單傳遞參數不同的地方。為什么會有這個不同呢?可能是因為form表單中的參數是由客戶 端傳送到服務端上的,需要經過一個request的打包過程,但是<jsp:param>傳遞的參數本身就是在服務器端的,不需要經歷由客戶 端到服務端這么一個過程,但是服務器里的參數傳遞是這么回事呢?這個問題,我不知道了!真是知識是一個擴大的圓圈,你知道的越多,那么不知道的就越多!努 力吧!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。