<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.rmi.PortableRemoteObject"%>
<%@ page import="test.*"%>
<html>
<head>
<title>TestPage</title>
</head>
<body bgcolor="#ffffff">
<%
Context context = null;
app app1 = null;
//获取Context
try
{
String url = "t3://192.168.1.163:7001";
String user = null;
String password = null;
Properties properties = null;
out.println("debug | ready to set property ... <br/>"); properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null)
{
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
//
out.println("debug | ready to create context ... <br/>");
context = new InitialContext(properties);
}
catch (Exception ex)
{
out.println("<br/>创建Context对象出错:" + ex);
return;
} //查找Home接口,创建对象引用
try
{
//look up jndi name
Object ref = context.lookup("app");
//look up jndi name and cast to Home interface
appHome Home = (appHome) PortableRemoteObject.narrow(ref, appHome.class); //
app1 = Home.create();
}
catch (Exception ex)
{
out.println("<br/>查找Home接口出错:" + ex);
return;
} //调用EJB对象
try
{
if (app1 == null)
throw new Exception("EJBObject is null.");
String msg = app1.sayHello("hello world!");
out.println(msg);
}
catch (Exception ex)
{
out.println("调用EJB对象出错:" + ex);
}
%></body>
</html>
<%@ page import="java.util.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.rmi.PortableRemoteObject"%>
<%@ page import="test.*"%>
<html>
<head>
<title>TestPage</title>
</head>
<body bgcolor="#ffffff">
<%
Context context = null;
app app1 = null;
//获取Context
try
{
String url = "t3://192.168.1.163:7001";
String user = null;
String password = null;
Properties properties = null;
out.println("debug | ready to set property ... <br/>"); properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null)
{
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
//
out.println("debug | ready to create context ... <br/>");
context = new InitialContext(properties);
}
catch (Exception ex)
{
out.println("<br/>创建Context对象出错:" + ex);
return;
} //查找Home接口,创建对象引用
try
{
//look up jndi name
Object ref = context.lookup("app");
//look up jndi name and cast to Home interface
appHome Home = (appHome) PortableRemoteObject.narrow(ref, appHome.class); //
app1 = Home.create();
}
catch (Exception ex)
{
out.println("<br/>查找Home接口出错:" + ex);
return;
} //调用EJB对象
try
{
if (app1 == null)
throw new Exception("EJBObject is null.");
String msg = app1.sayHello("hello world!");
out.println(msg);
}
catch (Exception ex)
{
out.println("调用EJB对象出错:" + ex);
}
%></body>
</html>
谢谢!!
用static还可以在整个jsp/servlet中共享Home接口