【JSP的概述】
? 什么是JSP:
* java Server Pages(Java服務器端的頁面)
? 為什么要學習JSP:
* SUN公司推出的Servlet自身有缺陷,沒有辦法與asp,php進行競爭.推出了動態網頁開發技術JSP.
? 使用JSP:
* JSP = HTML + Java代碼 + JSP自身的東西.
? 執行JSP的過程:
* JSP翻譯成Servlet,編譯這個Servlet的類,生成class文件.得到執行.
【JSP的腳本】
? <%! %> :翻譯成Servlet中的成員內容. 定義變量,方法,類. -- 不建議.
? <% %> :翻譯成Servlet中service方法內部的內容. 定義類,變量
? <%= %> :翻譯成Servlet中service方法中out.PRint();
【JSP的注釋】-了解
? HTML的注釋 :<!-- 注釋 -->
? Java代碼的注釋 :// 單行注釋 /*多行注釋*/ /** 文檔注釋 */
? JSP的注釋 :<%-- JSP的注釋 --%>
【JSP的指令】
? 指令的語法:
<%@ 指令名稱屬性名稱=”屬性值” 屬性名稱=”屬性值” ...%>
? JSP中有三個指令:page指令, include指令, taglib指令.
? JSP中page指令:<%@ page %> -- 設置JSP的.
* language :JSP腳本中使用的語言.現在只能寫java.
* contentType :設置瀏覽器打開這個JSP的時候采用的默認的字符集的編碼.
* pageEncoding :設置文件保存到本地硬盤,以及生成Servlet后,Servlet保存到硬盤上的編碼.
* import :在JSP中引入類對象.但是import可以出現多次.
<%@pageimport="java.util.ArrayList"%>
<%@pageimport="java.util.List"%>
* extends :設置JSP翻譯成Servlet后繼承的類,默認值:org.apache.jasper.runtime.HttpJspBase,這個值要想修改,這個類必須是HttpServlet的子類
* autoFlush :設置JSP的緩存自動刷出.true:自動刷出.
* buffer :設置JSP的緩沖區的大小,默認8kb.
* session :設置在JSP中是否可以直接使用session對象.默認值是true.
* isELIgnored :設置在JSP中是否忽略EL表達式.默認值是false不忽略.
* errorPage :設置錯誤友好頁面的提示.
* isErrorPage :通過這個設置顯示JSP的錯誤信息.
* 設置全局的錯誤友好頁面:
* 在web.xml中設置:
<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>
? JSP中的include指令:指示JSP包含其他的頁面.
<%@ include file="logo.jsp" %>
<%@ include file="menu.jsp" %>
<h1>BODY部分</h1>
<%@ include file="footer.jsp" %>
? JSP中的taglib指令:指示JSP引入標簽庫.
<%@ taglib uri="標簽的URI的路徑" prefix="標簽的別名" %>
【JSP的內置對象(*****)】
? JSP的內置對象:在JSP中可以直接使用的對象.
? JSP中有9大內置對象:
* request HttpServletRequest getParameter(),setAttribute(Stringname,Object value);
* response HttpServletResponse setHeader(String name,Stringvalue);getOutputStream();getWriter();
* session HttpSession setAttribute();getAttribute();
* application ServletContext setAttribute();getAttribute();
* page Object toString();wait();
* pageContext PageContext setAttribute();getAttribute();
* config ServletConfig getServletName();getServletContext();
* out JspWriter write(),print();
* exception Throwable getMessage(),getCause(); 設置isErrorPage=”true”
? page內置對象 :真實對象是Object,就是JSP翻譯成Servlet后的類的引用.
? out內置對象 :out和response.getWriter是不是同一個對象?區別是什么?
* 不是out真實對象JspWriter ,response獲得Writer是PrintWriter.
? pageContext內置對象 :
* 獲得其他的8個內置對象 :編寫通用性代碼或者框架的時候.
* 向JSP的四個域中存取數據 :
JSP的四個域范圍:
*PageScope :當前頁面中有效. pageContext PageContext
*RequestScope :一次請求范圍. request HttpServletRequest
*SessionScope :一次會話范圍. session HttpSession
*ApplicationScope :應用范圍 application ServletContext
【JSP的動作標簽】列出6個.
? 標簽的作用:簡化代碼.
? <jsp:forward /> :用于頁面的轉發.
* <jsp:forwardpage="/demo1-jsp/demo3-object/demo3.jsp"></jsp:forward>
? <jsp:include /> :用于頁面的包含.(動態包含)
*****靜態包含和動態包含的區別?(<%@ include%>和<jsp:include>)
? <jsp:param /> :用于帶有路徑的標簽下,傳遞參數.
? <jsp:useBean /> :用于在JSP中使用JavaBean.
? <jsp:setProperty /> :用于在JSP中向JavaBean設置屬性的.
? <jsp:getProperty /> :用于在JSP中獲得JavaBean的屬性.
【EL的概述】
? 什么是EL:表達式語言
? EL的作用:
* 簡化JSP的代碼使用EL表達式:
* 語法:${ EL表達式 }
? EL的功能:
* 獲取數據:(JSP的四個域)
* 執行運算:
* 操作WEB開發的常用的對象:
* 調用Java中方法:--很少用.
【EL獲取數據】
<h3>存取是普通的單值數據</h3>
<%
//pageContext.setAttribute("name","pValue");
//request.setAttribute("name","rValue");
//session.setAttribute("name","sValue");
application.setAttribute("name","aValue");
%>
<%=pageContext.getAttribute("name")%> <!-- 如果沒找到 返回null -->
<%=request.getAttribute("name") %>
<%=session.getAttribute("name") %>
<%=application.getAttribute("name")%>
<hr/>
${ pageScope.name } <!-- 返回的是"" -->
${ requestScope.name }
${ sessionScope.name }
${ applicationScope.name }
<hr/>
${ name } <!-- 類似findAttribute("name") 先從page域中查找,沒找到去request域中查詢,沒有找到去session域中找,沒有找到就去application域中找-->
<h3>獲取數組的數據</h3>
<%
String[]arrs = {"aaa ","bbb","ccc","ddd"};
pageContext.setAttribute("arrs",arrs);
%>
${ arrs[0] }
${ arrs[1] }
${ arrs[2] }
${ arrs[3] }
<h3>獲取List集合的數據</h3>
<%
List<String>list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
pageContext.setAttribute("list",list);
%>
${ list[0] }
${ list[1] }
${ list[2] }
<h3>獲取Map集合的數據</h3>
<%
Map<String,String>map = new HashMap<String,String>();
map.put("aaa","張三");
map.put("bbb","李四");
map.put("ccc","王五");
map.put("ddd","趙六");
pageContext.setAttribute("map",map);
%>
${ map.aaa }
${ map.bbb }
${ map.ccc }
${ map.ddd }
<h3>獲取對象的數據</h3>
<%
User user= new User(1,"aaa","123");
pageContext.setAttribute("user",user);
%>
${ user.id }
${ user.username }
${ user.passWord }
<h3>獲取對象的集合的數據</h3>
<%
Useruser1 = new User(1,"aaa","123");
Useruser2 = new User(2,"bbb","123");
Useruser3 = new User(3,"ccc","123");
List<User>userList = new ArrayList<User>();
userList.add(user1);
userList.add(user2);
userList.add(user3);
pageContext.setAttribute("userList",userList);
%>
${ userList[0].id } - ${ userList[0].username } -${ userList[0].password }<br/>
${ userList[1].id } - ${ userList[1].username } -${ userList[1].password }<br/>
${ userList[2].id } - ${ userList[2].username } -${ userList[2].password }<br/>
***** .和[]的區別.
* []用于有下標的數據(數組,list集合) .用于有屬性的數據(map,對象)
* 如果屬性名中包含有特殊的字符.必須使用[]
【EL執行運算】
<h1>EL的功能二:執行運算</h1>
<h3>EL執行算數運算</h3>
<%
pageContext.setAttribute("n1","10");
pageContext.setAttribute("n2","20");
pageContext.setAttribute("n3","30");
pageContext.setAttribute("n4","40");
%>
${ n1 + n2 + n3 }
<h3>EL執行邏輯運算</h3>
${ n1 < n2 } - ${ n1 lt n2 } <!-- less than--><br/>
${ n1 > n2 } - ${ n1 gt n2 } <!-- great than--><br/>
${ n1 <= n2 } - ${ n1 le n2 } <!-- lessequal --><br/>
${ n1 >= n2 } - ${ n1 ge n2 } <!-- greatequal --><br/>
${ n1 == n2 } - ${ n1 eq n2 } <!-- equal--><br/>
<h3>EL執行關系運算</h3>
${ n1<n2 && n3 < n4 } - ${ n1<n2and n3 < n4 }<br/>
${ n1<n2 || n3 < n4 } - ${ n1<n2 or n3< n4 }<br/>
${ !(n1 < n2) } - ${ not(n1<n2) }
<h3>EL執行三元運算</h3>
${ n1 < n2 ? "正確":"錯誤" }
<h3>empty運算</h3>
${ user == null } - ${ empty user }
${ user != null } - ${ not empty user }
【EL操作WEB開發的常用對象11個】
<h1>EL功能三:操作WEB開發常用的對象</h1>
<!--
pageScope,requestScope,sessionScope,applicationScope- 獲取JSP中域中的數據
param,paramValues - 接收參數.
header,headerValues- 獲取請求頭信息
initParam - 獲取全局初始化參數
cookie -WEB開發中cookie
pageContext - WEB開發中的pageContext.
-->
<h3>接收請求的參數</h3>
<%= request.getParameter("id") %>
<%= request.getParameter("name")%>
<%=Arrays.toString(request.getParameterValues("hobby")) %>
<hr/>
${ param.id }
${ param.name }
${ paramValues.hobby[0] }
${ paramValues.hobby[1] }
<h3>獲取請求頭</h3>
<%= request.getHeader("User-Agent")%>
<hr/>
${ header["User-Agent"] }
<h3>獲取全局初始化參數</h3>
${ initParam.username }
<h3>獲取Cookie中的值</h3>
${ cookie.history.value }
<h3>獲取PageContext中的對象</h3>
ip地址:${pageContext.request.remoteAddr }
工程路徑:${pageContext.request.contextPath }
【JSTL的概述】
? 什么是JSTL:JSP標準標簽庫
? 為什么學習JSTL:
* JSTL和EL結合 替換頁面中<%%>
? JSTL版本:
* JSTL1.0 :不支持EL表達式.
* JSTL1.1 和 1.2 :支持EL表達式.
? JSTL的標簽庫:包含了五類標簽.
* core(核心標簽),fmt(國際化標簽),xml(XML標簽),sql(SQL標簽),fn(JSTL提供EL函數庫)
? 使用JSTL:
* 引入JSTL的相關的jar包.
* 在頁面中引入標簽庫.<%@taglib uri=”” prefix=””%>
【JSTL的核心標簽的用法】
* if
* forEach
【JSTL的提供EL的函數庫】
<h1>JSTL提供的EL的函數庫</h1>
${ fn:contains("HelloWorld","Hello") }
${ fn:length("HelloWorld") }
${ fn:toLowerCase("ABCDE") }
<c:forEach var="i" items='${fn:split("a-b-c-d","-") }'>
${ i }
</c:forEach>
新聞熱點
疑難解答