Developing a portlet which accesses an enterprise bean

This article shows how to invoke an enterprise bean which conforms to Sun's Enterprise JavaBean architecture (hereafter called an EJB) from portlet code. It includes step-by-step instructions for developing a portlet which accesses a stateless session EJB. It also provides an EJB enterprise application, as a download, and detailed instructions for deploying it to WebSphere Application Server.

Sukumar Konduru (konduru@us.ibm.com), Advisory Software Engineer, IBM

Sukumar Konduru is an Advisory Software Engineer at the IBM Dallas, Developer Technical Support Center. He holds M.S in Computer Science from University of Houston. You can reach Sukumar Konduru at konduru@us.ibm.com



Venkata Nagalla (nagalla@us.ibm.com), Senior Software Engineer, IBM

Author photo: Vankata Negalla Venkata Nagalla is an e-business architect in IBM's Developer Relations Technical Consulting office in Austin, Texas. Venkata works with IBM's business partners-small or large companies, from startups to large firms-helping them get evangelized, educated, and enabled on IBM's e-business platform. You can reach Venkata Nagalla at nagalla@us.ibm.com.



08 September 2004

Introduction

For enterprise applications, the Enterprise Archive (EAR) file can contain both Web modules and EJB modules. It is easy to develop a servlet which accesses an EJB, if it is deployed in a single EAR file. However, a portlet can only be developed as a WAR file; you cannot deploy a portlet and an EJB in single file. Therefore, developing a portlet which accesses an EJB from a portlet is different than developing servlets to access an EJB.

This article is for portlet developers who need to access an EJB from portlet code. To get the most out of this article, you should already know how to develop and deploy portlets in Websphere Portal, using WebSphere tooling.

To follow along with the scenario described here, you need to download the example ZIP file and extract the contents to a directory on your harddrive. You also need access to the following environment:

  1. IBM® WebSphere® Studio Application Developer V5.1.2 (hereafter called Application Developer), which is used to develop enterprise applications
  2. IBM Portal Toolkit 5.0.2.2 installed into Application Developer as a plug-in, for developing portlets
  3. IBM WebSphere Application Server V5.0.2, for installing the application

Deploying the sample enterprise application

The attached enterprise application (in the download) complies with the EJB 1.1 specification. It includes an EAR file and a JAR file containing an EJB session bean. The enterprise application project contains one stateless session bean, which provides the sayHello method. The example portlet code you see later invokes this method. You must package the EJB JAR file in the portlet as described in the following sections. To deploy the sample EJB:

  1. Open the WebSphere Administrative Console in a Web browser.
  2. Expand Applications and then click on Install New Application
  3. For the local path field, browse to the directory where you extracted the download. Select HelloWorldEAR.ear, and then click on Next.
  4. On the You can choose to generate default bindings and mappings window, click Next.
  5. Click Step 5 Summary, and then click Finish.
  6. Click Save to Master Configuration, and then click Save.
  7. Click Application -> Enterprise Applications, and find HelloWorldEAR.ear.
  8. Click the checkbox next to the application, and click Start to start it.

Developing a portlet to access the sample EJB

In this section, you see how to develop a portlet application that accesses a session bean using JNDI context.

Generating the portlet code

  1. Open WebSphere Studio.
  2. Select File => New => Other to start the New wizard.
    Figure 1. Creating a new project
    Creating a new project
  3. On the Select page, in the left pane select Portlet development; in the right pane, select Portlet Project.
    Figure 2. Selecting the portlet project type
    Selecting the portlet project type
  4. Click Next.
  5. In the Project Name field, enter PortletAccessingEJB, and check Configure advanced options.
    Figure 3. Entering a project name
    Entering a project name
  6. Click Next.
  7. On the J2EE Settings Page, in the EAR project field, enter PortletAccessingEJBEAR.
    Figure 4. J2EE Settings Page
    J2EE Settings Page
  8. Click Next until you see Event Handling page.
  9. De-select Add action Listener:
    Figure 5. Event Handling
    Event Handling
  10. Click Finish, and then click Yes on the Confirm Perspective Switch window to see the Portlet Perspective. The portlet.xml file opens in Application Developer's Portlet view.
    Figure 6. portlet.xml
    portlet.xml

Copying the EJB JAR to the portlet project

