|
firstEJB.jsp JSP Client page :
Create a new JSP page in the C:\Projects\TomcatJBoss folder and save it as "firstEJB.jsp". Now copy and paste
the following code in it :
<%@ page import="javax.naming.InitialContext,
javax.naming.Context,
java.util.Properties,
com.stardeveloper.ejb.session.First,
com.stardeveloper.ejb.session.FirstHome"%>
<%
long t1 = System.currentTimeMillis();
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.PROVIDER_URL, "localhost:1099");
Context ctx = new InitialContext(props);
FirstHome home = (FirstHome)ctx.lookup("ejb/First");
First bean = home.create();
String time = bean.getTime();
bean.remove();
ctx.close();
long t2 = System.currentTimeMillis();
%>
<html>
<head>
<style>p { font-family:Verdana;font-size:12px; }</style>
</head>
<body>
<p>Message received from bean = "<%= time %>".<br>Time taken :
<%= (t2 - t1) %> ms.</p>
</body>
</html>
Hit the 'save' button to save the firstEJB.jsp JSP page.
Explanation :
As you can see the code to connect to an *external* JNDI/EJB Server is extremely simple. First we create a
Properties object and put certain values for Context.INITIAL_CONTEXT_FACTORY and Context.PROVIDER_URL
properties. The value for Context.INITIAL_CONTEXT_FACTORY is the interface provided by JBoss and the value for
Context.PROVIDER_URL is the location:port number where JBoss is running. Both of these properties are required to
connect to an *external* JNDI Server.
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.PROVIDER_URL, "localhost:1099");
We then get hold of that external JNDI Context object by creating a new InitialContext() object, it's
argument being the Properties object we had created earlier.
Context ctx = new InitialContext(props);
Next we use that external JNDI Context handle to lookup our FirstEJB running on JBoss. Notice that the
argument to Context.lookup("ejb/First") is the same value we had put in the jboss.xml file to bind our FirstEJB
to this name in the JNDI context. We use that same value again to look for it. Once our lookup is successful,
we cast it to our FirstHome Home interface.
FirstHome home = (FirstHome)ctx.lookup("ejb/First");
We then use create method of our FirstHome Home interface to get an instance of the Remote interface;
First. We will now use the methods of our EJB's remote interface ( First ).
First bean = home.create();
We then call the getTime() method we had created in our EJB to get the current time from JBoss Server and save
it in a temporary String object.
String time = bean.getTime();
Once we are done with our Session EJB, we use the remove method to tell the JBoss Server that we no longer
need this bean instance. Next we also close the external JNDI Context.
bean.remove();
ctx.close();
We then display this retrieved value from getTime() method on the user screen.
vi. Running the JSP page :
Before trying to run firstJSP.jsp page, we have to do one other thing. Copy following files from C:\JBoss\client
folder and paste them in the C:\Projects\TomcatJBoss\WEB-INF\lib folder. It is a must, without it firstEJB.jsp page will
not run.
connector.jar
deploy.jar
jaas.jar
jboss-client.jar
jboss-j2ee.jar
jbossmq-client.jar
jbosssx-client.jar
jndi.jar
jnp-client.jar
You will also copy the FirstEJB.jar file from C:\Projects\EJB\FirstEJB folder to the C:\Projects\TomcatJBoss\WEB-INF\lib
folder. Without it the Tomcat will not be able to compile firstEJB.jsp JSP page.
We are now ready to run our firstEJB.jsp page. Mover over to the last page of this tutorial, the
next page.
Running firstEJB.jsp JSP page :
Now start JBoss Server if it is not already running. Also start Tomcat Server. If it is already running, then
stop it and restart it again. Open your browser and access following page :
http://localhost:8080/jboss/firstEJB.jsp
Please substitute port number "8080" above with the port number where your Tomcat Server is running. By default
it is "8080". If you have done everything as was described in this article, then you should see a result
like following :

