%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject,
RockPaperScissors, RockPaperScissorsHome" %>
<%!
RockPaperScissors rockPaperScissors = null;
public void jspInit() {
try {
InitialContext initialContext =
new InitialContext();
Object initialContextObject =
initialContext.lookup("java:comp/env/ejb/TheRockPaperScissors");
RockPaperScissorsHome rockPaperScissorsHome =
(RockPaperScissorsHome)PortableRemoteObject.narrow(
initialContextObject, RockPaperScissorsHome.class);
rockPaperScissors = rockPaperScissorsHome.create();
} catch (java.rmi.RemoteException remoteException) {
System.out.println(
"RemoteException - Couldn't create RockPaperScissors bean." +
remoteException.getMessage());
} catch (javax.ejb.CreateException createException) {
System.out.println(
"CreateException - Couldn't create RockPaperScissors bean." +
createException.getMessage());
} catch (NamingException namingException) {
System.out.println(
"Unable to lookup home TheRockPaperScissors " +
namingException.getMessage());
}
}
%>
Rock-Paper-Scissors
Choose Rock, Paper, or Scissors
<%
String theJspRPS = request.getParameter("rps");
String theBeansRPS = rockPaperScissors.getRockPaperOrScissors();
if (theBeansRPS == null) {theBeansRPS = "no pick from the Bean";}
String whoWins;
if (theJspRPS == null) {whoWins=" ";} //first time no jsp pick
else
{
if (theBeansRPS.equals("Rock") &&
theJspRPS.equals("Rock")) {
whoWins="Tie!";
} else if (theBeansRPS.equals("Rock") &&
theJspRPS.equals("Paper")) {
whoWins="You win!";
} else if (theBeansRPS.equals("Rock") &&
theJspRPS.equals("Scissors")) {
whoWins="The Bean wins!";
} else if (theBeansRPS.equals("Paper") &&
theJspRPS.equals("Rock")) {
whoWins="The Bean wins!";
} else if (theBeansRPS.equals("Paper") &&
theJspRPS.equals("Paper")) {
whoWins="Tie!";
} else if (theBeansRPS.equals("Paper") &&
theJspRPS.equals("Scissors")) {
whoWins="You win!";
} else if (theBeansRPS.equals("Scissors") &&
theJspRPS.equals("Rock")) {
whoWins="You win!";
} else if (theBeansRPS.equals("Scissors") &&
theJspRPS.equals("Paper")) {
whoWins="The Bean wins!";
} else if (theBeansRPS.equals("Scissors") &&
theJspRPS.equals("Scissors")) {
whoWins="Tie!";
} else {whoWins = "Someone didn't choose!";}
whoWins = (whoWins + " The Bean chose " + theBeansRPS +
" while you chose " + theJspRPS + ".");
}
%>
<%= whoWins %>