Sun Java System Application Server |
This simple application demonstrates the new Annotation facility introduced in Java EE 5. The application is a lottery simulation in which the user selects the Lottery to play and the application generates a "quick pick" number.
This sample consist of:
index.html
.
PlayLotteryServlet
.
Dice
and Date
.
Lottery
.
LotteryView.jsp
The main HTML page, index.html
is an entry point to the application.
It prompts for user input and then invokes the PlayLotteryServlet
servlet.
The servlet PlayLotteryServlet
generates the required data (lottery number, lottery date), sets date values in the request object, and dispatches the JSP, LotteryView.jsp
for output. PlayLotteryServlet
obtains the lottery date using the stateless session bean, Date
. It uses the stateful session bean, Lottery
, to
form the lottery number from the individual digits obtained from the stateless session bean, Dice
.
This EJB has a remote business interface with a single business method:
import javax.ejb.Remote; @Remote public interface Dice { public int play(); }
Unlike prior versions of EJB, the remote interface is not
required to extend java.rmi.Remote
and its business methods are not
required to throw java.rmi.RemoteException
.
The business interface is designated as a remote business interface via
the @javax.ejb.Remote
annotation.
The implementation of the Dice bean is:
import java.util.Random; import javax.ejb.Stateless; @Stateless public class DiceBean implements Dice { public int play() { Random random = new Random(); return random.nextInt(10); } }
In EJB 3.0, a stateless session bean is just a plain Java
object with a class-level annotation @Stateless
.
It does not have a home interface, as in previous versions of EJB.
No deployment descriptor entries are needed to define the Bean.
The JNDI name for the bean will default to the class name of its business interface.
This is a very simple EJB 3.0 stateless session bean with a local business interface.
The stateless session bean has a local business interface with a single business method.
public interface Date { public String today(); }
The bean implementation is:
import java.util.Calendar; import javax.ejb.Stateless; @Stateless public class DateBean implements Date { public String today() { StringBuffer date = new StringBuffer(); Calendar calendar = Calendar.getInstance(); date.append(calendar.get(Calendar.MONTH)+1); date.append("/"); date.append(calendar.get(Calendar.DAY_OF_MONTH)); date.append("/"); date.append(calendar.get(Calendar.YEAR)); return date.toString(); } }
This is a very simple EJB 3.0 stateful session bean with a remote business interface.
The stateful session bean has a remote business interface with multiple business methods.
import javax.ejb.Remote; @Remote public interface Lottery { public String getName(); public String getNumber(); public String getDate(); public void select(int number); public void setName(String name); }
Unlike prior versions of EJB, the remote interface is not
required to extend java.rmi.Remote
and its business methods are not
required to throw
java.rmi.RemoteException
.
The business interface is designated as a remote business interface via
the @javax.ejb.Remote
annotation.
The bean implementation is:
import javax.ejb.Stateful; import javax.ejb.EJB; @Stateful public class LotteryBean implements Lottery { public String getName() { return name; } public String getNumber() { return number; } public String getDate() { return date.today(); } public void select(int number) { if( (number > -1) && (number < 10) ) { this.number = this.number + SPACE + java.lang.Integer.toString(number); } } public void setName(String name) { this.name = name; } //Dependency injection to get an instance of the Date EJB. @EJB(name="Date") private Date date; private String name = "Super Lotto"; private String number = ""; private static final String SPACE = " "; }
In EJB 3.0, a stateful session bean is just a plain Java object with a class-level annotation
@Stateful
. It does not have a home interface. No deployment dscriptor entries are needed to define the Bean. The JNDI name for the bean will default to the class
name of its business interface. @EJB
is used to get the
local bean Date
, eliminating the error-prone string-based JNDI-lookup.
This is a very simple Servlet that access EJB using JAVA EE 5 servlet 2.5 feature. Only the relevant code snippet is depicted below.
public class PlayLotteryServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", Locale.getDefault()); try { InitialContext initContext = new InitialContext(); lottery = (Lottery) initContext.lookup("enterprise.lottery_annotation_ejb_stateful.Lottery"); dice = (Dice) initContext.lookup("enterprise.lottery_annotation_ejb_stateless.Dice"); } ... } }
The bean's business interface class name is used for its lookup. There is no need to assign explicit JNDI name for the bean.
Following are the instructions for building, deploying, and running this sample application.
app_dir
is the sample application base
directory: samples_install_dir/javaee5/enterprise/lottery-annotation-ear
app_dir.
all
app_dir> ant all
app_dir> ant
default
compiles and packages the application
app_dir> ant
deploy
deploys it to application server
app_dir> ant
run
runs the application. It launches the browser with the
correct url.
run-client
.
app_dir>
ant run-client
run-client
target invokes the java client code
under app_dir/
lottery-annotation-appclient
directory.clean
to remove the temporary
directories like build
and dist
.app_dir>
ant
clean
Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.
samples_install_dir/javaee5/enterprise/lottery-annotation-ear
as the project.lottery-annotation-ear
and select Run Project
which will build, deploy and run the project. If configured properly, the IDE will open the browser with appropriate url. Sample output is given below. Browsing: http://localhost:8080/lottery-annotation-ear/ run-display-browser: pre-init: init-private: init-userdir: init-user: init-project: do-init: post-init: init-check: init: run-ac: BUILD SUCCESSFUL (total time: 11 seconds)Browser output
http://localhost:8080/lottery-annotation-ear/ Simple Annotation Application Super Lotto Quick Pick When you click Quick Pick, your lottery number will be generated.
If you have problems when running the application, refer to troubleshooting document.
Copyright © 2006 Sun Microsystems, Inc. All rights reserved.