servlet 调用 EJB 经典实例!

/*    文件结构:



EJB端:

    EJBFolderName

          |

          |------classes  

          |            |

          |            |-------hello

          |            |           |---------hello.class             //Remote 

          |            |           |---------helloHome.class    //Home

          |            |           | ---------helloBean.class    //Bean 

          |            |

          |            |--------META-INF

          |                           |----------------ejb-jar.xml           //EJB配置文件

            |                           |----------------MANIFEST.MF  //Manifest-Version: 1.0           Class-Path: 

          |  

          |  

          |-----------src 

                       |

                       |-------hello

                       |           |---------hello.java             //Remote 

                       |           |---------helloHome.java    //Home

                       |           | ---------helloBean.java    //Bean 

                       |

                       |--------META-INF

                                     |----------------ejb-jar.xml           //servlet配置文件

                                     |----------------MANIFEST.MF  //Manifest-Version: 1.0           Class-Path: 



servlete端:



ServletFolderName

          |

          |----------src

          |            |-------hello

          |            |          |--------TestEJB.java  //被Servlet(只为目标清晰,也可直接写到Servlet内)

          |            |

          |            |-------TestEJBServlet.java  //一个Servlet  调用 包hello 中的方法。

          |

          |

          |----------WebRoot

                           |----------META-INF

                           |                  |--------MANIFEST.MF    //Manifest-Version: 1.0       Class-Path: 

                           |

                           |----------WEB-INF

                                            |----------classes

                                            |               |-----------TestEJBServlet.class

                                            |               | 

                                            |               | -----------hello    

                                            |                               |----------TestEJB.class

                                            |-----------lib               

                                            |            |----------FirstEJB.jar   //EJB打好的一个Jar包!EJB2.0不可缺少   

                                            |                            

                                            |------------web.xml   //Servlet配置文件              

基本文件结构就是这样。结束!          

*/
//



//TestEJBServlet.java:

import java.io.IOException;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



import hello.TestEJB;

public class TestEJBServlet extends HttpServlet {



	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {

		this.doPost(request,response);  //直接以页面的形式调用Servlet 调用的事doGet方法,

                                                                                            //也可以直接在doGet方法中调用EJB

	}

         public void doPost(HttpServletRequest request, HttpServletResponse response){

         		try{

			new TestEJB("iiop://localhost:2809/").test();  //调用 TestEJB.java 中的test()方法。

		} catch(Exception e){

			e.printStackTrace();

		}



          }

}
//TestEJB.java:
package hello;

import javax.naming.Context;

import java.util.Properties;

import javax.naming.NamingException;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import java.rmi.RemoteException;

import javax.ejb.CreateException;



public class TestEJB {



	private static final String JNDI_NAME = "HelloEJB";  //EJB上传websphere时用的JNDI Name

	private String url;

	private HelloHome home;

	public TestEJB(String url) throws NamingException{

		

		this.url = url;

		home = lookUpHome();

	}

	

	private Context getInitialContext() throws NamingException {

		Properties prop = new Properties();

	   prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");

		prop.put(Context.PROVIDER_URL, url);

		return new InitialContext(prop);

	}

	private HelloHome lookUpHome() throws NamingException{

		

		Context ctx = getInitialContext();

		Object home = ctx.lookup(JNDI_NAME);

		return (HelloHome)PortableRemoteObject.narrow(home,HelloHome.class);

	}

	

	public void test() throws RemoteException,CreateException,NamingException {

		

		Hello hello = (Hello)PortableRemoteObject.narrow(home.create(),Hello.class);

		

		System.out.println(hello.sayHello());

	}

	

	/*public static void main(String args[]) throws Exception{

		try{

		new TestEJB("iiop://localhost:2809/").test();

		}catch(Exception e){

			e.printStackTrace();

		}

	}*/

}
//Servlet 配置文件 web.xml




	

		TestEJB   

		TestEJBServlet 

	

	

		TestEJBServlet  //Servlet映射到web服务器上的名字

		/servlet/TestEJBServlet//Servlet映射到web服务器上的路径

	

  

    index.jsp//默认开始页面(可有可无)

  

 
 
  
helloHome.java(就是Home.java  本地接口)
 
  
package hello;

import java.rmi.RemoteException;

import javax.ejb.CreateException;

import javax.ejb.EJBHome;



public interface HelloHome extends EJBHome {



	public Hello create()throws CreateException,RemoteException;

}

hello.java(就是Remote.java 远程接口)
 
  
package hello;



import java.rmi.RemoteException;



import javax.ejb.EJBObject;



public interface Hello extends EJBObject {

	public String sayHello()throws RemoteException;

}
helloBean.java(就是Bean.java 实现类)
package hello;



import java.rmi.RemoteException;



import javax.ejb.EJBException;

import javax.ejb.CreateException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;



public class HelloBean implements SessionBean {



	private SessionContext ctx;

	private String words ;

	

	public String sayHello(){

		

		System.out.println("i am in an EJB server."+words);

		return words;

	}

	

	public void ejbCreate() throws CreateException{

		this.words = "hello world";

	}



	public void ejbActivate() throws EJBException, RemoteException {

		// TODO Auto-generated method stub



	}



	public void ejbPassivate() throws EJBException, RemoteException {

		// TODO Auto-generated method stub



	}



	public void ejbRemove() throws EJBException, RemoteException {

		// TODO Auto-generated method stub



	}



	public void setSessionContext(SessionContext arg0) throws EJBException,

			RemoteException {

		this.ctx = arg0;

	}



}
还有一个配置文件ejb-jar.xml
 
  






	HelloEjb

	

		

			FirstEJB

			hello.HelloHome

			hello.Hello

			hello.HelloBean

			Stateless

			Container

		

	

	

		

			

				FirstEJB

				Remote

				*

			

			Required

		

	


调用方法:Http://localhost:9080/Context(上下文节点)/servlet/TestEJBServlet

你可能感兴趣的:(j2ee,servlet,ejb,exception,string,properties,websphere)