Eclipse + JBoss 5 + EJB3 Development Guide (3): local interface with Session Bean

Source: Internet
Author: User
Tags jboss

Sometimes client programs, such as JSP, Servlet, and EJB components run on the same JVM, such as in the same Java EE server (JBoss, WebLogic, and so on). There is no need to access the session bean through the remote interface again, because both the client and the session bean are on the same JVM and need to be accessed only through local interfaces (locals). Just like accessing a local class directly (of course, you cannot access the EJB class directly, you need to use the Jndi and local interfaces to get the native interface object.) Of course, you can also access the EJB through the remote interface, but there are some performance losses. Much of the performance is lost on the web, just as it is accessed through sockets on the same machine. Here's a simple example of how to use the local interface to access the session bean.

First create a session bean local interface with the following code:

package service;

import java.util.List;
import javax.ejb.Local;

@Local
public interface ShoppingCart
{
// 添加某个商品信息
public void addCommodity (String value);
// 获得客户购买的所有商品的纱衔泥
public List<String> getCommodity();
}

The specific implementation class of the session Bean is the same as the implementation class for the remote interface, and the reader can refer to the Shoppingcartbean class in the article "Writing stateful Sessionbean".

The following is a new Web project in Eclipse (project name: Jbossweb) that needs to refer to the MYEJB project (the project that contains the EJB component) to create a test.jsp page in the WebContent directory with the following code:

<%@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030" import= "java.util.list,java.util.properties,
Javax.naming.initialcontext,service. ShoppingCart "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>

</body> Remote
<%
Properties prop = new properties ();
InitialContext CTX = new InitialContext ();
ShoppingCart ShoppingCart = (ShoppingCart) ctx
. Lookup ("shoppingcartbean/local");
Shoppingcart.addcommodity ("Bicycle");
Shoppingcart.addcommodity ("notebook computer");
Shoppingcart.addcommodity ("LCD TV");
List<string> commodities = shoppingcart.getcommodity ();
for (String commodity:commodities)
{
SYSTEM.OUT.PRINTLN (commodity);
}
%>

The Jndi name to use for accessing the local interface should be "shoppingcartbean/local".

Start JBoss and enter the following url:http://localhost:8080/jbossweb/test.jsp in the browser

The following information is output in Eclipse's console:

16:55:17,633 INFO [STDOUT] Bike

16:55:17,633 INFO [STDOUT] notebook computer

16:55:17,633 INFO [STDOUT] LCD TV

Related Article

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.