Lab2: Session Beans

draindecorumSoftware and s/w Development

Aug 15, 2012 (5 years and 29 days ago)

290 views

Lab2: Session Beans
Geoff Sharman, Jianing Wang
October 2009
Aim:
To develop a session bean that can perform some business functions and can be invoked by the
JSP page from Lab 1.
Introduction:
Enterprise JavaBeans (EJB) technology is the
server-side
component architecture for Java Platform,
Enterprise Edition (Java EE). One goal of the original EJB specification was to expand upon Java's
write-once, run-anywhere philosophy: developers could build applications from server-side
components and focus on business logic and other features without worrying about where the
components would be deployed.
Simply speaking, there are three types of beans can be hold by a bean container, termed

Session Beans
that can be either "Stateful" or "Stateless";

Entity Beans
that can be either "CMP" (Container managed persistence) or "BMP" (Bean
managed persistence;

Message Driven Beans
(also known as MDBs or Message Beans).
For the middleware module, we only learn to use Session Beans and Entity Beans, and Entity Beans
will be introduced in Lab 3.
A Session Bean is created by a client and usually exists only for the duration of a single client-server
session. A Session Bean performs operations, such as calculations or database access, for the
client. Session bean objects either can be
stateless
or
stateful
, where the conversational state across
methods and transactions
can
or
cannot
be maintained
respectively
. If a Session Bean maintains
state (a stateful bean), then the EJB container manages this state if the object must be removed from
memory.
A Session Bean has to be associated with either one or both of two types of interfaces, termed the
local
interface and the
remote
interface. The local interface indicates business methods that can be
invoked by other beans
within the same process
in the same EJB container. The remote interface
indicates business methods that can be invoked by other applications
inside
/
outside
the EJB
container. Such applications could be a java program, a JSP page, or other session beans running
on other EJB containers.
Stateless/stateful Session Beans with local/remote interfaces can be defined in the NetBeans IDE
very easily and the instruction will be listed later.
1
Instruction:
1.
IMPORTANT NOTE:

Due to an unknown configuration error in the lab machine, we will not use Maven for building and
deploying our enterprise project. Instead, we will use default NetBeans plugins for our task. This
plugin is much easier to use than Maven although it is not very powerful. Follow the instruction
below to set the correct environment and create an Enterprise project.
a.
Add the “
EJB and EAR plugin

i.
Open the NetBeans IDE
ii.
Goto “
Tools



Plugins

iii.
On the “
Available Plugins
” page, Type “
Enterprise
” in the search field, tick to select
the “
EJB and EAR
” plugin, and Click “
Install

iv.
Follow the installation guide. A restart of NetBeans may be required.
2
b.
Create an Enterprise Project
i.
Click “
File



New Project”
ii.
Select “Java EE”

“Enterprise Application”, Click “Next”
iii.
Give a name to the project, and click “
Next

3
iv.
In the next window, make sure the GlassFish v2.1 is selected as the server, and
“Create EJB Module” and “Create Web Application Module” are ticked. Click Finish
v.
Now, three modules are created, the project, one EJB module for Java Beans, and
one WAR module for web applications.
4
2.
Create a Session Bean
a.
Right-click on the EJB module, and select “New”

“Session Bean…”
5
b.
In the appearing window, give a name for your Session Bean as the EJB Name, and provide a
package name. Packages help you to organise files. Select “
Stateless
” for the session type and

Remote

only
for the Interface. Click “
Finish
”.
c.
Two files are generated, namely xxxxxBean.java and xxxxxRemote.java, where xxxxx the name
you gave for your bean.
In the xxxxxBean.java file, code “@Stateless” is generated by NetBeans automatically. This
code indicates that this bean is a stateless bean. In the xxxxxRemote.java file, “@Remote” is
also generated by NetBeans automatically. This code indicates that this bean has only a remote
interface.
6
d.
Create a business method in the xxxxx
Bean
.java file.
i.
Open the xxxxx
Bean
.java file.
ii.
Right click within the class definition, and choose “Insert Code…” from the droplist.
iii.
Choose “Add Business Method…”. A business method is the place where you can write
code to perform customised functions, such as calculations. In this lab, our business
7
method should perform the same function as your JSP page did in Lab 1, which is to check
if an input studentID is numeric and within the range of 1 to 10.
e.
In the appearing window, give the name of your business method; specify the return type, which is
“String” for our lab because we want a text message to be returned from this method. We also
need to specify the name and type of the parameters passing to your business method. Click “OK”.
8
f.
As you can observe, a piece of code is generated automatically.
@Stateless
public class mySessionBean implements mySessionRemote {

public String checkID(String studentID) {

return null;
}
g.
Add the function below to test if a number is numeric. This function should be pasted
WITHIN
the
Bean class and
OUTSIDE
your business method. Try to understand how the code works.
private boolean IsNumeric(String strString) {

String strValidChars = "0123456789.-";

char strChar;

boolean blnResult = true;

if (strString.length() == 0) {

return false;

}

for (int i = 0; i < strString.length() && blnResult == true; i++) {

strChar = strString.charAt(i);

if (strValidChars.indexOf(strChar) == -1) {

blnResult = false;

}

}

return blnResult;
}
h.
Add the code below to test if the input studentID is numeric and within the range of 1 to 10. This
function should be pasted
WITHIN
your business method. Try to understand how the code works.
9
String returnVal = "";

if (studentID != null)

{

if (IsNumeric(studentID))

{

Integer integerVal = new Integer(studentID);

int input = integerVal.intValue();

if (input > 0 && input < 11)

{

returnVal = "The Student ID is within the range of 1 to 10";

}

else

{

returnVal = "The Student ID is not within the range of 1 to 10";

}

}

else

{

returnVal = "The input Student ID is not numeric";

}

}

else

{

returnVal = "";

}
return returnVal;
i.
Now your xxxxxBean.java file should look similar to this.
10
3.
Develop a JSP that can invoke the business method in our session bean.
11
a.
Create a JSP page. Instruction can be found from Lab 1. The only difference is that you
need to Right-click on the xxxxx-war module and select “
NEW



JSP…

b.
In your JSP page, add the code below
i.
Goto the
<%@page contentType="text/html" pageEncoding="UTF-8"%>
Embed code below just before the %> symbol. Replace xxxxx with the package
name of your session bean, and yyyyy with the remote interface name of your
session bean. Do not leave any space before the %> symbol.
12
import="xxxxx.yyyyy,
java.util.Properties, javax.naming.Context, javax.naming.NamingException, javax.naming.InitialContext,
javax.rmi.PortableRemoteObject"
ii.
Add the code below before the <html> tag. The jspInit() procedure will be invoked
when the JSP is loaded. The jspDestroy() procedure will be invoked when the JSP
page is closed. Replace xxxxx with the package name of your session bean, and
yyyyy with the remote interface name of your session bean. Try to understand this
code.
<%!
InitialContext jndiContext = null;
Object ref = null;
MySessionRemote dao = null;
String iString = "1";
String returnVal = "default";
public void jspInit() {

iString = "1";

returnVal = "default";

try {

jndiContext = new InitialContext();

dao = (yyyyy) jndiContext.lookup("xxxxx.yyyyy");

} catch (NamingException ex) {

ex.printStackTrace();

}
}
public void jspDestroy() {

jndiContext = null;

ref = null;

dao = null;

iString = "1";

returnVal = "default";
}
%>
iii.
Replace the <body> </body> tags and their contents with code below. Replace
xxxxx with the method you want to invoke in your session bean.
13
<body>

<h2>Retrieve Records from Database</h2>

<form method=get>

<input type=text name="inputString" value="1" />

<input type=submit name="submitButton" value="OK" />

</form>

<%

iString = request.getParameter("inputString");

if (dao != null) {

returnVal = dao.xxxxx(iString);

}

%>

<p>

<table border="0">

<tr>

<th>Student ID:</th>

<td><%= iString%></td>

</tr>

<tr>

<th>Output:</th>

<td><%= returnVal%></td>

</tr>

</table>

</p>
</body>
4.
Build and Deploy your Enterprise project
a.
To build the project, Right-Click on the Project tag, and select “Clean and Build”
b.
To deploy the project, Right-Click on the Project tag, and select “Deploy”.
14
c.
To check your JSP page, go to this address
http://localhost:8080/xxxxx-war/yyyyy.jsp
,
where xxxxx is the name of your project and yyyyy is the name of your JSP page. Note:
differing from Lab1, we are using
war
in the address instead of
web
.
15