Fig - firstEJB.jsp Client View
Note:
Since we are not running Tomcat/JBoss combination on Stardeveloper, I could not provide an online demo for
this tutorial. But one thing I can tell you is that this is all very easy and there is no reason why you should not be
able to run it on your own system.
Summary :
In this tutorial we were able to learn what comprises an Enterprise JavaBean and how to develop and deploy an
EJB. We created an EJB, deployed it on JBoss Server and called it from a JSP page running on Tomcat Server in a
separate process.
We also learned how to install and configure JBoss and Tomcat Servers. JBoss/Tomcat is a pretty popular
combination now a days as quite a few sites are using it. If you don't like Tomcat, don't worry there is another
very good Web container which supports latest JSP 1.2 and Servlets 2.3 specifications; Resin. Resin is an extremely fast server and having personal experience with it, I can say that
Orion and Resin are two of the fastest Servers around. So
if you want to substitute Resin with Tomcat for this tutorial ( or in your production web site ), you can easily do
so. And as I have tested, you won't have to change a single line of code or change any configuration settings in the EJB and
JSP pages that we have made.
Since it would have been your first EJB, I wanted to take as much time ( without wasting it of course ;) )
as possible to describe things in detail. I hope if you followed everything, you should be able to run it
successfully on your system. Just in case if it didn't work out for you, try doing it again and pay attention
to every step so that you don't leave anything. firstEJB.jsp JSP Client page :
Create a new JSP page in the C:\Projects\TomcatJBoss folder and save it as "firstEJB.jsp". Now copy and paste
the following code in it :
<%@ page import="javax.naming.InitialContext,
javax.naming.Context,
java.util.Properties,
com.stardeveloper.ejb.session.First,
com.stardeveloper.ejb.session.FirstHome"%>
<%
long t1 = System.currentTimeMillis();
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.PROVIDER_URL, "localhost:1099");
Context ctx = new InitialContext(props);
FirstHome home = (FirstHome)ctx.lookup("ejb/First");
First bean = home.create();
String time = bean.getTime();
bean.remove();
ctx.close();
long t2 = System.currentTimeMillis();
%>
<html>
<head>
<style>p { font-family:Verdana;font-size:12px; }</style>
</head>
<body>
<p>Message received from bean = "<%= time %>".<br>Time taken :
<%= (t2 - t1) %> ms.</p>
</body>
</html>
Hit the 'save' button to save the firstEJB.jsp JSP page.
Explanation :
As you can see the code to connect to an *external* JNDI/EJB Server is extremely simple. First we create a
Properties object and put certain values for Context.INITIAL_CONTEXT_FACTORY and Context.PROVIDER_URL
properties. The value for Context.INITIAL_CONTEXT_FACTORY is the interface provided by JBoss and the value for
Context.PROVIDER_URL is the location:port number where JBoss is running. Both of these properties are required to
connect to an *external* JNDI Server.
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.PROVIDER_URL, "localhost:1099");
We then get hold of that external JNDI Context object by creating a new InitialContext() object, it's
argument being the Properties object we had created earlier.
Context ctx = new InitialContext(props);
Next we use that external JNDI Context handle to lookup our FirstEJB running on JBoss. Notice that the
argument to Context.lookup("ejb/First") is the same value we had put in the jboss.xml file to bind our FirstEJB
to this name in the JNDI context. We use that same value again to look for it. Once our lookup is successful,
we cast it to our FirstHome Home interface.
FirstHome home = (FirstHome)ctx.lookup("ejb/First");
We then use create method of our FirstHome Home interface to get an instance of the Remote interface;
First. We will now use the methods of our EJB's remote interface ( First ).
First bean = home.create();
We then call the getTime() method we had created in our EJB to get the current time from JBoss Server and save
it in a temporary String object.
String time = bean.getTime();
Once we are done with our Session EJB, we use the remove method to tell the JBoss Server that we no longer
need this bean instance. Next we also close the external JNDI Context.
bean.remove();
ctx.close();
We then display this retrieved value from getTime() method on the user screen.
vi. Running the JSP page :
Before trying to run firstJSP.jsp page, we have to do one other thing. Copy following files from C:\JBoss\client
folder and paste them in the C:\Projects\TomcatJBoss\WEB-INF\lib folder. It is a must, without it firstEJB.jsp page will
not run.
connector.jar
deploy.jar
jaas.jar
jboss-client.jar
jboss-j2ee.jar
jbossmq-client.jar
jbosssx-client.jar
jndi.jar
jnp-client.jar
You will also copy the FirstEJB.jar file from C:\Projects\EJB\FirstEJB folder to the C:\Projects\TomcatJBoss\WEB-INF\lib
folder. Without it the Tomcat will not be able to compile firstEJB.jsp JSP page.
We are now ready to run our firstEJB.jsp page. Mover over to the last page of this tutorial, the
next page.
Running firstEJB.jsp JSP page :
Now start JBoss Server if it is not already running. Also start Tomcat Server. If it is already running, then
stop it and restart it again. Open your browser and access following page :
http://localhost:8080/jboss/firstEJB.jsp
Please substitute port number "8080" above with the port number where your Tomcat Server is running. By default
it is "8080". If you have done everything as was described in this article, then you should see a result
like following :

Fig - firstEJB.jsp Client View
Note:
Since we are not running Tomcat/JBoss combination on Stardeveloper, I could not provide an online demo for
this tutorial. But one thing I can tell you is that this is all very easy and there is no reason why you should not be
able to run it on your own system.
Summary :
In this tutorial we were able to learn what comprises an Enterprise JavaBean and how to develop and deploy an
EJB. We created an EJB, deployed it on JBoss Server and called it from a JSP page running on Tomcat Server in a
separate process.
We also learned how to install and configure JBoss and Tomcat Servers. JBoss/Tomcat is a pretty popular
combination now a days as quite a few sites are using it. If you don't like Tomcat, don't worry there is another
very good Web container which supports latest JSP 1.2 and Servlets 2.3 specifications; Resin. Resin is an extremely fast server and having personal experience with it, I can say that
Orion and Resin are two of the fastest Servers around. So
if you want to substitute Resin with Tomcat for this tutorial ( or in your production web site ), you can easily do
so. And as I have tested, you won't have to change a single line of code or change any configuration settings in the EJB and
JSP pages that we have made.
Since it would have been your first EJB, I wanted to take as much time ( without wasting it of course ;) )
as possible to describe things in detail. I hope if you followed everything, you should be able to run it
successfully on your system. Just in case if it didn't work out for you, try doing it again and pay attention
to every step so that you don't leave anything.
|