Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : EJB : Building your first Enterprise JavaBean
 

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 :

firstEJB.jsp Client View
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 :

firstEJB.jsp Client View
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.


Previous ( 3 Gone )( No Further Pages )

See all comments and questions (post-ad) posted for this tutorial.


Download Associated Files
0009.jar

Related Articles
  1. An Introduction to Enterprise JavaBeans
  2. Reading and Parsing XML Files with Enterprise JavaBeans
  3. EJB 2.1 Kick Start : Implementing a Solution Using EJB 2.1

Comments/Questions ( Threads: 32, Comments: 42 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Facing problem to Run EJB Sample
  2. How to make this?
  3. Best tutorial who want to start.
  4. Thanks very much very helpful
  5. Can I use Entity Bean with out session bean ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  6. Building your first Enterprise JavaBean
  7. Downloading EJB builder
  8. Exception while executing the above code
  9. how to debug in MyEclipse using JBoss server
  10. not able to deploy ejb,jsp in JBoss server
  11. HttpSession Replication
  12. How to Deploy the jar in Weblogic81?
  13. HTTP Status 404 - /firstEJB.jsp
  14. Regarding EJB tutorial
  15. ejb not bound ( 2 Replies )
  16. Ejb on Weblogic server using Eclipse ( 1 Reply )
  17. getting error while i am compiling FirstHome.java, how can i solve this error
  18. org.apache.jasper.JasperException: Unable to compile class for JSP
  19. Tomcat/Jboss: help me!! ( 1 Reply )
  20. Is it necessary to have EJBs deployed in EJBServer(JBoss) to be present in the client side
  21. not able to deploy jsp on tomcat server
  22. Not able to compile the java files.
  23. Great article. Thanks.
  24. Problem in EJB
  25. JBoss 3.2.1 and Tomcat 4.1.24 ( 2 Replies )
  26. JBoss 3.2.1 and Tomcat 4.1.18
  27. EJB on weblogic
  28. Thank you
  29. Return an object called client for example
  30. EJB Not Bound
  31. Tomcat/Jboss: help me !! ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  32. Tomcat/JBoss : Port Issue ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.