EL表达式+JSTL

0717.png

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();
        }
        
    %>
    

ck1:<%=v1 %>
ck2:<%=v2 %>

EL表达式获取 ck1 : ${cookie.uname.value }
ck2 : ${cookie.uinfo.value }

获取header


    //jsp
    <%
        Enumeration keys = request.getHeaderNames();
        while( keys.hasMoreElements() ){
            String k = (String)keys.nextElement();
            out.print("
"+ k +"::"+request.getHeader( k )); } %>

1.<%=request.getHeader("user-agent") %>
2.<%=request.getHeader("accept-language") %>


EL表达式 1.${header["user-agent"] }
2.${header["accept-language"] }

其它符号


    <%
        pageContext.setAttribute("fff", new Users(300,"周瑜",1.82));
        pageContext.setAttribute("sss", new Users(301,"太史慈",1.96));
    %>
         个子高: ${fff.uheight > sss.uheight ? fff.uname : sss.uname }
个子高2: ${fff.uheight gt sss.uheight ? fff.uname : sss.uname }

名字相等吗 : ${ fff.uname == sss.uname ? "" :"不" }相等
名字相等吗2 : ${ fff.uname eq sss.uname ? "" :"不" }相等

相等吗 : ${ fff.uname != sss.uname ? "不" :"" }是一个人
相等吗 : ${ fff.uname ne sss.uname ? "不" :"" }是一个人

<% pageContext.setAttribute("ttt", "asdfasdf"); pageContext.setAttribute("qqq", ""); %> 存在或为空1: ttt ${ empty ttt ? "无":"有" }数据
存在或为空2: qqq ${ empty qqq ? "无":"有" }数据
<%-- qqq!=null && qqq.size>0 --%> 存在或为空3: www ${ empty www ? "无":"有" }数据

JSTL

JSP Standard Tag Library

jsp引入

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

定义


    
    
    
    1.${a1}
2.${a2}
3.${requestScope.a2}

输出



    输出:
0.
1.
2.
3.
3.--${requestScope.a2.uname}

删除


    删除:
1.${a1}
2.${a2}
3.${requestScope.a2}

异常


    异常:
1. ${requestScope.a2.uname1}

静态/动态引入


    静态/动态引入:
<%@ include file="/myDiv1.jsp" %> 2 3
6
7 210

路径


    路径:
    
    
        50
        
    
    
    ${aa}--${bb}
003.PNG

if



    <%  
    
        pageContext.setAttribute("upd", new Users(400,"陈宫",173));
    
        List ulist =new ArrayList();
        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:
没有要修改的对象 有有有

swith


    swith:
小兵 吾长 大将

for



    for:
${st.index}--${f.uname}--${f.uheight}
${s}
${x}
004.PNG

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 %>
2.<%=fmt.format(d1) %>


3.
4.
5.
6.
7.
005.PNG

functions标签



    <%
        pageContext.setAttribute("xyz", "aabbccdd");
    %>
    1.${fn:substring(xyz,2,4)   }
2.${fn:substring(xyz,4,-1) }
3.${fn:replace(xyz,"bc","海洋") }
4.${fn:toUpperCase(xyz) }
006.PNG

你可能感兴趣的:(EL表达式+JSTL)