Vous êtes sur la page 1sur 22

ITT73 COMPONENT TECHNOLOGY

UNIT-IV THE

SUN WAY
Components variety – Applets, Servlets, Java beans, enterprise beans – EJB
architecture – Types of beans – Characteristics – Building and deploying distributed
applications using EJB.

4.1 APPLETS
 Applets are the first Component model introduced by Java.
 A Java applet is a small application written in Java, and delivered to users in the
form of byte code.
 The user launches the Java applet from a web page.
 The Java applet then executes within a Java Virtual Machine (JVM) in a process separate
from the web browser itself, yet it can appear in a frame of the web page, in a new
application window, or in Sun's Applet Viewer, a stand-alone tool for testing applets.
 Java applets are executed in a sandbox by most web browsers, preventing them from
accessing local data like clipboard or file system.
 The code of the applet is downloaded from a web server and the browser either
embeds the applet into a web page or opens a new window showing the applet's user
interface.
 A Java applet extends the class java.applet.Applet. The class must override methods
from the applet class to set up a user interface.
 Since Java’s byte code is platform independent, Java applets can be executed by browsers
for many platforms, including Microsoft windows, UNIX, Mac os and Linux.

4.2 SERVLETS
 The second Java component model is Servlets.
 The Servlet is a Java programming languageclass used to extend the capabilities of a
server.
 A Servlet is an object that receives a request and generates a response based on that
request.
 Servlets are most often used to:
o Process or store data that was submitted from an HTML form
o Provide dynamic content such as the results of a database query
o Manage state information that does not exist in the stateless HTTP protocol
 To deploy and run a Servlet, a web container must be used.
 A web container is essentially the component of a web server that interacts with the
servlets.

Department of Information Technology, SMVEC Page85


ITT73 COMPONENT TECHNOLOGY

 The web container is responsible for managing the lifecycle of servlets, mapping a URL
to a particular servlet and ensuring that the URL requester has the correct access rights.
 Servlets can be generated automatically from Java Server Pages (JSP) by the Java
Server Pages compiler.
 The difference between servlets and JSP is that servlets typically embed HTML inside
Java code, while JSPs embed Java code in HTML.

4.2.1 LIFE CYCLE OF A SERVLET

Three methods are central to the life cycle of a servlet. These are init(), service(), and
destroy(). They are implemented by every servlet and are invoked at specific times by the
server.

 During initialization stage of the servlet life cycle, the web container initializes the
servlet instance by calling the init() method, passing an object implementing the
javax.servlet.ServletConfig interface.
 After initialization, the servlet instance can service client requests The web container
calls the service() method of the servlet for every request.
 The service() method determines the kind of request being made and dispatches it to
an appropriate method to handle the request.
 Finally, the web container calls the destroy() method that takes the servlet out of service.
The destroy() method, like init(), is called only once in the lifecycle of a
servlet.
4.3 JAVABEANS
 The third component model of Java is the Java beans.
 A Java Bean is a reusable software component that can be manipulated visually in a
builder tool.
 The builder tools may include web page builders, GUI layout builders. Sometimes the
“builder tool” may simply be a document editor.
 Some Java Beans may be simple GUI elements such as buttons and sliders. Other
Java Beans may be visual software components such as database viewers, or data feeds.
 Beans are appropriate for software components that can be visually manipulated and
customized to achieve some effect
 They are classes that encapsulate many objects into a single object (the bean). They
are serializable, have a 0-argument constructor, and allow access to properties using
getter and setter methods
 Java beans focuses on “Connection – Oriented programming” between a client and a
server.
 Java beans are very useful in building client-side applications.
 They explicitly support a visual application design mode.

Department of Information Technology, SMVEC Page86


ITT73 COMPONENT TECHNOLOGY

4.3.1 PROPERTIES, EVENTS, AND METHODS

4.3.1.1 Properties
 Properties are named attributes associated with a bean that can be read or written by
calling appropriate methods on the bean.
 Properties show up in a number of ways:
1. Properties may be exposed in scripting environments as though they
were fields of objects.
2. Properties can be accessed programmatically by other components
calling their getter and setter methods
3. Typically a bean’s properties will be persistent, so that their state will be
stored away as part of the persistent state of the bean.

