利用 WebSphere Application Server Community Edition 中现有的 WebSphere Application Server J2EE 资源
2009-10-23 00:00:00 来源:WEB开发网右键单击 Deployment Descriptor: GeronimoWebApp ,并选择 New => Servlet。将 Servlet 命名为 TestEJBServlet,然后单击 Next。
图 11. 创建一个新的 Servlet
对于 Java 包的名称,输入 devworks.example,然后单击 Next。
图 12. 指定包
取消选中 doPost,然后单击 Finish。
图13. 完成 Servlet
为 doGet 方法插入下面的代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String text = "";
final String jndiName = "ejb/devworks/example/SimpleEJBHome"; [A]
Properties env = new Properties();
env.setProperty(Context.PROVIDER_URL, "iiop://localhost:2809"); [B]
env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
try {
InitialContext ctx = new InitialContext(env); [C]
Object obj = ctx.lookup(jndiName); [D]
SimpleEJBHome ejbHome =
(SimpleEJBHome)
javax.rmi.PortableRemoteObject.narrow(obj, SimpleEJBHome.class); [E]
SimpleEJB myEJB = ejbHome.create(); [F]
text = myEJB.retrieveText(); [G]
} catch(NamingException ne) { [H]
System.err.println("TestEJBServlet failed to lookup SimpleEJBHome. JNDI
String="+jndiName);
ne.printStackTrace(System.err);
// Recovery strategy omitted (i.e. try again with a different host/port)
} catch(CreateException ce) {
System.err.println("TestEJBServlet failed to create SimpleEJB instance.");
ce.printStackTrace(System.err);
// Recovery strategy omitted (i.e. clear the JNDI resource cache on the
// context factory and try again)
}
response.getWriter().println("Text from WebSphere Server: " + text); [I]
Tags:利用 WebSphere Application
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接