You need to copy the EJB JAR to the portlet project so that the portlet code can invoke methods on the session bean.

Copy the Helloworld.jar from the download by dragging and dropping it to the WEB-INF/lib directory.

Figure 7. Copying Helloworld.jar to WEB-INF/lib directory
Copying Helloworld.jar

You see the JAR file in the path, as shown in the Figure 8.

Figure 8. Copied Helloworld.jar in WEB-INF/lib directory
Copied Helloworld.jar

Modifying the Portlet Code

Next you see how to modify the PortletAccessingEJBPortlet.java file to invoke methods on the remote session bean. You need to set the value of Context.PROVIDER_URL in the doView() method to the URL of the server on which the EAR was deployed.

  1. In the Java Resources folder, open PortletAccessingEJBPortlet.java. Replace the doView method, shown in Listing 1, with the code shown in Listing 2.

    Listing 1. Including JSP

    // Invoke the JSP to render        		
    	    getPortletConfig().getContext().
    	    include(VIEW_JSP+getJspExtension(request), request, response);
       

    Listing 2. Code for invoking method on MyHelloWorld bean

        PrintWriter pw = response.getWriter();
        	  try{	
        		Hashtable env = new Hashtable(); 
        		 env.put(Context.INITIAL_CONTEXT_FACTORY,
        		  "com.ibm.websphere.naming.WsnInitialContextFactory"); 
        		 env.put(Context.PROVIDER_URL,
        		  "corbaloc:iiop:sukumar.dfw.ibm.com:2809");
         
        		 InitialContext ctx= new InitialContext(env);
        		Object objectHome 
        		  =ctx.lookup("ejb/com/ibm/dr/MyHelloWorldHome");
        	
        		MyHelloWorldHome 
        		  home=(MyHelloWorldHome)javax.rmi.PortableRemoteObject.
        		  narrow(objectHome, MyHelloWorldHome.class);
        	  
        		MyHelloWorld myHelloWorld=home.create();
        	  
        		String s=myHelloWorld.sayHello();
        		pw.println(
        		"The <i>sayHello</i> method of MyHelloWorld returns<b>"
        		   + s + " </b>");  	  
        	    } catch(Exception e){
        	  	
        		pw.println("the exception is " + e);
        	  	
    	   }
  2. Replace the value of Context.PROVIDER_URL in the above code to the appropriate URL of the deployed EJB; that is, set it to the URL of the server on which the EAR was deployed.

    The following displays in Application Developer.

    Figure 9. PortletAccessingEJBPortlet.java
    PortletAccessingEJBPortlet
  3. You will see some errors. To fix them, right-click, and select Source-> Organize Imports, as shown in the Figure 10.
    Figure 10. Organizing the imports
    Organizing Imports
  4. In the Organize Imports wizard, select javax.naming.context type. You see the file without errors, as shown Figure 11.
    Figure 11. After organizing imports
    After organizing iImports
  5. Click Ctrl+S to save the file.

Exporting WAR file

  1. Select the project and click on File -> Export.
    Figure 12. Exporting the WAR file
    Exporting the WAR file
  2. Select the WAR file.
    Figure 13. Selecting the WAR file
    Selecting the WAR file
  3. Point to the WAR file.
    Figure 14. Specifying the directory to export the WAR file
    Specifying the directory to export the WAR file
  4. Click on Finish to complete exporting WAR file.

Testing the portlet

To test the portlet:

  1. Install the portlet in WebSphere Portal, as you would any other portlet.
  2. Customize a portal page with the the portlet.
  3. Display the page. The contents of the portlet shown in the Figure 15.
    Figure 15. Output of the portlet
    Output of the portlet

In Figure 15, you see the portlet displaying the returned data from sayHello method of the EJB session bean.


Conclusion

In this article, you stepped through the procedure for developing a portlet that accesses an EJB of type stateless session. Invoking entity beans from portlet code is similar. Because you cannot package an EJB along with portlets, you need to code the access to the EJB deployed in a different manner than you would within a servlet accessing the enterprise application.


Download

DescriptionNameSize
Code sampleportletaccessejb.zip  ( HTTP | FTP )177KB

Resources

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into WebSphere on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=13694
ArticleTitle=Developing a portlet which accesses an enterprise bean
publish-date=09082004