Tạo java JSP với Bean


  • Tạo 1 trang JSP :

<HTML>

<HEAD>
<TITLE>LotteryGuesser.JSP</TITLE>
</HEAD>

<BODY>
<jsp:useBean id=”guessingBean”

scope=”session”/>

<jsp:setProperty name=”guessingBean”
property=”highNumberString”
param=”highNumberString” />
<jsp:setProperty name=”guessingBean”
property=”numberOfNumbersString”
param=”numberOfNumbersString” />
<jsp:setProperty name=”guessingBean”
property=”usingBonusBall”
param=”usingBonusBall” />

<h4>
Using a Highest Number of:
<jsp:getProperty name=”guessingBean” property=”highNumber” />
</b>
</h4>

<h4>
Using Number of Numbers:
<jsp:getProperty name=”guessingBean” property=”numberOfNumbers” />
</h4>

<h4>
Using a Bonus Ball?
<jsp:getProperty name=”guessingBean” property=”usingBonusBall” />
</h4>

<h3>
Your Guess is:
<jsp:getProperty name=”guessingBean” property=”guess” />
</h3>

<h2><A href=LotteryGuesser.jsp>Guess Again</A></h2>

<br>
<h4>Change the highest number to guess</h4>
<h5>(This number must be greater than zero)</H5>
<FORM action='<%= request.getRequestURI() %>’ METHOD=post>
<INPUT type=text NAME=”highNumberString” size=’3′>
<INPUT type=SUBMIT VALUE=”Change Highest Number”>
<br>
<b>
<jsp:getProperty name=”guessingBean” property=”highNumberMessage” />
</b>
<br>

<br>
<h4>Change the number of numbers to guess</h4>
<h5>(This number must be greater than zero,
and less than your highest number to guess)</H5>
<FORM action='<%= request.getRequestURI() %>’ METHOD=post>
<INPUT type=text NAME=”numberOfNumbersString” size=’3′>
<INPUT type=SUBMIT VALUE=”Change Number of Numbers”>
<br>
<b><jsp:getProperty name=”guessingBean”
property=”numberOfNumbersMessage” /></b>
<br>

<br>
<h4>Use a bonus ball?</h4>
<h5>(check “use a bonus ball” or “do not use a bonus ball”
and press “change if Using a Bonus Ball”)</H5>
<FORM action='<%= request.getRequestURI() %>’ METHOD=post>
<INPUT type=radio NAME=”usingBonusBall” Value=”true”>
use a bonus ball
<INPUT type=radio NAME=”usingBonusBall” Value=”false”>
do not use a bonus ball
<INPUT type=SUBMIT VALUE=”Change if Using a Bonus Ball”>
<br>

</BODY>
</HTML>

  • Tạo 1 Bean :

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(guessesIterator.next());
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();
}
}

* Ghi chú :

– Nếu bạn muốn tạo ra 1 Bean bạn cần làm như sau:

+ Bean phải được thực thi (implement) giao diện (serializable interface).

+ Bean cần phải được đưa vào 1 tập tin jar với “Java-Bean: true” trong file manifest.

Gửi phản hồi

Please log in using one of these methods to post your comment:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s