ejb_jndi访问

来源:互联网 发布:多货币对冲ea源码 编辑:程序博客网 时间:2024/06/04 17:42

搞了好久终究搞出来了,一个人慢慢的摸索,确实浪费时间,不多说了,ejb的数种访问方式(jndi)

慢慢补充:先介绍java:app,java:module,java:global

服务器依旧选取的是glassfish

EJB采用3.0

写好ejb;以HelloWorld为例:

接口:

package com.chx.helloworld;
import javax.ejb.Local;

@Local //也可以为Remote
public interface HelloWorld {
public String sayHello(String name ,int i);
}

bean类:

package com.chx.helloworld;

import javax.ejb.Stateful;


@Stateful //或者Stateless/Singleton
public class HelloWorldBean implements HelloWorld {

public String sayHello(String name, int i) {

System.out.println("the test num is :"+i);
return name + "say the test is ok !";
}


}


由bean类和接口即可打包成jar文件,即ejb_jar。

下面是客户端代码:

<%@page import="com.chx.helloworld.HelloWorld"%>
<%@page import="javax.naming.InitialContext"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    This is my JSP page. <br>
    <%
    
try {

InitialContext ctx = new InitialContext();
for(int i =0;i<=9;i++){
// HelloWorld helloWorld = (HelloWorld ) ctx.lookup("java:global/MyEjb3_module/MyEjb3_moduleEJB/HelloWorldBean");
// HelloWorld helloWorld = (HelloWorld ) ctx.lookup("java:app/MyEjb3_moduleEJB/HelloWorldBean");
HelloWorld helloWorld = (HelloWorld ) ctx.lookup("java:module/HelloWorldBean");
out.println(helloWorld.sayHello("chx", i));
}
}catch(Exception e){
e.printStackTrace();
}    
    
     %>
  </body>
</html>



说明:当采用java:app 形式锁定EJB时,服务器端和客户端可以分开,但都要部署到通一个cluster上.

而当采用java:module 形式,如:


即ejb在war包中。






0 0
原创粉丝点击