4.3.1.2 Methods

 The Methods a Java Bean exports are just normal Java methods which can be called
from other components or from a scripting environment
 For readable properties there will be a getter method to read the property value. For
writable properties there will be a setter method to allow the property value to be
updated.
 For simple properties the accessor type signatures are:
o void setFoo(PropertyType value); // simple setter
o PropertyTypegetFoo(); // simple getter

GetFoo and setFoo are simply example names.

4.3.1.3 Events

 Events provide a way for one component to notify other components that something
interesting has happened.
 Events are one of the core features of the Java Beans architecture.
 Events provide a convenient mechanism for allowing components to be plugged
together in an application builder.
 Events are a mechanism for propagating state change notifications between a source
object and one or more target listener objects.
Event State Objects
 Information associated with a particular event notification is normally encapsulated in
an “event state” object that is a subclass of java.util.EventObject.

Department of Information Technology, SMVEC Page87


ITT73 COMPONENT TECHNOLOGY

4.4 ENTERPRISE JAVA BEAN


 EJB (Enterprise Java Beans) is the Java’s fourth component model.
 Enterprise JavaBeans (EJB) is a managed, server-side component architecture for
modular construction of enterprise applications.
 Enterprise JavaBeans (EJBs) are a part of the Java 2 Enterprise Edition (J2EE)
specifications.
 EJBs provide developers with a distributed, object-oriented, component-based
architecture.
 EJB focuses on “Container – Integrated Services”.
 EJB is a server-side model that encapsulates the business logic of an application.
 The EJB specification intends to provide a standard way to implement the back-end
'business' code typically found in enterprise applications
 Java Beans also have containers, but EJB containers are different from Java bean
containers.
 Java beans are just a mechanism for containment, whereas EJB beans are driven from
declarative attributes.
 However, Java beans does not require an interactive user during execution, it is
advisable to use Java beans to construct more complex EJB’s.
 An EJB is a collection of Java classes and XML file, combined into a single unit. The
Java classes must follow certain rules and provide certain callback methods.
 They are designed to encapsulate business logic and to protect the application
developer from worrying about the system level issues.

4.4.1 FEATURES OF EJB:

1. EJB components are server-side components written entirely in the Java programming
language.
2. System level services such as Transactions, security, life-cycle, threading, persistence
etc., are automatically managed for the EJB component by the EJB server.
3. EJB architecture is absolutely transactionl, distributed, portable, multi-tier, scalable
and secure.
4. EJB components are fully portable across any server and any os, work with any client.
5. Four major parts of bean are
The home interface.
The remote interface.
The implementation class.
The XML deployment descriptor.

Department of Information Technology, SMVEC Page88


ITT73 COMPONENT TECHNOLOGY

4.4.2 EJB vs JAVA BEANS:

EJB JAVA BEANS


1. EJB provides a format for encapsulation and 1. Java Beans provides a format for
management of business logic. general- purpose components.
2. EJB has tier of execution at business logic of 2. Java Beans has tier of execution at
server. client side.
3. Runtime Environment provides services such as 3. Runtime Environment provides services
Persistence such as
Transactions Java libraries
Security Java application.
Connection
Table 4.1 Difference of EJB and JAVA BEANS

4.4.3 APPLICATION CLIENT COMPONENTS:

 J2EE introduces fifth Java Component model, Application client components.


 These are unconstrained java applications that reside on client.
 A client component uses JNDI enterprise naming context to access EJB’s and other
resources on J2EE servers.

4.4.4 DEPLOYMENT FORMAT:

 J2EE enterprise applications are packaged in .ear (enterprise archive) files.


 This .ear file contains .war (web archive) files and .jar (java archive) files.
 Servlets and JSPs are packaged in .war files.
 Applets, Java beans, EJBs are packaged in .jar files.
 All these files follow the ZIP archive file format.

4.4.5 DEPLOYMENT DESCRIPTOR:

 J2EE deployment descriptor serves for two purposes.


