1.九大内置对象
request 转译后对应HttpServletRequest/ServletRequest对象
response 转译后对应HttpServletRespons/ServletResponse对象
session 转译后对应HttpSession对象
application 转译后对应ServletContext对象
exception 转译后对应Throwable对象,代表由其他JSP页面抛出的异常对象,只会出现于JSP错误页面(isErrorPage设置为true的JSP页面)
page 转译后对应this
config 转译后对应ServletConfig对象
out 转译后对应JspWriter对象,其内部关联一个PringWriter对象
pageContext 配置康text 转译后对应PageContext对象,它提供了JSP页面资源的封装,并可设置页面范围属性
2.在域中保存对象 使用EL获取
User user = new User();
user.setName("wl");
user.setPwd(123);
// 添加地址对象
Address address = new Address();
address.setCity("中国");
user.setAddress(address);
//out.print(user);
request.setAttribute("u", user);
<!-- 如果user对象中 有属性也是对象 -->
${u.name}
<!-- javaBean导航 通过调用get方法获取 直接使用点-->
${u.address.city }
<!-- 使用[] 获取值 -->
<!-- 如果域中保存的是个集合 怎么获取 -->
${u["name"] } ${u['name'] }
3.EL表达式获取集合数据
// ArrayList集合
ArrayList<String> arrayList = new ArrayList();
arrayList.add( 1,"wl");
arrayList.add( "2","whh");
arrayList.add( "3","wll");
request.setAttribute(1, "arrayList");
${arrayList.[1]}
//Map集合
HashMap<String, String> hashMap = new HashMap();
hashMap.put("1", "大王");
hashMap.put("2", "小王");
hashMap.put("3", "鬼");
request.setAttribute("hasMap", hashMap);
${hasMap.[2]}
4.利用EL表达式判断empty是否为空
String str1 = null;
request.setAttribute("str1", str1);
String str2 = "";
request.setAttribute("str2", str2);
<!-- 判断是否为空 只有没有值就返回true-->
${empty str1 }
${empty str2 }
判断判断集合中指向空 和 不添加值 返回什么
ArrayList<String> list1= new ArrayList();
list1.add("aaa");
request.setAttribute("list1", list1);
ArrayList<String> list2= new ArrayList();
request.setAttribute("list2", list2);
ArrayList<String> list3 = null;
request.setAttribute("list3", list3);
${empty list1}
${empty list2}
${empty list3}
5.三目运算符
String sex = "nv";
request.setAttribute("s", sex);
${empty list3 ? "我是前面的" : "我是后边的" }
<br>
<input type="radio" value="nan" name="sex" ${s=="nan" ? "checked='checked' ":"" }>男<br>
<input type="radio" value="nv" name="sex" ${s=="nv" ? "checked='checked' ": ""}>女
6.测试引试对象
<%
pageContext.setAttribute("p", "pageContext", PageContext.PAGE_SCOPE);
pageContext.setAttribute("p1", "request", PageContext.REQUEST_SCOPE);
pageContext.setAttribute("p2", "session", PageContext.SESSION_SCOPE);
pageContext.setAttribute("p3", "application", PageContext.APPLICATION_SCOPE);
%>
${pageScope.p }
${requestScope.p1 }
${sessionScope.p2 }
${applicationScope.p3 }
7.用EL表达式获取请求中的参数
<!-- 相当于调用了get. -->
${param.userName }<br>
${paramValues.hobby[0] }<br>
<!-- 取请求头 -->
${header["User-Agent"] }<br>
<!-- 取cookie值 -->
${cookie.JSESSIONID.value }<br>
8.JSTL表达式
需要一个包,JSTL表达式 处理jsp上的逻辑 通用标签 set out remove
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="num" value="10" scope="session"></c:set>
<c:remove var="num" scope="session"/>
<!--相当于直接输出于网页上 -->
<c:out value="${num }" default="000"></c:out><br>
${num }
9.JSTL声明变量 和逻辑表达
<!-- 声明变量 -->
<c:set var="n" value="10"></c:set>
<!-- 逻辑表达式 if choose -->
<c:if test="${n<5 }">
aaaa
</c:if>
<c:if test="${n>5 }">
bbbb
</c:if>
<c:choose>
<c:when test="${n==5 }">5</c:when>
<c:when test="${n==10}">10</c:when>
<c:when test="${n==8}">1</c:when>
<c:otherwise>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
</c:otherwise>
</c:choose>
10.循环 step 循环曾量
<c:forEach var="i" begin="0" end="10" step="1">
${i }<br>
</c:forEach>
<%
ArrayList<String> list = new ArrayList();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");
request.setAttribute("list",list);
%>
<!-- items 你要遍历容器 var 表示容器中保存的对象-->
<c:forEach items="${list }" var="j" step="2">
${j }
</c:forEach>