Creating and Accessing a Session Bean in a  Web Application

Overview

    Purpose

    This tutorial covers creating a Session Bean and accessing it in a Web Application using JSP and Servlet.

    Time to Complete

    Approximately 45 minutes.

    Introduction

    Enterprise JavaBeans technology is the server-side component architecture for developing and deploying business applications in Java EE. The latest release of the technology, JSR 318: Enterprise JavaBeans 3.1, which is available in the Java EE 6 platform, further simplifies the technology and makes many improvements. There are two types of EJB components: Session beans and  Message-driven beans. 

    In this tutorial, you will create a JEE 6 Web Application and add the following components to it - Stateless Session Bean, a Servlet, and a JSP. The  JSP will contain a form allowing you to specify a name. The form will submit to a Servlet which will read the name entered as the form parameter. The Servlet will pass the name to a sayHello method that is in the Session bean that you will create.

    Hardware and Software Requirements

    The following is a list of hardware and software requirements:

    • Download and install Java JDK 7 from this link.
    • Download and install   NetBeans 7.1.2 with Java EE which includes GlassFish 3.1.2 (Java EE download bundle) from this link. During installation, be sure to check the box to install GlassFish. JUnit is an optional installation and not required for this tutorial

    Prerequisites

    Before starting this tutorial, you should:

    •  Have the software installed as listed under Hardware and Software Requirements section.
    •  Ensure NetBeans is running.
    • For best results use Firefox or Chrome browsers.

Create  a Java EE  Web Application

    To create a Java EE  Web Application, perform the following steps in the NetBeans IDE.

    Create a new Web Application.

    Select File->New Project from the NetBeans menu.

    Select the Java Web category and a project type of  Web Application.

    Click Next.


    Enter  the project name as SessionBeanDemo. Click Next.



    Verify the following :

     GlassFish Server 3.1.2 is  selected as the server.

    Java EE 6 Web is selected as  Java EE Version.

    Click Finish.

    You should now have a Web Application project with an index.jsp file.

Create Session Bean

    To create a stateless session bean that is accessed using the local client access mode, perform the following steps in NetBeans IDE.

    Right-click on the SessionBeanDemo project and select New->Other.

    In the New File window, select a category of Enterprise JavaBeans and a file type of Session Bean.

    Click Next.


    Specify the Session Bean information as follows:

    EJB Name: SayHelloSessionBean

    Package Name: com.example

    Ensure Stateless is selected as the Session Type

    Select  Local as the option for Create Interface.

    Click Finish.

    Clients access a session bean through the bean's interfaces. The interfaces enable  communication between the client and the bean. A bean can have local interfaces or remote interfaces. If the beans must run in the same Virtual machine, they are local.
    Remote interfaces will work for any client. You can use local interfaces for Servlet and JSP clients. In this tutorial, Web components (JSP/Servlet) are clients and hence we are creating a Local interface to the session bean. NetBeans automatically creates the local interface for the session bean since we have selected Interface Type as Local.

    Double-click SayHelloSessionBeanLocal.java in the Source Packages node to open  in the code editor.

    Add the following line of code to the SayHelloSessionBeanLocal interface to declare the sayHello method.


           String sayHello(String name) ;



    SayHelloSessionBeanLocal.java is the local interface of the session bean that NetBeans automatically generates.

    Double-click SayHelloSessionBean.java in the Source Packages node to open  in the code editor.

    Implement  sayHello method in the SayHelloSessionBean.

    public String sayHello(String name) {
            return "Hello  " + name;
        }


    Observe that the SayHelloSessionBean session bean implements its local interface, SayHelloSessionBeanLocal.


Create Servlet

    To create a Servlet, perform the below steps in NetBeans IDE.

    Right-click on the SessionBeanDemo  project and select New->Other.

    In the New File window, select a category of Web and a file type of Servlet.

    Click Next.


    Specify the Servlet  information as follows:

    Class Name: SayHelloServlet

    Package Name: com.example

    Click Finish.

    Perform the following changes to the SayHelloServlet.

    a.  Import the following package.

             import javax.ejb.EJB;

    b. Add a field of type SayHelloSessionBeanLocal named sayHelloSessionBean.

     @EJB
     private SayHelloSessionBeanLocal sayHelloSessionBean;
                                                    


    In the ProcessRequest method of SayHelloServlet, make the following changes.

    a.  Add the below lines of code

    String str1=request.getParameter("name");
    String str2=sayHelloSessionBean.sayHello(str1);


    b.  Add the below line of code to modify the HTML response produced by the SayHelloServlet.

    out.println("<h1>" + str2 + "</h1>");



Create JSP

    To modify index.jsp, perform the following steps in NetBeans IDE.

    Open the existing index.jsp file from the Web Pages portion of the SessionBeanDemo project.


    You will modify the index.jsp file to produce the output shown in the figure.


    Modify the title and heading of the page to Say Hello.

    Add a form to the body of the index.jsp page which contains one text box  named  name and a submit button named OK.

        <form  method="post" action="SayHelloServlet">
                Enter Your Name: <input type="text" name="name">
                <input type="submit" value="OK">
         </form>

           



Deploy the Web Application

    To deploy and run the application, perform the following steps in NetBeans IDE.

    Right-click SessionBeanDemo project in the projects window and select  Build. 


    In the Projects window, right-click SessionBeanDemo and select Run.

    Verify the output. Enter a name in the text box and click OK.


    The output will be as shown below.




Summary


    In this tutorial, you have learned how to:

    • Create a Session Bean.
    • Access the Session Bean in a Web Application using Servlet and JSP.

    Resources




    Credits

    • Lead Curriculum Developer: Anjana Shenoy

To help navigate this Oracle by Example, note the following:

Hiding Header Buttons:
Click the Title to hide the buttons in the header. To show the buttons again, simply click the Title again.
Topic List Button:
A list of all the topics. Click one of the topics to navigate to that section.
Expand/Collapse All Topics:
To show/hide all the detail for all the sections. By default, all topics are collapsed
Show/Hide All Images:
To show/hide all the screenshots. By default, all images are displayed.
Print:
To print the content. The content currently displayed or hidden will be printed.

To navigate to a particular section in this tutorial, select the topic from the list.