1. They enable the component developer to communicate the requirements.
2. They enable the component deployer to fill in the blanks.
 The role of deployer is district, and usually served by a person different than the
component developer.

4.4.6 JAVA SERVER PAGES AND SERVLETS:

Serving web pages includes 3 core functions.

1. Incoming requests need to be accepted, checked for proper authorization, and routed
to appropriate components which are ready to handle a particular request.
2. Such components need to draw the information sources to retrieve or synthesize the
requested contents.
3. The retrieved contents need to be sent to the requesting party.
These three steps are handled by a web server.

Department of Information Technology, SMVEC Page89


ITT73 COMPONENT TECHNOLOGY

HTTP Request URL URL Component

Client Web C
Machine G Requested
Server
I
Static HTML HTML
Fig.4.1 working of web server

 Incoming requests are received by web server as shown in figure 4.1.


 A simple web server will find a static HTML page in the local file system.
 The web server also uses the URL to activate the requested component through a
simple interface called Common Gateway Interface (CGI).
 The respective component receives the URL and generates a HTML page.
 In both cases, the resulting HTML page is returned to the client via a web browser.

4.4.7 ASP:

 For handling bulk of website requirements, Microsoft introduces ASP (Active Server
Pages).
 ASP allows raw web pages to include server-side script code.
 The ASP server executes such scripts and produces HTML fragments as result.
 ASP then places the HTML fragments on the script and returns the resulting HTML
page to the client.

4.4.8 JSP:

 JSP (Java Server Pages) was improved on the ASP model. JSP are pages containing
HTML pages with inserted java fragments.
 JSP pages are compiled into servlets.
 A JSP server activates servlets to handle the requests.
 At the level of servlets, HTML resides inline in Java source code.
 The following example of servlet produces a HTML result.

importjavax.servlet.*;
importjavax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException,java.io.IOException
{
java.util.calender calendar = new java.util.GregorianCalender();
int hour = calendar.get(currTime.Hour);
int minute = calendar.get(currTime.Minute);
ServletOutputStream out = response.getWriter();
out.print(“<HTML><BODY>\r\n”);
out.print(“The time is:”+hour+”:”+minute+\r\n);
Department of Information Technology, SMVEC Page90
ITT73 COMPONENT TECHNOLOGY

out.print(“</BODY></HTML>\r\n”);
}
}
 The servlet container supports servlets by providing access to HTTP request
parameters and by managing sessions.
 JSP embeds the source code within the markup language. The content of
<%.......%>tags is interpreted as Java source code.
 In case of large pieces of Java code in JSP page, it is preferable to keep the code in
separate java packages and reduce code.

4.4.9 WEB SERVICES:

 JSP servlets can be used to implement simple web services by combining servlets
with libraries.
 JSP server is combined with web service protocols such as SOAP to implement web
service.
 Servlets do not directly produce the output by the server to the client.
 Instead, a request can be handled by one server containing business logic.
 After processing, the servlet can handover output generation to another servlet class.

4.5 EJB ARCHITECTURE

Fig.4.2 Architecture of EJB

4.5.1 EJB SERVERS:

 Composed of server software, the EJB server is analogous to the Object Request
Broker (ORB) from the CORBA world.
 The server provides a way for the client to transact with the EJBs. It also provides
naming services.

Department of Information Technology, SMVEC Page91


ITT73 COMPONENT TECHNOLOGY

4.5.2 EJB CONTAINERS:

 A container is an execution environment for a component.


 The component lives in the container and the container provides the services for the
component.
 Similarly, a container lives in an application server, which provides an execution
environment for it and other containers.
 Containers are located inside the EJB server. The EJB containers provide a means for
invoking an EJB’s methods as shown in figure 4.2.
 An EJB client can send a request to invoke a method on an EJB to the EJB container.
 The EJB container invokes the appropriate method on the EJB.
 Containers provide support for security, object persistence, resource pooling, and
transaction processing.

4.5.3 EJB CLIENTS:

 EJB clients find EJBs through a naming and directory service.


 The client then sends a message to an EJB container, which determines the EJB
method that the client wants to invoke.
 EJB clients are applications that access EJB components in EJB containers.
 Two possible types of clients are
 Stand-alone applications accessing the EJB components using RMI-
