JSPAdvertisement宣言 (Declaration)
書式
<%! 〜 %> 変数の宣言
宣言部で宣言した変数はインスタンス変数として扱われるため、複数のリクエスト間で共有される。
<%! 変数宣言; %> 例
<%@ page contentType="text/html; charset=Shift_JIS" %>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">
<title>test</title>
</head>
<body>
<%!
public int numA;
public int numB = 10;
%>
<p>
numA = <%= numA %><br>
numB = <%= numB %>
</p>
</body>
</html>
実行結果
numA = 0 numB = 10 メソッドの宣言<%! メソッド宣言 %> 例
<%@ page contentType="text/html; charset=Shift_JIS" %>
<html>
<head><title>test</title></head>
<body>
<%!
public String createMessage(String str){
return "[" + str + "]";
}
%>
<p>
<%= createMessage("welcome") %>
</p>
</body>
</html>
実行結果
[welcome] |