JSPの暗黙オブジェクト
参考サイトを参照。
暗黙オブジェクト
index.jsp に暗黙オブジェクト request から取得できるデータを表示してみる。
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> <p> 日本語の表示を確認 </p> <h3>式</h3> <%=1+1 %> <br /> <%=new java.util.Date() %> <!-- HTMLコメント --> <%-- JSPコメント --%> <h3>JSP宣言</h3> <%! int count = 10; %> <%=count %> <h3>スクリプトレット</h3> <% out.print("scriptlet<br />"); %> <h3>requestの情報</h3> authType=<%=request.getAuthType() %><br /> characterEncoding=<%=request.getCharacterEncoding() %><br /> contentLength=<%=request.getContentLength() %><br /> contentType=<%=request.getContentType() %><br /> contextPath=<%=request.getContextPath() %><br /> localAddr=<%=request.getLocalAddr() %><br /> localName=<%=request.getLocalName() %><br /> localPort=<%=request.getLocalPort() %><br /> method=<%=request.getMethod() %><br /> pathInfo=<%=request.getPathInfo() %><br /> pathTranslated=<%=request.getPathTranslated() %><br /> protocol=<%=request.getProtocol() %><br /> queryString=<%=request.getQueryString() %><br /> remoteAddr=<%=request.getRemoteAddr() %><br /> remoteHost=<%=request.getRemoteHost() %><br /> remotePort=<%=request.getRemotePort() %><br /> remoteUser=<%=request.getRemoteUser() %><br /> requestedSessionId=<%=request.getRequestedSessionId() %><br /> requestURI=<%=request.getRequestURI() %><br /> scheme=<%=request.getScheme() %><br /> serverName=<%=request.getServerName() %><br /> serverPort=<%=request.getServerPort() %><br /> servletPath=<%=request.getServletPath() %><br /> </body> </html>
パラメータ
URL にパラメータを追加すれば、request.getParameter() で値を取得できる。
index.jsp
<h3>パラメータ</h3> a=<%=request.getParameter("a") %><br /> b=<%=request.getParameter("b") %><br /> c=<%=request.getParameter("c") %><br />
EL式
EL式は、${式} の書式で記述できる。
index.jsp
<h3>EL式</h3> ${100 * 100}<br /> <% request.setAttribute("test", new java.util.Date()); %> ${test}<br />
JSTL
2つのJARファイルを以下の場所からコピーして、WEB-INF/lib の下に貼り付ける。
\\kgakusei1\share\澤田\jstl\lib
c:setタグ
c:set タグで、値を設定できる。
実際には、暗黙オブジェクト request に対して、setAttribute() メソッドを呼んでいる。
jstl.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSTL</title> </head> <body> <h1>JSTL</h1> <c:set var="data" value="てすと!" /> ${data}<br /> </body> </html>
c:ifタグ
c:if タグで、単一の条件分岐を記述できる。
jstl.jsp
<c:set var="count" value="10" /> <c:if test="${count >= 10}" > count=${count} </c:if> <c:if test="${count >= 11}" > count=${count} </c:if>
c:chooseタグ
c:choose タグで複数の条件分岐を記述できる。
jstl.jsp
<c:set var="age" value="9" /> <c:choose> <c:when test="${age >= 30}">30代以上</c:when> <c:when test="${age >= 20}">20代</c:when> <c:when test="${age >= 10}">10代</c:when> <c:otherwise>10歳以下</c:otherwise> </c:choose>
c:forEachタグ
c:forEach タグでループを記述できる。
items属性にループの対象となる配列またはコレクションを指定し、var属性に取得した要素を格納する変数名を指定する。
jstl.jsp
<h3>c:forEach</h3> <% List<String> list = new ArrayList<String>(); list.add("Wii U"); list.add("PS Vita"); list.add("Nintendo 3DS"); request.setAttribute("list",list); %> <c:forEach var="s" items="${list}"> ${s}<br /> </c:forEach>
fmt:formatDateタグ
fmt:formatDateタグで書式を指定して日時を出力できる。
jstl.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <h3>fmt:formatDate</h3> <% request.setAttribute("date", new Date()); %> <fmt:formatDate value="${date}" pattern="yyyy年MM月dd日(E) a KK時mm分ss秒" /><br>