开发远程和本地接口的无状态会话bean

  

这个bean的开发,就是前面两个开发的综合,没有什么新东西。代码如下:

1、定义接口

      远程接口:

package huizhi.HelloEJB3;
import javax.ejb.Remote;

@Remote
public interface HelloEJB3beanRemote {
	public String remoteHello(String name);
}


    本地接口:

package huizhi.HelloEJB3;
import javax.ejb.Local;

@Local
public interface HelloEJB3beanLocal {
	public String localHello(String name);
}



2、完成实现类

package huizhi.HelloEJB3;

import javax.ejb.Stateless;

/**
 * Session Bean implementation class HelloEJB3bean
 */
@Stateless
public class HelloEJB3bean implements HelloEJB3beanRemote, HelloEJB3beanLocal {

    /**
     * Default constructor. 
     */
    public HelloEJB3bean() {
        // TODO Auto-generated constructor stub
    }
    public String localHello(String name){
    	return new String("本地接口:你好," + name);
    }
    public String remoteHello(String name){
    	return new String("远程接口:你好," + name);
    }

}


3、测试的JSP页面

<%@ page language="java" import="javax.naming.*, huizhi.HelloEJB3.*" pageEncoding="GB2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>无状态远程接口&本地接口调用</title>
</head>
<body>
	<%
		try{
			InitialContext ctx = new InitialContext();
			HelloEJB3beanLocal hrL = (HelloEJB3beanLocal)ctx.lookup("HelloEJB3bean/local");
			out.println("<br>通过本地接口调用EJB<br>" + hrL.localHello("李小婉"));
			
			HelloEJB3beanRemote hrR = (HelloEJB3beanRemote)ctx.lookup("HelloEJB3bean/remote");
			out.println("<br><br>通过远程接口调用EJB<br>" + hrR.remoteHello("李小婉"));
		}catch(Exception e){
			out.println("调用失败!");
		}
	%>
</body>
</html>


在JBoss打包和发布,也和远程接口的会话bean那里说的一样。

你可能感兴趣的:(开发远程和本地接口的无状态会话bean)