110p protocol.
 Java servlets and JSPs that access EJB components via RMI-110p
protocol.

4.5.4 ENTERPRISE JAVABEANS:

 Enterprise JavaBeans can be session beans, entity beans, or message-driven beans.


 EJBs reside within EJB containers.

4.5.5 DIRECTORY SERVICE:

 The directory/naming service provides the ability to locate any object registered with
the directory/naming service.
 It provides a mapping between a given name of an entity and, in the case of EJBs, a
stub that corresponds to the home interface of an Enterprise Java Bean.

4.5.6 COMPONENTS OF EJB

The four main components of an EJB are

 The home interface


 The remote interface
 The bean class and
 The deployment descriptor.

Department of Information Technology, SMVEC Page92


ITT73 COMPONENT TECHNOLOGY

4.5.6.1 The home interface

 The home interface declares the methods for creating, finding, and destroying
instances of EJBs.
 The methods declared in the home interface must correspond to certain methods
implemented in the bean class.

4.5.6.2 The remote interface

 The remote interface declares the methods that contain the EJB's business logic.
 The methods declared in the remote interface must be implemented in the bean class.

4.5.6.3 The bean class

 The bean class provides the logic of the bean.


 It must implement the methods declared in the remote interface and must implement
methods corresponding to the methods in the home interface.

4.5.6.4 The deployment descriptor

 The deployment descriptor is always called ejb-jar.xml.


 The deployment descriptor describes EJB(s) to the EJB container.

4.5.7 NAMING THE COMPONENTS OF AN EJB

If the EJB is named Demo, then the remote interface should be Demo.java, the home
interface should be DemoHome.java, and the bean class should be either DemoBean.java or
DemoEJB.java. The deployment descriptor should always be ejb-jar.xml.

Fig.4.3 Components of an EJB

Department of Information Technology, SMVEC Page93


ITT73 COMPONENT TECHNOLOGY

4.5.7.1 How EJBs Work?

How the client interacts with an EJB


 When the EJB is deployed by the container, the container registers the given name of
the home interface to the naming directory.
 When a client requests the given name of the home interface, the naming directory
returns a stub for the home interface.
 The home stub does not actually interact with the home interface at all. Instead, the
home stub interacts with the container's implementation of the home interface.
 The client uses the home stub to call create, remove, and/or find methods declared by
the home interface.
 When create or find method is called successfully, the container will create an
instance of the bean class and the home implementation will return a remote stub, which
corresponds to an implementation of the remote interface.
 The client can then use the remote stub to make calls to the EJB as shown in figure
4.3.

What are stubs?

 A stub is a part of Remote Method Invocation (RMI) technology.

 In figure 4.4, a client can use a stub to remotely invoke exposed methods of an object
that exists in a location separate from the client.

 The stub will receive the return value from the method invoked from the remote
object and pass the value to the client, if a value is returned.

Fig.4.4 working of Stubs

Department of Information Technology, SMVEC Page94


ITT73 COMPONENT TECHNOLOGY

4.6 TYPES OF ENTERPRISE JAVA BEANS

Java beans are of three types

1. Session beans
2. Entity bean
3. Message-driven bean or Message beans

4.6.1 SESSION BEANS

Session beans are used to represent non-persistent business logic. They almost always
have an association with a particular client. If the container is shutdown, the session beans
running are lost. Session beans can access and modify persistent data, though entity beans are
usually more suitable for working with persistent data.

It is of two types

1. Stateless bean
2. Stateful bean

Stateless bean

1. A stateless session bean does not maintain state across multiple invocations.
2. The container decides to keep the instance in a pool instead of creating new ones for
every invocation in every session.
3. A stateless session bean does not maintain a conversational state with the client.
4. When a client invokes the methods of a stateless bean, the bean’s instance variables
may contain a state specific to that client, but only for the duration of the invocation.
5. When the method is finished, the client-specific state should not be retained.
6. Clients may, however, change the state of instance variables in pooled stateless beans,
and this state is held over to the next invocation of the pooled stateless bean.
7. Except during method invocation, all instances of a stateless bean are equivalent,
allowing the EJB container to assign an instance to any client. The state of a stateless
session bean should apply across all clients.

