annie613
asked on
jsp and beans - how do you display a bean?
i am looking for some comments and suggestions about jsp and beans. i am trying to write a JavaBean class which I call DiceGame.java. This is to create values of the dice and keep score and count.
also i have a jsp file called DiceGame.jsp which includes an error page (which works b/c i keep getting errors) and a common html file for graphics (seems to work) and uses the JavaBean (here is my problem). I am not sure how to get this to work. I have in the jsp file what i think needs to be there..but i am not sure what i need for id.. i have it null as of now
<jsp:useBean id="" class="week12.DiceGame" scope="session" />
or where this is coming from..this is in my jsp file but its the id..where does that pull from???? below is my jsp and java file... do i need an html file to set up the table first...and have that change.... *confused*
also how do i set the Roll Button to call the rollDice() in the .java file?????
any suggestions are welcome!!! thanks in advance annie :)
******JAVA******
package week12;
/*
the user rolls two dice until the total score is > 50.
this class will create the values for the dice to keep track
of the total score and number of rolls
*/
public class DiceGame
{
private int count = 0;
private int score = 0;
private int die1 = 0;
private int die2 = 0;
public int getDie1()
{
return die1;
}//end getDie1()
public void setDie1(int d1)
{
this.die1 = d1;
}//end setDie1
public int getDie2()
{
return die2;
}//end getDie2()
public void setDie2(int d2)
{
this.die2 = d2;
}//end setDie2
public int getScore()
{
return score;
}//end getScore()
public void setScore(int s)
{
this.score = s;
}//end setScore
public int getCount()
{
return count;
}//end getCount()
public void setCount(int c)
{
this.count = c;
}//end setCount
//rollDice() creates random numbers for dice and ands to count and score
public void rollDice()
{
int die1 = 1 +(int)(Math.random() * 6);
int die2 = 1 +(int)(Math.random() * 6);
score = die1 + die2;
count++;
//need to add logic for >50
}//end rollDice()
//restart() sets all to 0 for new game
public void restart()
{
int count = 0;
int score = 0;
int die1 = 0;
int die2 = 0;
}//end restart()
}//end DiceGame
******END JAVA******
*********JSP********
<%--
display data from DiceGame javabean it contains an error page and includes
htlm files and uses the javabean.
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 "Transitional//EN">
<html>
<head>
<title>Dice Game Lesson 12</title>
<link rel=stylesheet
href="class12.css"
type="text/css">
</link>
</head>
<body bgcolor="#0033FF" >
<%@ page errorPage="Week12Errors.js p" %>
<jsp:useBean id="" class="week12.DiceGame" scope="session" />
<h1 align="center">Dice Game</h1>
<%@ include file="IT608top.jsp" %>
<br>
<br>
Here is the Dice Game. Click on the Roll Button until you roll 50
or more! Your results will display!!!<br>
Die 1 value: <jsp:getProperty name="die1" property="die1" /> <br>
Die 2 value: <jsp:getProperty name="die2" property="die2" /> <br>
Your Total Score: <jsp:getProperty name="score" property="score" /> <br>
Rolls So Far: <jsp:getProperty name="count" property="count" /> <br>
<tr><td><input type=submit value="Roll"></td>
//call rollDice() after you display bean
</body>
</html>
*******END JSP********
also i have a jsp file called DiceGame.jsp which includes an error page (which works b/c i keep getting errors) and a common html file for graphics (seems to work) and uses the JavaBean (here is my problem). I am not sure how to get this to work. I have in the jsp file what i think needs to be there..but i am not sure what i need for id.. i have it null as of now
<jsp:useBean id="" class="week12.DiceGame" scope="session" />
or where this is coming from..this is in my jsp file but its the id..where does that pull from???? below is my jsp and java file... do i need an html file to set up the table first...and have that change.... *confused*
also how do i set the Roll Button to call the rollDice() in the .java file?????
any suggestions are welcome!!! thanks in advance annie :)
******JAVA******
package week12;
/*
the user rolls two dice until the total score is > 50.
this class will create the values for the dice to keep track
of the total score and number of rolls
*/
public class DiceGame
{
private int count = 0;
private int score = 0;
private int die1 = 0;
private int die2 = 0;
public int getDie1()
{
return die1;
}//end getDie1()
public void setDie1(int d1)
{
this.die1 = d1;
}//end setDie1
public int getDie2()
{
return die2;
}//end getDie2()
public void setDie2(int d2)
{
this.die2 = d2;
}//end setDie2
public int getScore()
{
return score;
}//end getScore()
public void setScore(int s)
{
this.score = s;
}//end setScore
public int getCount()
{
return count;
}//end getCount()
public void setCount(int c)
{
this.count = c;
}//end setCount
//rollDice() creates random numbers for dice and ands to count and score
public void rollDice()
{
int die1 = 1 +(int)(Math.random() * 6);
int die2 = 1 +(int)(Math.random() * 6);
score = die1 + die2;
count++;
//need to add logic for >50
}//end rollDice()
//restart() sets all to 0 for new game
public void restart()
{
int count = 0;
int score = 0;
int die1 = 0;
int die2 = 0;
}//end restart()
}//end DiceGame
******END JAVA******
*********JSP********
<%--
display data from DiceGame javabean it contains an error page and includes
htlm files and uses the javabean.
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 "Transitional//EN">
<html>
<head>
<title>Dice Game Lesson 12</title>
<link rel=stylesheet
href="class12.css"
type="text/css">
</link>
</head>
<body bgcolor="#0033FF" >
<%@ page errorPage="Week12Errors.js
<jsp:useBean id="" class="week12.DiceGame" scope="session" />
<h1 align="center">Dice Game</h1>
<%@ include file="IT608top.jsp" %>
<br>
<br>
Here is the Dice Game. Click on the Roll Button until you roll 50
or more! Your results will display!!!<br>
Die 1 value: <jsp:getProperty name="die1" property="die1" /> <br>
Die 2 value: <jsp:getProperty name="die2" property="die2" /> <br>
Your Total Score: <jsp:getProperty name="score" property="score" /> <br>
Rolls So Far: <jsp:getProperty name="count" property="count" /> <br>
<tr><td><input type=submit value="Roll"></td>
//call rollDice() after you display bean
</body>
</html>
*******END JSP********
ASKER
so leaving it blank as i do is an illegal expression
what does the XXX mean???
jsp:getProperty name="XXX" property="count" />
do i need multiple lines for calling score too???
jsp:getProperty name="XXX" property="count" />
jsp:getProperty name="XXX" property="score" />
what does the XXX mean???
jsp:getProperty name="XXX" property="count" />
do i need multiple lines for calling score too???
jsp:getProperty name="XXX" property="count" />
jsp:getProperty name="XXX" property="score" />
Any name you pick to identify the bean.
>> do i need multiple lines for calling score too???
Yes.
<jsp:useBean id="my_bean_identifier" class="week12.DiceGame" scope="session" />
and then
<jsp:getProperty name="my_bean_identifier" property="count" />
<jsp:getProperty name="my_bean_identifier" property="score" />
>> do i need multiple lines for calling score too???
Yes.
<jsp:useBean id="my_bean_identifier" class="week12.DiceGame" scope="session" />
and then
<jsp:getProperty name="my_bean_identifier" property="count" />
<jsp:getProperty name="my_bean_identifier" property="score" />
ASKER
ok..say i want to call the my_bean_identifer dice
<body bgcolor="#0033FF" >
<%@ page errorPage="Week12Errors.js p" %>
<jsp:useBean id="dice" class="week12.DiceGame" scope="session" />
<h1 align="center">Dice Game</h1>
<%@ include file="IT608top.jsp" %>
<br>
<br>
Here is the Dice Game. Click on the Roll Button until you roll 50
or more! Your results will display!!!<br>
Die 1 value: <jsp:getProperty name="dice" property="die1" /> <br>
Die 2 value: <jsp:getProperty name="dice" property="die2" /> <br>
Your Total Score: <jsp:getProperty name="dice" property="score" /> <br>
Rolls So Far: <jsp:getProperty name="dice" property="count" /> <br>
</body>
that is how i would setup the code??? but if i run this code i still get errors??? is there something i have to do for the id???
<body bgcolor="#0033FF" >
<%@ page errorPage="Week12Errors.js
<jsp:useBean id="dice" class="week12.DiceGame" scope="session" />
<h1 align="center">Dice Game</h1>
<%@ include file="IT608top.jsp" %>
<br>
<br>
Here is the Dice Game. Click on the Roll Button until you roll 50
or more! Your results will display!!!<br>
Die 1 value: <jsp:getProperty name="dice" property="die1" /> <br>
Die 2 value: <jsp:getProperty name="dice" property="die2" /> <br>
Your Total Score: <jsp:getProperty name="dice" property="score" /> <br>
Rolls So Far: <jsp:getProperty name="dice" property="count" /> <br>
</body>
that is how i would setup the code??? but if i run this code i still get errors??? is there something i have to do for the id???
ASKER
Attempted a bean operation on a null object..
that is the error i receive???
that is the error i receive???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Id in usebean tag is to define a name for the nean instance in the scope. It is like a variable you can use later in your page:
<jsp:useBean id="myDiceBnea" class="week12.DiceGame" scope="session" />
Here's how u use the bean:
Die 1 value: <jsp:getProperty name="myDiceBean" property="die1" /> <br>
Die 2 value: <jsp:getProperty name="myDiceBean" property="die2" /> <br>
Your Total Score: <jsp:getProperty name="myDiceBean" property="score" /> <br>
Rolls So Far: <jsp:getProperty name="myDiceBean" property="count" /> <br>
<jsp:useBean id="myDiceBnea" class="week12.DiceGame" scope="session" />
Here's how u use the bean:
Die 1 value: <jsp:getProperty name="myDiceBean" property="die1" /> <br>
Die 2 value: <jsp:getProperty name="myDiceBean" property="die2" /> <br>
Your Total Score: <jsp:getProperty name="myDiceBean" property="score" /> <br>
Rolls So Far: <jsp:getProperty name="myDiceBean" property="count" /> <br>
ASKER
that is what i was missing the <jsp:setProperty name=dice" property="*" />
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
aozarov i forgot to populate as you suggested...
limaideal as for the roll that is what i needed to do for the dice!
thanks! cheers! :) annie
limaideal as for the roll that is what i needed to do for the dice!
thanks! cheers! :) annie
e.g
<jsp:useBean id="XXX" class="week12.DiceGame" scope="session" />
jsp:getProperty name="XXX" property="count" />