<!-- userbean标签的标签体只会在实例化bean时才执行 -->
<%
pageContext.setAttribute("person",new Person());
%>
<jsp:useBean id="person" class="cn.itcast.domain.Person">
yyyy
</jsp:useBean>
<%=person.getName() %>
<head> <title>jsp:setProperty标签</title> </head> <body> <jsp:useBean id="person" class="cn.itcast.domain.Person"/> <jsp:setProperty name="person" property="name" value="zzzz"/> <jsp:setProperty name="person" property="birthday" value="<%=new Date() %>"/> <%=person.getName() %> <%=person.getBirthday() %> <jsp:getProperty name="person" property="name"/> <br/>----------http://localhost:8080/day08/2.jsp?name=kkkk------------<br/> <jsp:setProperty name="person" property="name" param="name"/> <%=person.getName() %> <br/>----------http://localhost:8080/day08/2.jsp?name=kkkk&age=12------------<br/> <jsp:setProperty name="person" property="*"/> <%=person.getName() %> <%=person.getAge() %> </body>
<title>el表达式</title> </head> <body> <% Person p = new Person(); p.setName("flx"); request.setAttribute("person",p); %> ${person.name } <!-- pageContext.findAttribute("person") --> <% p = new Person(); Address address = new Address(); address.setCity("北京"); p.setAddress(address); request.setAttribute("person",p); %> ${person.address.city } <% Person p1 = new Person("aaa"); Person p2 = new Person("bbb"); Person p3 = new Person("ccc"); List list = new ArrayList(); list.add(p1); list.add(p2); list.add(p3); request.setAttribute("list",list); %> ${list[0].name } <% Map map = new HashMap(); map.put("1",new Person("aa")); map.put("2",new Person("bb")); map.put("3",new Person("cc")); request.setAttribute("map",map); %> ${map['2'].name } ${pageContext.request.contextPath } <a href="${pageContext.request.contextPath }/servlet/LoginServlet" mce_href="${pageContext.request.contextPath }/servlet/LoginServlet">登陆</a>
jstl+ el获取数据
<head> <title>使用jstl+el获取数据</title> </head> <body> <% Person p1 = new Person("aaa"); Person p2 = new Person("bbb"); Person p3 = new Person("ccc"); List list = new ArrayList(); list.add(p1); list.add(p2); list.add(p3); request.setAttribute("list",list); %> <c:forEach var="p" items="${list}"> ${p.name }<br/> </c:forEach> <% Map map = new HashMap(); map.put("1",new Person("aa")); map.put("2",new Person("bb")); map.put("3",new Person("cc")); request.setAttribute("map",map); %> <c:forEach var="entry" items="${map}"> ${entry.key }=${entry.value.name }<br/> </c:forEach> <c:if test="${user!=null}"> 欢迎您:${user.username } </c:if> <c:if test="${user==null}"> <form> 用户名:<input type="text" name="username"> 密码:<input type="password" name="password"> <input type="submit" value="登陆"> </form> </c:if>
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <title>My JSP '6.jsp' starting page</title> <style type="text/css"> .even{ background-color: #CCCCFF; } .odd{ background-color: #CC99FF; } tr:hover{ background-color: #FFFF33; cursor:hand; } </style> </head>
<br/>------------------c:catch-------------------<br/> <c:catch var="exception"> <% int x = 1/0; %> </c:catch> ${exception.cause }<br/> ${exception.message }<br/> ${exception.stackTrace }<br/> <br/>------------------c:if-------------------<br/> <c:if var="a1" test="${sessionScope.user==null}"></c:if> ${a1 } <br/>------------------c:foreach-------------------<br/> <c:forEach var="num" begin="1" end="10"> ${num } </c:forEach> <br/>------------------c:foreach实现表格间色-------------------<br/> <% List list = new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("ccc"); list.add("ddd"); list.add("eee"); list.add("fff"); request.setAttribute("list",list); %> <table border="1" width="20%"> <c:forEach var="str" items="${list}" varStatus="status"> <tr class='${status.count%2==0?'even':'odd' }'> <td>${str }</td> </tr> </c:forEach> </table> <br/>------------------c:url-------------------<br/> <c:url var="url" value="/1.jsp"> <c:param name="username" value="中国"/> <c:param name="password" value="123"/> </c:url> <a href="${url }">点点</a> <c:url value="/2.jsp"/> <br/>------------------c:fortokens-------------------<br/> <% String s = "aaa,bbb,ccc"; request.setAttribute("s",s); %> <c:forTokens var="str" items="${s}" delims=","> ${str } </c:forTokens>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>sun el函数库</title> </head> <body> ${fn:trim(" aaa ") } <br/>------------------------------------<br/> <% List list = new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("ccc"); request.setAttribute("list",list); %> <c:forEach var="num" begin="0" end="${fn:length(list)}"> ${list[num] }<br/> </c:forEach> ${fn:length("aaaa") } <br/>------------------------------------<br/> ${fn:split("aaa,bbb,ccc",",")} <c:forEach var="str" items=''> ${str } </c:forEach> <br/>------------------------------------<br/> <% String arr[] = {"aaa","bbb","ccc"}; request.setAttribute("arr",arr); %> ${fn:join(arr,"-") } <br/>------------------------------------<br/> ${fn:escapeXml('<a href="">点点</a>')} </body> </html>