A stateless session bean can implement a web service, but other types of enterprise beans
cannot.

Stateful session bean:

1. A stateful session bean remains associated with one session for the lifetime.
2. The state of an object consists of the values of its instance variables.
3. The bean can retain state across method invocations
4. In a stateful session bean, the instance variables represent the state of a unique client-
bean session.

Department of Information Technology, SMVEC Page95


ITT73 COMPONENT TECHNOLOGY

5. Because the client interacts (“talks”) with its bean, this state is often called
theconversational state.
6. The state is retained for the duration of the client-bean session.
7. If the client removes the bean or terminates, the session ends and the state disappears.

The deployment descriptor of a session bean,

<session>
<ejb name>….name of the session bean….</ejb name>
<home>….name of the EJB home interface..</home>
<remote>…name of object interface…</remote>
<local-home>…name of local EJB home interface…</local-home>
<local>…name of local EJB object interface…</local>
<ejb-class>…name of class implementing this Ejb…</ejb-class>
<session-type>…Stateless or Stateful…</session-type>
<transaction-type>..container or bean…</transaction-type>
<ejb-ref>..import of other beans referenced by this bean….</ejb-ref>
<security-identity>…use caller-identity…</security-identity>
</session>
 The descriptor assigns name to the bean, home interface and object interface.
 The EJB class is the main class implementing the bean.
 The session-type determines whether it is a stateful or stateless bean, therefore
whether the container will create new instance per call or not.
 Session bean undergoes two types of transaction
 Bean-managed transaction(optional)
 Container-managed transaction(default)
 The EJB reference lists out names of other beans and their local and remote EJB
home and remote interfaces.
 Finally, the security identity field allows a bean to run under caller’s identity or under
some specified roles.

4.6.2 ENTITY BEANS:

 An entity bean uses database objects to encapsulate access to actual database records.
 Entity Beans always have states. Each Entity Bean may however be shared by
multiple EJB Clients. Their states can be persisted and stored across multiple
invocations. Hence they can survive System Shutdowns.
 An "Entity Bean" is a type of Enterprise JavaBean, a server-side J2EEcomponent, that
represents persistent data maintained in a database.
 An entity bean corresponds to a single row in a relational table or it corresponds to a
row in the result of a Join operation.
 The mapping of entity beans to and from database rows is called persistence.
 Entity beans can manage their own persistence by directly using JDBC to operate
over databases.

Department of Information Technology, SMVEC Page96


ITT73 COMPONENT TECHNOLOGY

 Alternatively, they can use container-manages persistence defined during the


deployment process.
 Entity in a database schema is related to each other. Thus, there is a need to find entity
bean instances by navigating entity relationships.

4.6.3 ENTITY BEAN CREATION:

 An entity bean is either newly created or located by its primary key.


 A new entity bean is created and associated with a new primary key.
 If a bean already exists, the container checks whether or not the corresponding beans
instance is already available. If so, it is returned.
 If not, the container creates a new instance and associates a primary key with it.

4.6.4 DEPLOYMENT DESCRIPTOR:

<entity>
<ejb name>…name of entity bean….</ejb name>
<home>…name of ejb home interface..</home>
<remote>…name of ejb object interface…</remote>
<local-home>…name of local ejb home interface</local-home>
<local>…name of ejb object interface…</local>
<ejb-class>…name of class implementing this ejb…</ejb-class>
<persistence-type>…container or bean…</persistence-type>
<primary-key class>….</primary-key class>
<reentrant>true or false</reentrant>
<abstract schema name>…..</abstract schema name>
<prim-key-field>…field name…</prim-key-field>
….
</entity>
 The initial fields are same as session bean descriptors.
 The re-entrant field can be used to enable re-entnrant calls, by default; this is prohibited
and causes the container to throw an exception.

4.6.5 DATABASE MAPPING:


 EJB 2.0 supports one-to-one, one-to-many, many-to-many relationships both in
