try
{
InitialContext ic = new InitialContext();
XXXLocalHome home = (XXXLocalHome) ic.lookup("jndiHomeName");
}
catch(NamingException ex)
{
}
{
InitialContext ic = new InitialContext();
XXXLocalHome home = (XXXLocalHome) ic.lookup("jndiHomeName");
}
catch(NamingException ex)
{
}
import java.util.Iterator;
import java.util.Properties;
import java.util.ArrayList;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.List;
import menutree_server.*;public class MenugetContext { public MenugetContext() { } public Iterator getMenuList(){
List list = new ArrayList();
try{
Context context = getInitialContext();
Object object = context.lookup("MenuSession");
//menuSessionHome = (MenuSessionHome) PortableRemoteObject.narrow(ref, MenuSessionHome.class);
//InitialContext initialContext=new InitialContext();
//Object object=initialContext.lookup("MenuSession");
MenuSessionHome msh=(MenuSessionHome)PortableRemoteObject.narrow(object,MenuSessionHome.class);
MenuSession ms=msh.create();
Collection collection = ms.getAllMenu();
Iterator iterator = collection.iterator();
for(;iterator.hasNext();){
menutree_server.Menu menu = (menutree_server.Menu)iterator.next();
MenuObject mo = new MenuObject();
System.out.println(menu.id+" "+menu.fid+" "+menu.xh+" "+menu.name);
mo.setId(menu.id);
mo.setFid(menu.fid);
mo.setXh(menu.xh);
mo.setName(menu.name);
mo.setUrl(menu.url);
mo.setNotes(menu.notes);
list.add(mo);
}
}
catch(javax.naming.NamingException namingException){
namingException.printStackTrace();
}
catch(javax.ejb.CreateException createException){
createException.printStackTrace();
}
catch(java.rmi.RemoteException remoteException) {
remoteException.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
}
return list.iterator(); } private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = "user";
String password = "password";
Properties properties = null;
try {
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);
} return new InitialContext(properties);
}
catch(Exception e) {
//log("Unable to connect to WebLogic server at " + url);
//log("Please make sure that the server is running.");
throw e;
}
}}
InitialContext ctx = null;
Object obj = null;
try {
ctx = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
try {
obj = ctx.lookup("java:comp/env/ejb/OrderSessionFacadeRef");
} catch (NamingException e) {
e.printStackTrace();
}
OrderSessionFacadeHome facadeHome =
(OrderSessionFacadeHome) PortableRemoteObject.narrow(
obj,
OrderSessionFacadeHome.class); }
<%@page import= "nethr.*"%>
<%@page import= "javax.rmi.PortableRemoteObject"%>
<%@page import= "java.util.Vector"%>
<%@page contentType="text/html; charset=Gb2312" %>
<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js"></script>
<html>
<head>
<title>
menutree
</title>
</head><% FacadeSsnHome facadeSsnHome = null;
FacadeSsn facade = null; Vector v = new Vector();
Context ctx = new InitialContext();
Object ref = ctx.lookup("FacadeSsn");
facadeSsnHome = (FacadeSsnHome) PortableRemoteObject.narrow(ref,
FacadeSsnHome.class);
FacadeSsn obj = facadeSsnHome.create();
v = obj.getMenrtree(); //调用sessionbean中的方法
int vFieldCount = obj.getFieldCount();
%>
.......package nethr;import javax.ejb.*;
import javax.naming.*;
import java.util.Vector;
import java.util.Collection;
import java.rmi.RemoteException;
import java.util.Iterator;public class FacadeSsnBean
implements SessionBean {
final int nFieldCount=3;
SessionContext sessionContext;
public Menutree mtree = null;
private MenutreeHome mtHome = null;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
try {
Context ctx = new InitialContext();
Object ref = ctx.lookup("java:/comp/env/menutree");
mtHome = (MenutreeHome) ref;
}
catch (Exception ex) {
ex.printStackTrace();
}
} public void ejbRemove() {
/**@todo Complete this method*/
} public void ejbActivate() {
/**@todo Complete this method*/
try {
Context ctx = new InitialContext();
Object ref = ctx.lookup("java:/comp/env/menutree");
mtHome = (MenutreeHome) ref;
}
catch (Exception ex) {
ex.printStackTrace();
} } public void ejbPassivate() {
/**@todo Complete this method*/
} public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
} public java.util.Vector getMenrtree() {
/**@todo Complete this method*/
Menutree mt = null;
Vector v = new Vector();
try{
Iterator i = mtHome.findAll().iterator();
while(i.hasNext()){
mt = (Menutree)i.next();
v.add(mt.getCiId());
v.add(mt.getCiPid());
v.add(mt.getCcName());
//v.add(mt.getCcUrl());
//v.add(mt.getCcTitle());
//v.add(mt.getCcTarget());
//v.add(mt.getCcIcon());
//v.add(mt.getCcIconopen());
//v.add(mt.getCiOpen());
}
Debug.print("getMenrtree");
}
catch(Exception ex){
ex.printStackTrace();
}
return v; }
public int getFieldCount() {
/**@todo Complete this method*/
return nFieldCount;
}
}
为什么要用JSP调用SESSION BEAN呢,这样做是很不可取的!偶正在做这方面的应用,想知道怎么不可取??