dポイントプレゼントキャンペーン実施中!

application.setAttributeとapplication.getAttributeを使ってint型変数の値を共有したいと思っています。
String型だとうまくいくのですがint型だとうまくいかないのです。

下記の上のソースはString型でのソースです。これの
String storedData = "JSPサンプル";
の部分をint型に変えたいと考えています。
下のソースはint型に変えたのですが,
Object型からint型にキャスト変換できないようなエラーが出て動きませんでした。

int型の変数を方法をapplication.setAttributeで使う方法はどうすればいいのでしょう?
ご存知のかたいらっしゃいましたら教えてください。よろしくお願いします。

-----------------------------------------------
<%@ page contentType="text/html;charset=Shift_JIS" %>
<html>
<head><title>Sample</title></head>
<body>
<%
//applicationオブジェクトに保存
String storedData = "JSPサンプル";
application.setAttribute("appdata",storedData);
//applicationオブジェクトから読み出し
Object readData = application.getAttribute("appdata");
String readDataString=(String)readData;
%>
applicationオブジェクト保存データ:<b><%= readDataString %></b>
</body>
</html>


-----------------------------------------------

<%@ page contentType="text/html;charset=Shift_JIS" %>
<html>
<head><title>Sample0</title></head>
<body>
<%
int storedData = 10;
application.setAttribute("appdata",storedData);
//applicationオブジェクトから読み出し
Object readData = application.getAttribute("appdata");
int readDataString=(int)readData;
%>
applicationオブジェクト保存データ:<b><%= read %></b>

</body>
</html>

A 回答 (3件)

int storedData = 10;


intObject = new Integer(int storedData);
application.setAttribute("appdata", intObject);

ですね。intをIntegerに変換してsetAttributeに渡します。

//applicationオブジェクトから読み出し
Object readData = application.getAttribute("appdata");
Integer intReadData = (Integer)readData;
int readDataString = intReadData.intValue();

こちらも。getAttributeはObjectしか返さないので、受け取ってからIntegerにキャストし、その後で読み取っています。

この回答への補足

直していただいてありがとうございます。
しかし、intObject = new Integer(int storedData);
の部分で、下記のエラーが出てしまいうまく動きません。

生成されたサーブレットのエラーです:
Syntax error on token "int", delete this token

プログラムのソースの問題ではなく、なにか他に問題があるのでしょうか?

補足日時:2006/08/20 23:04
    • good
    • 0

あー、すいません、元ソースのバグの一つですね。

消し忘れてました。
intObject = new Integer(storedData);
が正解です。
#いちいち動作確認してませんので・・・

あと、
int readDataString = intReadData.intValue();
の部分も、どうせ使うときは文字型なので

String readDataString = intReadData.toString();
の方がよさそうですね。

#他に単純ミスがあれば、自力で直してください。

この回答への補足

何度もすいません。エラーの種類が変わっただけで出来ませんでした。

intObject = new Integer(storedData);
application.setAttribute("appdata", intObject);

の部分で
intObject cannot be resolved
のエラーが起きてしまいます。

補足日時:2006/08/21 01:19
    • good
    • 0
この回答へのお礼

若干違うやり方で解決致しました。
ありがとうございました。

お礼日時:2006/08/21 16:09

Object型は全ての親クラスと入門書等にはありますが、プリミティブ型はその限りではありません。



intの場合はIntegerにラップしてセットするのが妥当だと思います。

・int→Integer
 new Integer(int i);

・Integer→int
 Integer#intValue();

この回答への補足

ラップしてセットするとはどのような記述をすれば良いのでしょうか?

直接new Integerを追加すると、追加した7行目で下のエラーが出てしまいました。
Syntax error on token "int", delete this token


-----------------------------------------------
<%@ page contentType="text/html;charset=Shift_JIS" %>
<html>
<head><title>Sample</title></head>
<body>
<%
int storedData = 10;
new Integer(int storedData);
application.setAttribute("appdata",storedData);
//applicationオブジェクトから読み出し
Object readData = application.getAttribute("appdata");
int readDataString = Integer.parseInt(readData);
%>
applicationオブジェクト保存データ:<b><%= readDataString %></b>
</body>
</html>

補足日時:2006/08/20 00:50
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!