Java .

Java JSP With A Bean


To download source right-click here and "Save As...".

package beans;

import java.util.Iterator; import java.util.TreeSet;

public class LotteryGuessingBean { private int highNumber = 40; private String highNumberString; private String highNumberMessage;

private int numberOfNumbers = 6; private String numberOfNumbersString; private String numberOfNumbersMessage;

private boolean usingBonusBall = true;

//highNumber get, set, and posse public int getHighNumber() {return highNumber;} public void setHighNumber(int highNumberIn) { if (highNumberIn > 0) { if (highNumberIn > this.numberOfNumbers) { this.highNumber = highNumberIn; this.setHighNumberMessage(null); } else { this.setHighNumberMessage( "High Number must greater than Number of Numbers"); } } else { this.setHighNumberMessage( "High Number must greater than zero"); } } //you can live on the edge and pass the highNumber int // into this bean with the setHighNumber(int) method // from your JSP, or you can use this nice safe method // which takes in a string and makes certain that it's // really ready to be assigned to an int, and then // passes it to setHighNumber(int). public void setHighNumberString(String highNumberStringIn) { int numToSet = 0; try { numToSet = Integer.parseInt(highNumberStringIn); } catch(NumberFormatException nfe) { this.setHighNumberMessage("High Number must be numeric"); return; } this.setHighNumber(numToSet); } public String getHighNumberMessage() { return highNumberMessage; } public void setHighNumberMessage(String messageIn) { highNumberMessage = messageIn; }

//numberOfNumbers get, set, and posse public int getNumberOfNumbers() {return numberOfNumbers;} public void setNumberOfNumbers(int numberOfNumbersIn) { if (numberOfNumbersIn > 0) { if (numberOfNumbersIn < this.highNumber) { this.numberOfNumbers = numberOfNumbersIn; this.setNumberOfNumbersMessage(null); } else { this.setNumberOfNumbersMessage( "Number of Numbers must be less than High Number"); } } else { this.setNumberOfNumbersMessage( "Number of Numbers must greater than zero"); } } //same idea as setHighNumberString public void setNumberOfNumbersString(String numbersOfNumbersStringIn) { int numToSet = 0; try { numToSet = Integer.parseInt(numbersOfNumbersStringIn); } catch(NumberFormatException nfe) { this.setNumberOfNumbersMessage( "Number of Numbers must be numeric"); return; } this.setNumberOfNumbers(numToSet); } public String getNumberOfNumbersMessage() { return numberOfNumbersMessage; } public void setNumberOfNumbersMessage(String messageIn) { numberOfNumbersMessage = messageIn; }

public boolean getUsingBonusBall() {return usingBonusBall;} public void setUsingBonusBall(boolean usingBonusBallIn) { usingBonusBall = usingBonusBallIn; } public String getGuess() { StringBuffer guessesOut = new StringBuffer(); TreeSet guesses = new TreeSet(); while (guesses.size() < numberOfNumbers) { guesses.add( new Integer((int)((Math.random() * highNumber) + 1))); } Iterator guessesIterator = guesses.iterator(); while (guessesIterator.hasNext()) { guessesOut.append(; if (guessesIterator.hasNext()) { guessesOut.append(", "); } } if (usingBonusBall) { //using format where bonus ball can't // be previously picked number boolean done = false; while (!done) { Integer bonus = new Integer((int)((Math.random() * highNumber) + 1)); if (!guesses.contains(bonus)) { guessesOut.append(" Bonus Ball: "); guessesOut.append(bonus); done = true; } } } return guessesOut.toString(); } }
To download source right-click here and "Save As...".


I put all of the input validation into the bean instead of using JavaScript to validate the input. If validation is in the bean then other JSPs calling the bean have their data validation all set.

If you really want to be picky about creating a true bean you do also need to do the following:

1. The bean must implement the serializable interface.

2. The bean must be put into a jar file with "Java-Bean: true" in the manifest file.


The Java 2 Platform Specification
Core JSP by Damon Hougland and Aaron Tavistock

Core Servlets and JavaServer Pages by Marty Hall
Sign in to be the first to comment on Java JSP With A Bean.