EL(Express Lanuage):表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL出现的目的是要替代jsp页面中脚本的编写。
jsp脚本:<%=request.getAttribute(name)%>
EL表达式替代上面的脚本:${requestScope.name}
EL最主要的作用是获得四大域中的数据,格式${EL表达式}
EL获得pageContext域中的值:${pageScope.key}
EL获得request域中的值:${requestScope.key}
EL获得session域中的值:${sessionScope.key}
EL获得application域中的值:${applicationScope.key}
EL从四个域中获得某个值${key} ——同样是依次从pageContext域,request域,session域,application域中获取属性,在某个域中获取后将不在向后寻找
案例一:
<body> <%--模拟域中的数据--%> <% //存储一个字符串 request.setAttribute("School", "安徽信息工程学院"); User user1 =new User(); user1.setId("1"); user1.setUsername("gyf"); user1.setPassword("123"); //存储一个对象 session.setAttribute("user1", user1); //存储一个集合 List<User> list =new ArrayList<User>(); list.add(new User("2","zyl","456")); list.add(new User("3","zhangsan","789")); application.setAttribute("UserList", list); %> <%--脚本方式取出域中的值--%> <% request.getAttribute("school"); //取对象需要进行强转 User u1=(User)session.getAttribute("user1"); out.write(u1.toString()); %> <%--使用el表达式获取域中的值--%> <%--省去get方法,其次对于集合,采用数组的形式,下表表示第几个对象--%> ${requestScope.School} ${sessionScope.user1.name} ${applicationScope.list[1].name} <%--使用el表达式全域查找我想要的值--%> ${company} ${user.name} ${list[1].name} </body>获取JSP中域中的数据
pageScoperequestScopesessionScopeapplicationScope接收参数
相当于request.getParameter() 和 request.getParameterValues()
paramparamValues获取请求头信息
相当于request.getHeader()
headerheaderValues获取全局初始化参数
相当于this.getServletContext().getInitParameter(name)
initParamWEB开发中cookie
相当于request.getCookies()和cookie.getName()以及cookie.getValue()
WEB开发中的pageContext
相当于jsp中的pageContext,pageContext可以获得其他八大对象
${pageContext.request.contextPath}相当于
<%=pageContext.getRequest().getContextPath%>案例一:
新建一个form.jsp,将数据提交到form2.jsp中
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/EL/form2.jsp" method="post"> <input type="text" name="username"><br/> <input type="password" name="password"><br/> <input type="checkbox" name="hobby" value="zq">足球 <input type="checkbox" name="hobby" value="pq">排球 <input type="checkbox" name="hobby" value="ppq">乒乒球<br/> <input type="submit" value="提交"><br/> </form> </body> </html>该案例主要阐述,我们应该动态获取应用名称。页面跳转的时候就不会出现图片加载不全的问题: "${pageContext.request.contextPath}/EL/form2.jsp"(重点)
案例二:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--获得表单参数--%> <%request.getParameter("username");%> <%--使用el获取参数--%> ${param.username} ${header["User-Agent"]} ${initParam.AAA} <%--cookie.name--%> ${cookie.name.value} //<%--通过el表达式获取request对象--%> ${pageContext.request} ${pageContext.servletContext} // <%--动态获取web名称--%> // <%--<%request.getContextPath()%>--%> // <%--获取项目和wb应用名称--%> ${pageContext.request.contextPath} </body> </html>效果图下:
EL表达式可以进行运算 例如:
${1+1}empty表示判定,某个对象是不是空的。是null就返回true${empty user}${user==null?true:false}JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。jstl出现的目的同el一样也是要代替jsp页面中的脚本代码。JSTL标准标准标签库有5个子库,但随着发展,目前常使用的是他的核心库
标签库标签库的URI前缀Corehttp://java.sun.com/jsp/jstl/corecI18Nhttp://java.sun.com/jsp/jstl/fmtfmtSQLhttp://java.sun.com/jsp/jstl/sqlsqlXMLhttp://java.sun.com/jsp/jstl/xmlxFunctionshttp://java.sun.com/jsp/jstl/functionsfn从Apache的网站下载JSTL的JAR包。进入 http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/网址下载 JSTL的安装包。jakarta-taglibs-standard-1.1.2.zip,进行解压。此时,在lib目录下可以看到两个JAR文件,分别为jstl.jar和standard.jar。 其中,jstl.jar文件包含JSTL规范中定义的接口和相关类,standard.jar文件包含用于 实现JSTL的.class文件以及JSTL中5个标签库描述符文件(TLD)
其中test是返回boolean的条件
案例:用户登陆主页,登陆成功显示用户名称
<!-- 用户没有登录 --> <c:if test="${empty user}"> <li><a href="login.jsp">登录</a></li> <li><a href="register.jsp">注册</a></li> </c:if> <!-- 用户已经登录 --> <c:if test="${!empty user}"> <li>${user.name }</li> <li><a href="#">退出</a></li> </c:if> <c:forEach>标签使用方式有两种组合形式
第一种:
<%--forEach模拟 for(int i=0;i<=5;i++){ System.out.println(i); } --%> <c:forEach begin="0" end="5" var="i"> ${i} </c:forEach>效果图下: 第二种:
<%--模拟增强for productList---List<Product> for(Product product:productList){ System.out.println(product.getName()); } --%> <%--items:一个集合或数组 var:代表集合中的某一个元素--%> <c:forEach items="${productList}" var="product"> ${product.name} </c:forEach>练习:
<%@page import="java.util.HashMap"%> <%@page import="java.util.Map"%> <%@page import="com.gds.bean.User"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <!--遍历List<String>的值 --> <% List<String> strList =new ArrayList<String>(); strList.add("1"); strList.add("2"); strList.add("3"); strList.add("4"); strList.add("5"); request.setAttribute("strList", strList); %> <h1>取出strList的数据</h1> <c:forEach items="${strList}" var="str"> ${str} </c:forEach> <!-- 遍历List<User>的值 --> <% List<User> users =new ArrayList<User>(); User user1 =new User(); user1.setId("1"); user1.setUsername("zhangsan"); user1.setPassword("123"); User user2 =new User(); user1.setId("2"); user1.setUsername("lisi"); user1.setPassword("456"); users.add(user1); users.add(user2); request.setAttribute("users", users); %> <h1>取出users的数据</h1> <c:forEach items="${users}" var="user"> ${user } </c:forEach> <!-- 遍历Map<String,String>的值 --> <% Map<String, String> strMap = new HashMap<String, String>(); strMap.put("name","zyl"); strMap.put("age","18"); strMap.put("addr","anhui"); strMap.put("email","123@123.com"); session.setAttribute("strMap",strMap); %> <h1>取出map的数据</h1> <c:forEach items="${strMap}" var="str"> ${str.key}==${str.value} </c:forEach> <!-- 遍历Map<String,User>的值 --> <% Map<String, User> userMap = new HashMap<String,User>(); userMap.put("user1",user1); userMap.put("user2",user2); request.setAttribute("userMap",userMap); %> <h1>取出userMap中的数据</h1> <c:forEach items="${userMap}" var="entry"> ${entry.key}:${entry.value.name}:${entry.value.password} </c:forEach> </body> </html>效果如下: