Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

jsp and java files - where do i put code???

Posted on 2005-04-20
23
Medium Priority
?
218 Views
Last Modified: 2010-04-01
i am working with beans in jsp and java... and i have a java file that is a bean and jsp file..im trying to figure out where to put some code..

i need to have a condition for if count is less than 50 do - rollDice() else if count is 50 or greater - show message- restart() - rollDice();

does this go in my jsp page or my java file?????

also i have a roll button on my jsp page for rolling the dice....how do i set it up so that when i click the roll button on the jsp page it activates the rollDice() in the java file?????????

any suggestions...thanks in advance annie :)

****************************JAVA***********************
package week12;
public class DiceGame
{
        private String dice = "dice";
        private int count = 0;
        private int score = 0;
        private int die1 = 0;
        private int die2 = 0;
   
        public String getDice()
        {
            return dice;
        }//end getDice()
        public void setDice(String d)
        {
            this.dice = d;
        }//end setDice
       
        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++;      
       }//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
****************************JAVA***********************

****************************JSP***********************
<!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="dice" class="week12.DiceGame" scope="session" />
       
        <jsp:setProperty name="dice" property="*"/>
       
        <h1 align="center">Dice Game</h1>
       
        <%@ include file="IT608top.jsp" %>
        <br>
        <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><br>
       
        <INPUT TYPE="SUBMIT" value="Roll">
       
        <%
        if(null != request.getParameter("Roll"))
            dice.rollDice();
        %>
       
        <%-- where would this go - might need to fix logic
        if(count < 50)
        {
            rollDice();
            //display this message at bottom of page
            Roll Dice until you get 50 points.
        }
        else
        {
           
            //display this message at bottom of page
            Click "Roll" to activate a new game.
            restart();
            then rollDice();
           
        }
        --%>
       
    </body>
</html>

****************************JSP***********************

0
Comment
Question by:annie613
  • 14
  • 9
23 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13828418
 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><br>
       
        <INPUT TYPE="SUBMIT" value="Roll">
       
        <%
        if(null != request.getParameter("Roll"))
        if(dice.getCount() < 50)
        {
   
  dice.rollDice();
%>
Roll dice till you get 50 points
<%
}else
{
            dice.restart();
           dice.rollDice();
%>
restarting game with zero
<%
}
%>
       
   
  </body>
</html>
0
 

Author Comment

by:annie613
ID: 13828597
ok..you suggest to put the conditional code in the jsp..not in the java...do i need some type of try catch for this??? im assuming i will????
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13828617
since your bean is not throwing any exceptions you do not need a try catch block for this piece of code..
I am suggesting it to put in jsp because today its 50 count where you want to make a restart tomorrow if you decide to make it 100 you do not have to change the bean, just a change in jsp will do the trick for you.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:annie613
ID: 13828670
ok..i will work around with it for a bit..and post back! :)
0
 

Author Comment

by:annie613
ID: 13828753
i see i was missing the {} in the first if...i think something is wrong with my server..i changed some code and it hasnt updated yet..just waiting for this to restart :)
0
 

Author Comment

by:annie613
ID: 13828776
OK..here is my jsp code..but when i click Roll all that changes in the time and date on the page from my include top.jsp file...still working around with it!

!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" >
        this line is a test--dummy1
       
        <%@ page errorPage="Week12Errors.jsp" %>
       
        <jsp:useBean id="dice" class="week12.DiceGame" scope="session" />
       
        <jsp:setProperty name="dice" property="*"/>
       
        <h1 align="center">Dice Game</h1>
       
        <%@ include file="IT608top.jsp" %>
        <br>
        <br>
         <form ACTION= "DiceGame.jsp">
            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><br>
           
            <INPUT TYPE="SUBMIT" value="Roll">
       
            <% if(null != request.getParameter("Roll")){
               if(dice.getScore() < 50){
               dice.rollDice(); %>
               Roll dice until you get 50 points!
            <% } else {
               dice.restart();
               dice.rollDice(); %>
               Click Roll to activate a new game!    
            <% }} %>
           
         </form>  
    </body>
</html>
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13828838
your rolls so far counter shd be increasing..

Rolls So Far: <jsp:getProperty name="dice" property="count" /> <br><br>

if not then we have a problem somewhere
0
 

Author Comment

by:annie613
ID: 13828941
no..when i click the roll all my values stay at zero and only the time changes...
0
 

Author Comment

by:annie613
ID: 13829026
maybe its my java file... but it seems to be working well... is my count in the wrong place..i have it in the rollDice() as count++ is that getting back to getCount????? is there a way to debug in beans..like using out.println's as with other java programs????

package week12;
public class DiceGame
{
        private String dice = "dice";
        private int count = 0;
        private int score = 0;
        private int die1 = 0;
        private int die2 = 0;
   
