【JSP的腳本】
? <%! %> :翻譯成Servlet中的成員內(nèi)容. 定義變量,方法,類. -- 不建議.
? <% %> :翻譯成Servlet中service方法內(nèi)部的內(nèi)容. 定義類,變量
? <%= %> :翻譯成Servlet中service方法中out.print();
- 設(shè)置全局的錯(cuò)誤友好頁面:
* 在web.xml中設(shè)置:
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
? pageContext內(nèi)置對象 :
* 獲得其他的8個(gè)內(nèi)置對象 :編寫通用性代碼或者框架的時(shí)候.
JSP的四個(gè)域范圍:
* PageScope :當(dāng)前頁面中有效. pageContext PageContext
* RequestScope :一次請求范圍. request HttpServletRequest
* SessionScope :一次會(huì)話范圍. session HttpSession
* ApplicationScope :應(yīng)用范圍 application ServletContext
EL 表達(dá)式
? 使用EL表達(dá)式:
* 語法:${ EL表達(dá)式 }
【EL操作WEB開發(fā)的常用對象11個(gè)】
<h1>EL功能三:操作WEB開發(fā)常用的對象</h1>
<h3>接收請求的參數(shù)</h3>
<%= request.getParameter("id") %>
<%= request.getParameter("name") %>
<%= Arrays.toString(request.getParameterValues("hobby")) %>
${ param.id }
${ param.name }
${ paramValues.hobby[0] }
${ paramValues.hobby[1] }
<h3>獲取請求頭</h3>
<%= request.getHeader("User-Agent") %>
${ header["User-Agent"] }
<h3>獲取全局初始化參數(shù)</h3>
${ initParam.username }
<h3>獲取Cookie中的值</h3>
${ cookie.history.value }
<h3>獲取PageContext中的對象</h3>
IP地址:${ pageContext.request.remoteAddr }
工程路徑:${ pageContext.request.contextPath }
【EL執(zhí)行運(yùn)算】
<h1>EL的功能二:執(zhí)行運(yùn)算</h1>
<h3>EL執(zhí)行算數(shù)運(yùn)算</h3>
<%
pageContext.setAttribute("n1", "10");
pageContext.setAttribute("n2", "20");
pageContext.setAttribute("n3", "30");
pageContext.setAttribute("n4", "40");
%>
${ n1 + n2 + n3 }
<h3>EL執(zhí)行邏輯運(yùn)算</h3>
${ n1 < n2 } - ${ n1 lt n2 }
${ n1 > n2 } - ${ n1 gt n2 }
${ n1 <= n2 } - ${ n1 le n2 }
${ n1 >= n2 } - ${ n1 ge n2 }
${ n1 == n2 } - ${ n1 eq n2 }
<h3>EL執(zhí)行關(guān)系運(yùn)算</h3>
${ n1<n2 && n3 < n4 } - ${ n1<n2 and n3 < n4 }<br/>
${ n1<n2 || n3 < n4 } - ${ n1<n2 or n3 < n4 }<br/>
${ !(n1 < n2) } - ${ not(n1<n2) }
<h3>EL執(zhí)行三元運(yùn)算</h3>
${ n1 < n2 ? "正確":"錯(cuò)誤" }
<h3>empty運(yùn)算</h3>
${ user == null } - ${ empty user }
${ user != null } - ${ not empty user }

? JSTL的標(biāo)簽庫:包含了五類標(biāo)簽.
- core(核心標(biāo)簽),fmt(國際化標(biāo)簽),xml(XML標(biāo)簽),sql(SQL標(biāo)簽),fn(JSTL提供EL函數(shù)庫)
? 使用JSTL: - 引入JSTL的相關(guān)的jar包.
- 在頁面中引入標(biāo)簽庫.<%@ taglib uri=”” prefix=””%>
【JSTL的核心標(biāo)簽的用法】 - if
- forEach
【JSTL的提供EL的函數(shù)庫】
<h1>JSTL提供的EL的函數(shù)庫</h1>
${ fn:contains("Hello World","Hello") }
${ fn:length("HelloWorld") }
${ fn:toLowerCase("ABCDE") }
<c:forEach var="i" items='${ fn:split("a-b-c-d","-") }'>
${ i }
</c:forEach>