Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 513
  • Last Modified:

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.jsp" %>
        <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********
0
annie613
Asked:
annie613
  • 5
  • 3
  • 2
2 Solutions
 
aozarovCommented:
Id is the value used by the get/set Property
e.g
<jsp:useBean id="XXX" class="week12.DiceGame" scope="session" />

jsp:getProperty name="XXX" property="count" />
0
 
annie613Author Commented:
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" />


0
 
aozarovCommented:
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" />
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
annie613Author Commented:
ok..say i want to call the my_bean_identifer dice

<body bgcolor="#0033FF" >

        <%@ page errorPage="Week12Errors.jsp" %>
        <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???
0
 
annie613Author Commented:
Attempted a bean operation on a null object..
that is the error i receive???

0
 
aozarovCommented:
Did you populate the bean from the request?
Add after <jsp:useBean id="dice" class="week12.DiceGame" scope="session" />
<jsp:setProperty name=dice" property="*"  />

see http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html#Section8.2
0
 
limaidealCommented:
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>
0
 
annie613Author Commented:
that is what i was missing the <jsp:setProperty name=dice" property="*"  />
0
 
limaidealCommented:
You probably also want to put
<%
if ( null != request.getParameter("Roll") )
     myDiceBean.rollDice()
%>

after the useBean tag.
0
 
annie613Author Commented:
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now