欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EJB学习笔记-4-web应用访问EJB(remote)

程序员文章站 2024-02-22 22:05:22
...

首先将ejb的接口文件jar包导入web项目下的lib目录中


如下是jsp文件编码:

<%@ page language="java" import="java.util.*,javax.naming.*,com.manfred.ejb.*;" pageEncoding="GB18030"%>
<%
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>
    =========================================<br>
       以下是web远程调用<br>
    <%
    	final Hashtable jndiProperties = new Hashtable();
    	jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    	Context context;
    	try{
    		context = new InitialContext(jndiProperties);
    		//appName和moduleName分别就打包的格式而定
    		//如果是.ear就是appName,其它就是moduleName(.jar,.war)
    		final String appName = "";
    		final String moduleName = "EJB_01";
    		final String disctinctName = "";
    		//实现类名
    		final String beanName = "HelloWorldBean";
    		final String viewClassName = HelloWorldRemote.class.getName();
    		
    		String jndi = "ejb:" + appName + "/" + moduleName + "/"
                + disctinctName + "/" + beanName + "!" + viewClassName;
    		
    		HelloWorldRemote hwr = (HelloWorldRemote) context.lookup(jndi);
    		out.println(hwr.sayHello("Manfred"));
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
     %>
  </body>
</html>