1.EL表达式($中间不能有空格{})
1.布尔型 ${true} <br/>
2.整型 ${10} 加空格$ {10}<br/>
3.浮点型 ${10.5}<br/>
4.空类型${null } java: <% String a =null; %><%=a %><br/>
5.字符串型 ${"jl"}<br>
6.四则运算 ${1+2-1*4/2%2} <br/>
<%
//对于集合来说只有存了值,有长度才不是null
List<String> strList = new ArrayList<String>();
pageContext.setAttribute("s",strList);
//对于对象来说,new了之后就不是null
User user = new User();
pageContext.setAttribute("u",user);
%>
7.empty运算: ${empty(u) } ${empty(s) }<br>
8.判断两个password是否相等:
<%
String pwd1 ="1111";
String pwd2 ="1221f";
String pwd3 ="1111";
pageContext.setAttribute("pwd1",pwd1);
pageContext.setAttribute("pwd2",pwd2);
pageContext.setAttribute("pwd3",pwd3);
%>
${pwd1 == pwd2 } ${pwd1 == pwd3 }<br/>
9.错误的写法:
$ {pwd1 } == ${pwd2 } $ {${pwd1 } == ${pwd2 } }<br>
10.从指定域中找:
<%
pageContext.setAttribute("password",pwd1);
request.setAttribute("password",pwd2);
session.setAttribute("password","9999");
application.setAttribute("password","0000");
%>
${password}(在最小的域中找) ${pageScope.password } ${requestScope.password }
${sessionScope.password } ${applicationScope.password }<br>
11.查找属性可用"."与"[]":
${pageScope.password } ${requestScope["password"] }<br/>
2.整型 ${10} 加空格$ {10}<br/>
3.浮点型 ${10.5}<br/>
4.空类型${null } java: <% String a =null; %><%=a %><br/>
5.字符串型 ${"jl"}<br>
6.四则运算 ${1+2-1*4/2%2} <br/>
<%
//对于集合来说只有存了值,有长度才不是null
List<String> strList = new ArrayList<String>();
pageContext.setAttribute("s",strList);
//对于对象来说,new了之后就不是null
User user = new User();
pageContext.setAttribute("u",user);
%>
7.empty运算: ${empty(u) } ${empty(s) }<br>
8.判断两个password是否相等:
<%
String pwd1 ="1111";
String pwd2 ="1221f";
String pwd3 ="1111";
pageContext.setAttribute("pwd1",pwd1);
pageContext.setAttribute("pwd2",pwd2);
pageContext.setAttribute("pwd3",pwd3);
%>
${pwd1 == pwd2 } ${pwd1 == pwd3 }<br/>
9.错误的写法:
$ {pwd1 } == ${pwd2 } $ {${pwd1 } == ${pwd2 } }<br>
10.从指定域中找:
<%
pageContext.setAttribute("password",pwd1);
request.setAttribute("password",pwd2);
session.setAttribute("password","9999");
application.setAttribute("password","0000");
%>
${password}(在最小的域中找) ${pageScope.password } ${requestScope.password }
${sessionScope.password } ${applicationScope.password }<br>
11.查找属性可用"."与"[]":
${pageScope.password } ${requestScope["password"] }<br/>
2.JSTL
<h1>JSTL:JSP标准标签库</h1><c:if test="${1==1}">
1=1
</c:if><br/>
<%
List<String > strList = new ArrayList<String>();
strList.add("1");
strList.add("2");
strList.add("3");
strList.add("4");
pageContext.setAttribute("strList",strList);
%>
<c:forEach var="str" items="${strList}">
${str }
</c:forEach><br/>
<%
int a = (int) (Math.random()*4);
pageContext.setAttribute("a",a);
Random rand = new Random();
int b = rand.nextInt(50)+5;//50~55
pageContext.setAttribute("b",b);
%>
<c:choose>
<c:when test="${a==1}">
产生的随机数是1
</c:when>
<c:when test="${a==2}">
产生的随机数是2
</c:when>
<c:when test="${a==3}">
产生的随机数是3
</c:when>
<c:when test="${b==55}">
产生的随机数是50
</c:when>
<c:otherwise>
产生的随机数是4
</c:otherwise>
</c:choose>