cookie的保存和获得
保存
<%
Cookie c1 = new Cookie("uname","abcd");
c1.setMaxAge(5*60);
Cookie c2 = new Cookie("uinfo","xyz");
c2.setMaxAge(5*60);
//保存cookie
response.addCookie(c1);
response.addCookie(c2);
%>
获取
//jsp获取
<%
String v1 = "";
String v2 = "";
Cookie[] cks = request.getCookies();
for(int i = 0 ;i < cks.length ;i++){
if("uname".equals( cks[i].getName())) v1 = cks[i].getValue();
if("uinfo".equals( cks[i].getName())) v2 = cks[i].getValue();
}
%>
<br/><br/>
ck1:<%=v1 %><br/>
ck2:<%=v2 %><br/>
<hr/>
EL表达式获取
ck1 : ${cookie.uname.value }<br/>
ck2 : ${cookie.uinfo.value }<br/>
获取header
//jsp
<%
Enumeration keys = request.getHeaderNames();
while( keys.hasMoreElements() ){
String k = (String)keys.nextElement();
out.print("<br/>"+ k +"::"+request.getHeader( k ));
}
%>
<hr/><hr/>
1.<%=request.getHeader("user-agent") %><br/>
2.<%=request.getHeader("accept-language") %><br/>
<hr/><hr/>
EL表达式
1.${header["user-agent"] }<br/>
2.${header["accept-language"] }<br/>
其它符号
<%
pageContext.setAttribute("fff", new Users(300,"周瑜",1.82));
pageContext.setAttribute("sss", new Users(301,"太史慈",1.96));
%>
个子高: ${fff.uheight > sss.uheight ? fff.uname : sss.uname }<br/>
个子高2: ${fff.uheight gt sss.uheight ? fff.uname : sss.uname }<br/><br/>
名字相等吗 : ${ fff.uname == sss.uname ? "" :"不" }相等<br/>
名字相等吗2 : ${ fff.uname eq sss.uname ? "" :"不" }相等<br/><br/>
相等吗 : ${ fff.uname != sss.uname ? "不" :"" }是一个人<br/>
相等吗 : ${ fff.uname ne sss.uname ? "不" :"" }是一个人<br/><br/>
<%
pageContext.setAttribute("ttt", "asdfasdf");
pageContext.setAttribute("qqq", "");
%>
存在或为空1: ttt ${ empty ttt ? "无":"有" }数据<br/>
存在或为空2: qqq ${ empty qqq ? "无":"有" }数据<br/> <%-- qqq!=null && qqq.size>0 --%>
存在或为空3: www ${ empty www ? "无":"有" }数据<br/>
JSTL
JSP Standard Tag Library
jsp引入
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
定义
<c:set var="a1" value="aabbcc"/>
<c:set var="a2" value="xx<font color='red'>yy</font>zz"/>
<c:set var="a2" value="${upd}" scope="request"/>
1.${a1}<br/>
2.${a2}<br/>
3.${requestScope.a2}<br/>
输出
输出:<br/>
0.<c:out value="a1"/><br/>
1.<c:out value="${a1}"/><br/>
2.<c:out value="${a2}" escapeXml="false"/><br/>
3.<c:out value="${a2}"/><br/>
3.<c:out value="${requestScope.a2.uname}"/>--${requestScope.a2.uname}<br/>
删除
删除:<br/>
<c:remove var="a2" scope="page"/>
1.${a1}<br/>
2.${a2}<br/>
3.${requestScope.a2}<br/>
异常
异常:<br/>
<c:catch var="haha">
1. ${requestScope.a2.uname1}
</c:catch>
静态/动态引入
静态/动态引入:<br/> <!-- jsp代码拷贝到引入的位置 一起翻译成 java文件,编译成class,运行返回html代码 -->
<!-- 各个jsp生成的html代码拷贝到引入的位置 -->
<table width="200" height="150" border="1">
<tr>
<td><%@ include file="/myDiv1.jsp" %></td><!--静态引入-->
<td>2</td>
<td>3</td>
</tr>
<tr>
<td><jsp:include page="/myDiv1.jsp"/></td><!--动态引入-->
<td>
<jsp:include page="/myDiv1.jsp">
<jsp:param value="100" name="tp"/><!--动态引入-->
</jsp:include>
</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td><c:import url="/myDiv1.jsp"/></td><!--动态引入-->
<td><c:import url="/myDiv1.jsp">
<c:param name="tp">210</c:param>
</c:import>
</td>
</tr>
</table>
路径
路径:
<c:url var="aa" value="http://www.baidu.com"/>
<c:url var="bb" value="http://www.baidu.com">
<c:param name="id">50</c:param>
<c:param name="id" value="${a1}"></c:param>
</c:url>
${aa}--${bb}
if
<%
pageContext.setAttribute("upd", new Users(400,"陈宫",173));
List<Users> ulist =new ArrayList<Users>();
ulist.add( new Users(100,"诸葛亮",1.84));
ulist.add( new Users(101,"关羽",2.12));
ulist.add( new Users(102,"张飞",1.98));
ulist.add( new Users(103,"刘备",1.74));
pageContext.setAttribute("fff", ulist);
%>
if:<br/>
<c:if test="${ empty upd }" var="ret">
没有要修改的对象
</c:if>
<c:if test="${!ret}">
有有有
</c:if>
swith
swith:<br/>
<c:choose>
<c:when test="${upd.uheight le 170}">小兵</c:when>
<c:when test="${upd.uheight le 180}">吾长</c:when>
<c:otherwise>大将</c:otherwise>
</c:choose>
for
for:<br/>
<c:forEach items="${fff}" var="f" varStatus="st"><!-- for( Users f : fff) -->
${st.index}--${f.uname}--${f.uheight}<br/>
</c:forEach>
<c:forTokens items="111,222,333,444#555,666" delims="," var="s">
${s}<br/>
</c:forTokens>
<c:forEach begin="1" end="10" step="2" var="x"> <!-- for( int i = 1 ; i <=10 ; i+=2) -->
${x}
</c:forEach>
fmt标签
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="m" %>
<%
Date d1 = new Date();
pageContext.setAttribute("ttt", d1);
SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒SSS毫秒 E");
%>
1.<%=d1 %><br/>
2.<%=fmt.format(d1) %><br/>
<br/><br/>
3.<m:formatDate value="${ttt}" pattern="yyyy年MM月dd日 hh时mm分ss秒SSS毫秒 E"/>
<hr/>
4.<m:formatNumber value="32454245.24545" pattern=",###.####"/><br/>
5.<m:formatNumber value="32454245.24" pattern=",###.0000"/><br/>
6.<m:formatNumber value="1.64" pattern=",###.0000"/><br/>
7.<m:formatNumber value="1.64" pattern=",000.0000"/><br/>
functions标签
<%
pageContext.setAttribute("xyz", "aabbccdd");
%>
1.${fn:substring(xyz,2,4) }<br/>
2.${fn:substring(xyz,4,-1) }<br/>
3.${fn:replace(xyz,"bc","海洋") }<br/>
4.${fn:toUpperCase(xyz) }