环境
MyEclipse 8.6 + JBoss 6.0 + JDK 1.6.13 + EJB 3.0
问题
使用EJB Local接口
解决
1. 新建Enterprise Application Project,注意New Web module Project勾选上
2.src--------->新建包
3. 新建接口HelloWorld
package com.wgb.bean;
/**
* @className: HelloWorld.java
* @classDescription:
* @function:
* @author: Wentasy
* @createTime: 2012-11-26 下午09:08:14
* @modifyTime:
* @modifyReason:
* @since: JDK 1.6
*/
public interface HelloWorld {
public String sayHello(String name);
}
4. 新建类 HelloWorldBean
package com.wgb.bean;
import javax.ejb.Local;
//import javax.ejb.Remote;
import javax.ejb.Stateless;
/**
* @className: HelloWorldBean.java
* @classDescription:
* @function:
* @author: Wentasy
* @createTime: 2012-11-26 下午09:09:02
* @modifyTime:
* @modifyReason:
* @since: JDK 1.6
*/
@Stateless
//@Remote ({HelloWorld.class})
@Local ({HelloWorld.class})
public class HelloWorldBean {
public String sayHello(String name) {
return "Hello World!" + name;
}
}
5. 在WebRoot下修改index.jsp文件
<%@ page language="java" import="java.util.*,javax.naming.*,com.wgb.bean.HelloWorld" 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>
<%
try {
//Remote
//Properties props = new Properties();
//props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
//props.setProperty("java.naming.provider.url", "localhost:1099");
//props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
//InitialContext ctx = new InitialContext(props);
//HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
//out.print(helloworld.sayHello("WGB"));
//Local
InitialContext ctx = new InitialContext();
HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/local");
out.print(helloworld.sayHello("Wentasy"));
} catch (NamingException e) {
e.printStackTrace();
}
%>
</body>
</html>
6. 在浏览器输入http://localhost:8080/HelloEJBWeb访问。
参考资料
深入学习EJB3.0之一:概述与搭建环境
http://blog.csdn.net/drykilllogic/article/details/6185745
ejb jboss myeclipse环境搭建工程实例
http://blog.csdn.net/weirenren_027/article/details/8024060
websphere ejb 远程/本地调用总结