unidirectional and bi-directional versions.
 EJB 2.0 introduced a query language similar to SQL called EJBQL( EJB Query
Language).
 This language is used in deployment descriptors to specify additional find methods.
 Though EJBQL provides methods to find an entity by name, it still has a number of
shortcomings.

Disadvantages:
 It is not possible to select rows in sorted order.
 It is not possible to properly handle dates.

Department of Information Technology, SMVEC Page97


ITT73 COMPONENT TECHNOLOGY

 The lack of support for sorted order is a significant drawback for high performance
applications.

4.6.6 MESSAGE-DRIVEN BEANS:

 Message-driven bean is an enterprise bean that allows Java EE applications to process


messages asynchronously.
 It normally acts as a JMS message listener, which is similar to an event listener except
that it receives JMS messages instead of events.
 The messages can be sent by any Java EE component (an application client, another
enterprise bean, or a web component) or by a JMS application or system that does not use
Java EE technology.
 They are stateless and therefore non-persistent.
 Like session and entity beans, md-beans reside inside a EJB container.
 MD beans have no container-managed persistent state.
 They also don’t have remote or local or home interfaces.
 The only way to instantiate and use an MD bean is to register it for a particular message
queue. (Java message service).

4.6.7 JMS:
 Java message service.
 JMS is a Java access mechanism to messaging systems.
 It implements 2 types of messaging
 point-to-point
 Broadcast.
 Point-to-point message queues supports delivery of messages between client and queue.
 In broadcast, messages published to a particular topic are delivered to all subscribers of
that topic.

4.6.8 WORKING OF MD-BEAN:


 When the container schedules a message in a queue or topic for processing, it creates a
new instance of any bound md-bean and then calls ejbcreate method.
 The container calls setMessageDrivenContext to associate the new md-bean with its
context.
 The actual message handling is requested by calling onMessage method.
 The container is free to recycle an md-bean instance to handle multiple messages.
 The container cleans up by calling the ejbRemove method. This allows md-bean to
release the resources.

Advantages:
It is possible to write all application logic using md-beans.
Well-suited for workflow-oriented automatic distribution of work-items.

Department of Information Technology, SMVEC Page98


ITT73 COMPONENT TECHNOLOGY

Disadvantages:
Message-queue oriented designed is fully asynchronous and thus not suitable for
serving interactive sessions.

4.7 BUILDING DISTRIBUTED APPLICATIONS

Distributed Application
A distributed application is software that is executed or run on multiple computers
within a network. These applications interact in order to achieve a specific goal or task.
Traditional applications relied on a single system to run them. Even in the client- server
model, the application software had to run on either the client, or the server that the client was
accessing. However, distributed applications run on both simultaneously.
With distributed applications, if a node that is running a particular application goes
down, another node can resume the task.
Building and Deploying an Application using EJB

The following are the steps involved in creating and deploying an application using EJB.
1. Create a Remote interface of Bean.
2. Create Bean’s home interface.
3. Create Bean’s implementation class.
4. Compile home interface, remote interface & implementation class.
5. Create a session descriptor.
6. Create a manifest.
7. Create aejb-jar file.
8. Deploy the ejb-jar file.
9. Write a client.
10. Run the client.

Steps

1. Create a remote interface for the Bean:

 The sequence is described in figure 4.5.


 A remote interface is the interface through which the client interacts.
 The EJB container uses the remote interface for your bean to generate both client-side
stub and a server side proxy object that passes client calls to EJB object.
 In EJB, the client is never allowed to obtain a direct reference to EJB implementation.
 Access to the implementation is always brokered through the container.
 The following is the code for Remote interface.
importjavax.ejb.;
importjava.rmi;
public interface Hello extends EJBObject
{

Department of Information Technology, SMVEC Page99


ITT73 COMPONENT TECHNOLOGY

String sayHello(String s) throws RemoteException;


}

2. Create the Bean’s Home interface:

 The home interface is the client’s initial point of contact with EJB components.
 The client obtains a reference to an object implementing the home interface via JNDI.
 After the client has obtained this reference, it can use a create() method to obtain a
