Introducing session beans
Session beans encapsulate business logic for enterprise applications. It is a good
idea to use session beans when developing enterprise applications, since we (as
application developers) can focus on developing business logic without worrying
about other enterprise application requirements such as scalability, security,
transactions, and so on.
Even though we don't directly implement common enterprise application
requirements such as transactions and security, we can configure these
services via annotations.
There are three types of session beans: stateless session beans , stateful session
beans , and singleton session beans . Stateful session beans maintain conversational
state with their client between method invocations, where stateless session beans
do not. There exists only one instance of a singleton session bean in an application,
whereas several instances are created by the application server for stateless and
stateful session beans.
Creating a session bean in NetBeans
Session beans can be created in three types of NetBeans projects: Enterprise
Application , EJB Module , and Web Application . EJB Module projects can contain
only EJBs, whereas Enterprise Application projects can contain EJBs along with their
clients, which can be web applications or "standalone" Java applications. The ability
to add EJBs to web applications was introduced in Java EE 6. Having this ability
allows us to simplify packaging and deployment of web applications using EJBs. We
can now package the web application code and the EJB code in a single Web Archive
( WAR ) file, whereas with previous versions of Java EE and J2EE, we had to create
an Enterprise Archive ( EAR ) file.
When deploying enterprise applications to the GlassFish application server included
with NetBeans, it is possible to deploy standalone clients as part of the application to
the application server. These standalone clients are then available via Java Web Start
using annotations. "True" standalone clients executing outside the application server
require Java Naming and Directory Interface ( JNDI ) lookups to obtain a reference
to the EJB. In our first example, we will create both a session bean and a Java Web
Start client both deployed to the same enterprise application.