Session Bean for Speed-based progressive learning (i)

Source: Internet
Author: User

The role of the session bean

The session bean represents the sessions between the client application and the EJB container. Session beans are usually implemented as business logic and interact with the entity bean to perform specific operations. However, the session bean does not necessarily use the entity bean. If needed, it can communicate directly with the database.

A ' Hello World ' session Bean

The structure of the Enterprise JavaBean is difficult to understand at the beginning, but as long as you use it, EJB is not too difficult. Fortunately, you can familiarize yourself with the basic EJB structure without having to involve a database connection or a transaction. To do this, you can start with the most famous "Hello World" bean.

When you are designing an EJB application, you may not know whether to start with the bean and then create the interface, or start with the interface and then create the bean. I suggest you better start with the interface. Because if you don't know how the customer uses the bean, you don't need to write it.

Create a remote interface

Listing 6.1 shows the Helloworldsession interface, which is the remote interface of the "Hello World" session bean.

  Listing 6.1 Source Code for HelloWorldSession.java
package usingj2ee.hello;
  import java.rmi.*;
import javax.ejb.*;
  /** Defines the methods you can call on a HelloWorldSession object */
  public interface HelloWorldSession extends EJBObject
{
  /** Returns the session's greeting */
public String getGreeting() throws RemoteException;
  /** Changes the session's greeting */
public void setGreeting(String aGreeting) throws RemoteException;
  }

Create the Home interface

A session Bean's home interface contains a method for creating a new session. For the Hello World example, there are two different create methods, one without parameters, and the other allows you to provide your own welcome words. Listing 6.2 shows the Helloworldsessionhome interface.

  Listing 6.2 Source Code for HelloWorldSessionHome.java
package usingj2ee.hello;
  import java.rmi.*;
import javax.ejb.*;
  /** Defines the methods for creating a HelloWorldSession */
  public interface HelloWorldSessionHome extends EJBHome
{
  /** Creates a HelloWorldSession bean with default settings */
public HelloWorldSession create() throws RemoteException, CreateException;
  /** Creates a HelloWorldSession bean with a specific initial greeting */
public HelloWorldSession create(String aGreeting)
throws RemoteException, CreateException;
  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.