reference to an object that implements the remote interface.
packagemyprogram.Helloejb;
importjavax.ejb;
publicinterfaceHelloHome extends EJBHome
{
public Hello create() throws java.rmi.RemoteException, javax.ejb.CreateException;
}
 The create() method is similar to constructors, that initializes an object, thereby
allowing the container implementers to avoid overhead of instantiating a new object
each time a client requests a reference.

3. Create the Bean’s implementation class:

packagemyprogram.HElloejb;
importjavax.ejb;
public class helloBean implements SessionBean
{ SessionContextctx;
public void ejbCreate()
{
}
public String sayHello(String s)
{
return “Hello there,”+s;
}
public void ejbRemove()
{
}
public void ejbpassivate()
{
}
public void ejbactivate()
{
}
public void setSessionContext(sessionContextctx)

Department of Information Technology, SMVEC Page100


ITT73 COMPONENT TECHNOLOGY

{
this.ctx=ctx;
}

 HelloBean implements the session bean interface, because we are using a session
bean.
 A class variable is declared to hold the reference to sessionContext variable. The
Bean uses the session context to interact with the container.
 ejbRemove() method is called by the container to notify the EJB instance that it
will be removed soon.
 ejbpassivate() and ajbactivate() are called by the container to swap put ejb
instances to temporary storage and vice-versa.

4. Compile the Remote interface, home interface, and implementation class:


 Compile the classes, javac *.java
 If classNotFound Exception is got,checkclasspath to ensure that it includes
javax.ejbpackage.

5. Create a session descriptor:


 The sessionDescriptor class passes information about the bean to the deployment
environment.
 At development time, the bean developer creates an instance of the sessionDescriptor
class, fills it with information and then serializes it.
 At deployment time, the deployerdeserializes this instance and uses the information to
deploy the bean.

6. Create a Manifest:
 A Manifest file is a text document that contains information about the contents of a jar
file.
 For an ejb.jar file a pair of lines must be added.
o Name: EJB/sample/myprogram/Helloejb.ser
o Enterprise-Bean : True.

7. Create an ejb-jar file:


 Use the jar program that comes with the java jdk to generate ejb-jar file.
 The end result is an ejb-jar file suitable for deployment.

8. Deploy ejb-jar file:


 Create additional classes such as implementation classes for the home and remote
interface.
 Move EJB classes to appropriate directory in the server’s classpath.
 After running ejbc on deployment descriptor, one final step must be undertaken to
deploy the bean in the server.
 Set the property server.ejb.deploy to point to your deployed jar file.

Department of Information Technology, SMVEC Page101


ITT73 COMPONENT TECHNOLOGY

 The server loads your EJB and make it ready for use.

9. Write a client:
importmyprogram.Helloejb.*;
importjava.rmi.*;
importjavx.rmi.naming.*;
public class Helloclient
{public static void main(String[] args)
{try{
InitialContextic=new InitialContext();
HelloHome home=(Hellohome)ic.lookup(“HelloHome”);
Hello hel=home.create();
String retval=hel.sayHello(“Tom”);
System.out.println(“returned:”+retval);
hel.remove();
}
catch(java.rmi.RemoteException e)
{
System.out.println(“remote exception occurred:”+e);
}catch(java.ejb.CreateException e){
System.out.println(“Create Exception occurred:”+e);
}}}
 The client uses JNDI classes to lookup a reference to EJB object’s home interface.
 The initial context class serves as an client’s interface to JNDI.
 Create() of method is invoked on the home interface to obtain reference to an object
that implements bean’s remote interface.
 Following the logic are a number of catch blocks to handle any exceptions that might
be thrown in the process of looking up, creating, removing the remote bean.

10. Run the client:


 Client invocation:
 java –Djava.naming.factory.initial=server.jndi
 T3InitialContextFactory
 Helloclient.
 Client should run and print.
 returned : Hello There, Tom
 Client must be able to find the following classes in its classpath.
 Bean’s home interface
 Bean’s Remote interface
 javax.naming.InitialContext class
 Stub classes generated by deployment process.

Department of Information Technology, SMVEC Page102


ITT73 COMPONENT TECHNOLOGY

Client Stub Skeleton EJB Object Enterprise Bean

Say Hello()
Say Hello()

Say Hello()

Say Hello()

Return

Return

Return

Return

Fig.4.5 sequence of EJB working

TWO MARKS
1) Define Java Bean.
A Java Bean is a reusable software component that can be manipulated visually in a
builder tool.
The builder tools may include web page builders, GUI layout builders. Sometimes the
“builder tool” may simply be a document editor.