        public String getDice()
        {
            return dice;
        }//end getDice()
        public void setDice(String d)
        {
            this.dice = d;
        }//end setDice
       
        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 adds to count and sums score
       public void rollDice()
       {
            int die1 = 1 +(int)(Math.random() * 6);
            int die2 = 1 +(int)(Math.random() * 6);
            score = die1 + die2;
            count++;  
       }//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
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13829045
your count looks fine and also you bean is okay too..
I guess since you are doing a
        <jsp:setProperty name="dice" property="*"/>
it will be trying to call setCount() as well which we do not need..
from your bean take out the
setCount Method...

We do need the getCount method so leave that one there
0
 

Author Comment

by:annie613
ID: 13829124
do i need to put a setProperty in my jsp file?????
i took out the setCount...but im still thinking i might need something like...
<jsp: setProperty name="dice" property="die1" value="<%= die1 %>" />
0
 

Author Comment

by:annie613
ID: 13829214
well i dont need the setProperty...im stumped..
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13833308
okay so as of now where are we?
you do not need setProperties because you are not taking any values from the page..., the page is a display only page...
0
 

Author Comment

by:annie613
ID: 13833606
hi again..i was trying to put in System.out.println's in the jsp to see what is going on..however i cant find the folder in tomcat to view the log..so i was at a halt there..

i dont think i changed much from before..if i did..i most likely ended up changing it back

im still at the point..the page loads and when i click roll the date and time changes..all the other values remain zero???

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 "Transitional//EN">

<html>
    <head>
    <title>Dice Game</title>
        <link   rel=stylesheet
                href="class12.css"
                type="text/css">
        </link>
    </head>
   
    <body bgcolor="#0033FF" >
        this line is a test--dummy8
       
        <%@ page errorPage="Week12Errors.jsp" %>
       
        <jsp:useBean id="dice" class="week12.DiceGame" scope="session" />
       
        <jsp:setProperty name="dice" property="*"/>
       
        <h1 align="center">Dice Game</h1>
       
        <%@ include file="IT608top.jsp" %>
        <br>
        <br>
         <form ACTION= "DiceGame.jsp">
            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><br>
           
            <INPUT TYPE="SUBMIT" value="Roll">

            System.out.println(dice.getScore());
       
            <% if(null != request.getParameter("Roll")){
               if(dice.getScore() < 50){
               dice.rollDice(); %>
               Roll dice until you get 50 points!
            <% } else {
               dice.restart();
               dice.rollDice(); %>
               Click Roll to activate a new game!    
            <% }} %>  

         </form>  
    </body>
</html>


public class DiceGame
{
        private String dice = "dice";
        private int count = 0;
        private int score = 0;
        private int die1 = 0;
        private int die2 = 0;
   
        public String getDice()
        {
            return dice;
        }//end getDice()
        public void setDice(String d)
        {
            this.dice = d;
        }//end setDice
       
        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 adds to count and sums score
       public void rollDice()
       {
            int die1 = 1 +(int)(Math.random() * 6);
            int die2 = 1 +(int)(Math.random() * 6);
            score = die1 + die2;
            count++;  
       }//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

0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13833891
ooops !!!
my bad!!!!!


you are doing
a
null != request.getParameter("Roll"))
and on the page there is nothing named as "Roll" as a result its not even executing anything from bean..

change your button to <INPUT TYPE="SUBMIT" name="Roll" value="Roll">
and it shd be working
0
 

Author Comment

by:annie613
ID: 13833923
ok now when i click on roll i see the score and count work..i wonder why my die1 and die2 values are the same..should i comment them out too..i will try! :)
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 1300 total points
ID: 13834100
alright I figured that one out too..

public void rollDice()
       {
            int die1 = 1 +(int)(Math.random() * 6);
            int die2 = 1 +(int)(Math.random() * 6);
            score = die1 + die2;
            count++;  
       }//end rollDice()

you are declaring the die1 and die2 as local variables.. i.e. the global once will always be zeros..!!!

change this to
public void rollDice()
       {
            die1 = 1 +(int)(Math.random() * 6);
            die2 = 1 +(int)(Math.random() * 6);
            score = die1 + die2;
            count++;  
       }//end rollDice()


Also the if condition  if(dice.getScore() < 50){
 is wrong, we dont wanna compare Score, we want to compare count.!!
it shd be
 if(dice.getCount() < 50){

0
 

Author Comment

by:annie613
ID: 13834172
i changed it to getScore() because i want the player to roll until the score is 50 once the score is 50 the player can choose to roll another game...

the count can be as high or low as it needs to be for the player to reach 50

im still not getting the die1 or die2 to display on my jsp page

here is the link if you want to view it

http://matrix.csis.pace.edu:23003/it608/week12/DiceGame.jsp
0
 

Author Comment

by:annie613
ID: 13834224
ok..i see what you mean with the local vars..let me change that and restart the server :)
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13834249
Have you restarted the tomcat after making the changes in bean? , you need to resart the tomcat to pick the bean up..
and secondly, your score will never be 50!!!, in fact it will never go beyond 12..

1 +(int)(Math.random() * 6);
1 +(int)(Math.random() * 6);
will never return a value greater than 6 and your score is total of this.. so how come the score will be 50!!!!
0
 

Author Comment

by:annie613
ID: 13834262
i think its working..i just need to fix the logic so that i add to the previous score..
right now it set the score to die1 + die2

i need it to add to score ... all is well

thanks ! you are awesome cheers
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13834271
I am here to just help...:-)
0
 

Author Comment

by:annie613
ID: 13834288
i think i have it now..im just working on the logic in the java file for the score..but you have helped me understand tons about this :) thanks again cheers!!!!!!!!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help to fix the below errors for MS Exchange Server 2016 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question