EL(Express Lanuage)表達式能夠嵌入在jsp頁面內部,減小jsp腳本的編寫,EL出現的目的是要替代jsp頁面中腳本(java代碼)的編寫。html
jsp腳本:<%=request.getAttribute(name)%>java
①EL表達式替代上面的腳本:${requestScope.name}apache
②EL最主要的做用是得到四大域中的數據,格式${EL表達式}數組
③EL得到pageContext域中的值:${pageScope.key};cookie
④EL得到request域中的值:${requestScope.key};session
⑤EL得到session域中的值:${sessionScope.key};app
⑥EL得到application域中的值:${applicationScope.key};jsp
EL從四個域中得到某個值${key};網站
---一樣是依次從pageContext域,request域,session域,application域中獲取屬性,在某個域中獲取後將不在向後尋找this
1)得到普通字符串
2)得到User對象的值
3)得到List<User>的值
<!-- 模擬域中的數據 --> <% pageContext.setAttribute("name", "pageContxt"); //存儲字符串 request.setAttribute("name", "request"); //存儲一個對象 Users user=new Users(); user.setId(1); user.setUsername("lisi"); user.setPwd("123"); session.setAttribute("user", user); //存儲一個集合 List<Users> list=new ArrayList<Users>(); Users user1=new Users(); user1.setId(1); user1.setUsername("wangwu"); user1.setPwd("123"); list.add(user1); Users user2=new Users(); user2.setId(1); user2.setUsername("zhaoliu"); user2.setPwd("123"); list.add(user2); application.setAttribute("List", list); %> <hr> <!-- 使用腳本取出域中的值 --> <%=request.getAttribute("name") %> <%Users u=(Users)session.getAttribute("user"); out.write(u.getUsername()); %> <hr> <!-- 使用EL表達式取出域中的值 --> ${requestScope.name} ${sessionScope.user.username} ${applicationScope.List[0].pwd} <hr> <!-- 使用el表達式 全域查找(會從最小的找,底層就是findAttribute()) --> ${name } ${user.username} ${List[0].pwd}
①pageScope,requestScope,sessionScope,applicationScope
---- 獲取JSP中域中的數據
②param,paramValues - 接收參數.
至關於request.getParameter() request.getParameterValues()
③header,headerValues - 獲取請求頭信息
至關於request.getHeader(name)
④initParam - 獲取全局初始化參數
至關於this.getServletContext().getInitParameter(name)
⑤cookie - WEB開發中cookie
至關於request.getCookies()---cookie.getName()---cookie.getValue()
Form.html <form action="/WEB02/form.jsp" method="get"> <input type="text" name="username"> <input type="text" name="password"> <input type="submit" value="提交"> </form>
Form.jsp <!-- 得到表單的參數 --> <% request.getParameter("username"); //..... %> <!-- 使用EL得到參數 --> ${param.username } ${header["User-Agent"] } ${cookie.abc.value } <!-- 經過el表達式得到request對象 --> <%--${requestScope } --%> ${pageContext.request }
Cookie.jsp <% Cookie cookie=new Cookie("abc","zhangsan"); response.addCookie(cookie); %>
Index.jsp <form action="${pageContext.request.contextPath }/form.jsp" method="get"> <input type="text" name="username"> <input type="text" name="password"> <input type="submit" value="提交"> </form>
pageContext - WEB開發中的pageContext.
pageContext得到其餘八大對象
${pageContext.request.contextPath}
例如:
${1+1}
${empty user}
${user==null?true:false}
JSTL(JSP Standard Tag Library),JSP標準標籤庫,能夠嵌入在jsp頁面中使用標籤的形式完成業務邏輯等功能。
jstl出現的目的同el同樣也是要代替jsp頁面中的腳本代碼。JSTL標準標準標籤庫有5個子庫,但隨着發展,目前常使用的是他的核心庫
JSTL下載:
從Apache的網站下載JSTL的JAR包。進入「http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/」網址下載 JSTL的安裝包。
jakarta-taglibs-standard-1.1.2.zip,而後將下載好的JSTL安裝包 進行解壓,此時,在lib目錄下能夠看到兩個JAR文件,分別爲jstl.jar和standard.jar。
其中,jstl.jar文件包含JSTL規範中定義的接口和相關類,standard.jar文件包含用於 實現JSTL的.class文件以及JSTL中5個標籤庫描述符文件(TLD)
將兩個jar包導入咱們工程的lib中
使用jsp的taglib指令導入核心標籤庫
1)<c:if test=」」>標籤
其中test是返回boolean的條件
<%request.setAttribute("count", 10); %> <!--jstl標籤常常會和el配合使用 --> <!-- test表明的返回boolean的表達式 --> <c:if test="${count==10 }"> xxxx </c:if>
Index.jsp <!-- 用戶沒有登錄 --> <c:if test="${empty user }"> <a href="login.jsp">登錄</a> <a href="register.jsp">註冊</a> </c:if> <!-- 用戶已經登錄 --> <c:if test="${!empty user }"> <span>${user.name }</span> <a href="#">退出</a> </c:if>
Ceshi.jsp <% //模擬用戶已經登陸成功 User user=new User(); user.setId(100); user.setName("張三"); user.setPwd("123"); session.setAttribute("user", user); %>
2)<c:forEach>標籤
使用方式有兩種組合形式:
<!-- 模擬加強for productList---List<Product> for(Product product:productList){ System.out.print(product.getName()); } -->
<!-- items:一個集合或數組(從域中選) var:表明集合中的某一個元素 --> <c:forEach items="${productList }" var="pro"> ${pro.name } </c:forEach> <c:forEach items = "${map}" var="entry"> ${entry.key }->${entry.value.name }<br> </c:forEach>
事例:
<!-- 取集合list中的值 --> <c:forEach items="${userlist}" var="user"> ${user.name }...${user.age }<br> </c:forEach>
<!-- 取集合map中的值 --> <c:forEach items="${usermap }" var="entry"> ${entry.key }...${entry.value }<br> </c:forEach>
<!-- 嵌套取值 --> <c:forEach items="${usermap}" var="entry"> ${entry.key}...${entry.value}<br> <c:forEach items="${entry.value}" var="entry1"> ${entry1.key}...${entry1.value}<br> </c:forEach> </c:forEach>
<!-- 打印三角形 --> <c:forEach begin="0" end="4" var="i"> <c:forEach begin="0" end="${i }" var="j"> <%System.out.print("*");%> </c:forEach> <%System.out.println();%> </c:forEach>