前置準備
package model; public class Bean { public String name = "今天禮拜四。"; public String getName() { return name; } public void setName(String name) { this.name = name; } }
一般
<% Date date = new Date(); String DateStr = new SimpleDateFormat("yyyyMMdd").format(date); pageContext.setAttribute("DateStr",DateStr); %>${DateStr}
物件
<% /**物件**/ Bean bean = new Bean(); pageContext.setAttribute("bean", bean); %>${bean.name}
${bean["name"]}
${bean.getName()}
Null
<% /**Null**/ String myNull = null; bean = null; pageContext.setAttribute("myNull", myNull); pageContext.setAttribute("bean", bean); %>myNull = ${myNull}
myNull = ${bean}
判斷
<% /**判斷式**/ String str = "AAAA"; String str1 = "AAAA"; String str2 = "BBBB"; pageContext.setAttribute("str", str); pageContext.setAttribute("str1", str1); pageContext.setAttribute("str2", str2); %>${str == str1}
${str2 == str1}
容器的使用
<% /**容器的使用**/ ArrayListlist = new ArrayList (); list.add("AAA"); list.add("C"); list.add("C"); list.add("DDDD"); Map map = new HashMap<>(); map.put("s1", "AAAA"); map.put("s2", "BBBB"); pageContext.setAttribute("map", map); pageContext.setAttribute("list", list); %> ${map["s1"]}
${list["0"]}
cookie
<% /**處理cookie**/ Cookie[] cks = request.getCookies(); Cookie ck = new Cookie("SJ","aBCD"); response.addCookie(ck); %>${cookie.SJ.value}
其他參考