JSP

1月 1, 2003 · Posted in JSP · Comment 

Advertisement


paramアクション

書式

<jsp:param name="パラメータ名" value="値" />

パラメータを他ファイルに渡す。

paramアクションの利用

メイン (param.jsp)

<%@ page contentType="text/html; charset=Shift_JIS" %>
<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">

<html lang="ja">
<head>
<title>テスト</title>
</head>
<body>

<%
    String data = request.getParameter("submit");
    String name = request.getParameter("name");
    String age = request.getParameter("age");

    if(data != null){
%>
        <jsp:forward page="param2.jsp">
            <jsp:param name="name" value="<%= name %>" />
            <jsp:param name="age" value="<%= age %>" />
        </jsp:forward>
<%
    }
%>

<form method="post" action="./param.jsp">
NAME<br>
<input type="textbox" name="name"><br>
<br>
AGE<br>
<input type="textbox" name="age"><br>
<br>
<input type="submit" name="submit" value="送信">
</form>

</body>
</html>

param2.jsp

<%@ page contentType="text/html; charset=Shift_JIS" %>
<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">

<html lang="ja">
<head>
<title>テスト</title>
</head>
<body>

<%
    String name = request.getParameter("name");
    String age = request.getParameter("age");
%>

<h1>受け取ったパラメータ</h1>
<p>
NAME:<%= name %><br>
AGE:<%= age %>
</p>

</body>
</html>


JSP

1月 1, 2003 · Posted in JSP · Comment 

Advertisement


コメント (Comment)

書式

HTMLコメント <!-- コメント -->
JSPコメント  <%-- コメント -->

上記2つのコメントはJSPの任意の位置に記述することができる。HTMLコメントはJSP実行時にソースコードに記述される。JSPコメントはソースコードに記述されない。

宣言、式、スクリプトレット内にはJavaのコメント // 、 /* 〜 */ を記述することもできる。

JSP

1月 1, 2003 · Posted in JSP · Comment 

Advertisement


宣言 (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]

« 前ページへ次ページへ »