2) What are the aspects of Java Bean?


Events
Properties
Introspection
Customization
Persistence

3) Give any 4 advantages of java bean.


A bean contains all the benefits of java “Write once, Run anywhere” paradigm.

Department of Information Technology, SMVEC Page103


ITT73 COMPONENT TECHNOLOGY

The configuration settings of a bean can be saved in persistence storage and restored
at later time.
A bean may register to receive events from other objects.
Auxiliary software provided to help a person configure a bean.

4) What are the different properties that a bean can define?


Simple
Indexed
Bound
Constrained

5) What is introspection and reflection?


Introspection is used to find out events methods, properties, standard interface and
classes.Reflection is a type of event listener. It is used to analyze software components or
software.

6) Define Applets.
A Java applet is a small application written in Java, and delivered to users in the form of
bytecode. The user launches the Java applet from a web page. The Java applet then executes
within a Java Virtual Machine (JVM) in a process separate from the web browser
itself, yet it can appear in a frame of the web page, in a new application window, or in Sun's
AppletViewer, a stand-alone tool for testing applets.

7) Define Servlets.
The Servlet is a Java programming languageclass used to extend the capabilities of a
server.AServlet is an object that receives a request and generates a response based on that
request.

8) What are Events?


Events provide a way for one component to notify other components that something
interesting has happened. Events are one of the core features of the Java Beans architecture.

9) What is EJB?
Enterprise Java Beans (EJB) technology is the server-side component architecture for the
Java 2 Platform, Enterprise Edition (J2EE) platform. Enterprise JavaBeans (EJB) is a
managed, server-side component architecture for modular construction of enterprise
applications.Enterprise JavaBeans (EJBs) are a part of the Java 2 Enterprise Edition
(J2EE) specifications. EJBs provide developers with a distributed, object-oriented,
component-based architecture.

Department of Information Technology, SMVEC Page104


ITT73 COMPONENT TECHNOLOGY

10) Differentiate between java bean and EJB.


JavaBeans EJB

JavaBeans may be visible or nonvisible at An EJB is a nonvisual ,remote object.


runtime.

javaBeans are intended to be local to a EJB's are remotely executable components


single process and are primarly intended to or business objects that can be deployed
run on the client side. only on the server.

javaBeans is a component technology to EJB is a component technology,it neither


create generic Java components that can be builds upon nor extends the original
composed together into applets and JavaBean specification
applications.

JavaBeans are not typed EJBs are of three types - session beans and
entity beans and Message beans.

No explicit support exists for transactions EJB's may be transactional and the EJB
in javaBeans. servers provide transactional support

11) Specify the varieties of bean.


Session Bean
 Stateful Bean
 Stateless Bean
Entity Bean
Message Driven Bean

12) Write about session bean?


Session beans are used to represent non-persistent business logic. They almost always have
an association with a particular client.

13) Write about state ful session bean?


A stateful session bean remains associated with one session for the lifetime.The state of
an object consists of the values of its instance variables. The bean can retain state across
method invocations.The state is retained for the duration of the client-bean session.

14) Write about stateless session bean?


A stateless session bean does not maintain a conversational state for a particular client. When
a client invokes the method of a stateless bean, the bean's instance variables may contain a
state, but only for the duration of the invocation. When the method is finished, the state is no
longer retained. Except during method invocation, all instances of a stateless bean are
equivalent, allowing the EJB container to assign an instance to any client.

Department of Information Technology, SMVEC Page105


ITT73 COMPONENT TECHNOLOGY

15) Write syntax to create jar and manifest file.


For jar file:

Jar cf XYZ.jar *.class *.gif

For manifest file:

Jar cfm XYZ.jar YXZ.mf *.class *.gif.

Department of Information Technology, SMVEC Page106

Vous aimerez peut-être aussi