Java > Java Servlets sample source codes
Access an ejb from a servlet
Access an ejb from a servlet package com.ack.web.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ack.j2ee.ejb.session.Lawyer; import com.ack.j2ee.ejb.session.LawyerHome; /** * web.xml configuration part of the an ejb component * *
...
yoda
Session
com.ack.j2ee.ejb.session.LawyerHome
com.ack.j2ee.ejb.session.Lawyer
...
* * the vendor-specific mapping of the res-ref-name into their * own application server space, eg weblogic does the following * in a weblogic.xml file *
...
...
yoda
ejb/lawyer
...
...
*/ public class AccessAnEjbFromAServlet extends HttpServlet { private Context ctx; public void init() throws ServletException { try { // lets share the context for ejb lookup for // this servlet's incoming requests ctx = new InitialContext(); } catch( NamingException nex ) { throw new ServletException( "couldn't locate JNDI context", nex ); } } public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { res.setContentType( "text/html" ); PrintWriter pw = res.getWriter(); try { // get reference to business interface from home interface Lawyer lawyer = getLawyer(); // use the business interface lawyer.sendHimDown( "bad guy" ); // let application server reclaim ejb resources lawyer.remove(); } catch( LawyerException lex ) { pw.println( lex.getMessage() ); } catch( Exception ex ) { log( "problem using the lawyer", ex ); pw.println( ex.getMessage() ); } pw.println( "well, that wasn't so bad" ); } private Lawyer getLawyer() throws LawyerException { try { Object ejbObject = null; // get hold of the object you want by name synchronized( this ) { System.out.println( "calling on yoda..." ); ejbObject = ctx.lookup( "java:comp/env/yoda" ); } // narrow retrieved object into specific expected type LawyerHome home = (LawyerHome) PortableRemoteObject. narrow( ejbObject, LawyerHome.class ); return home.create(); } catch( Exception ex ) { log( "problem getting hold of a lawyer", ex ); throw new LawyerException( ex.getMessage() ); } } } class LawyerException extends Exception { public LawyerException( String str ) { super( str ); } } /** * * This is the web.xml configuration *
...
bring forth the yoda lawyer
yoda
Session
com.ack.j2ee.ejb.session.LawyerHome
com.ack.j2ee.ejb.session.Lawyer
* * But the vendor-specific xml file must map this * ejb-ref-name to the name in the JNDI space, for * example, in weblogic we have the weblogic.xml, *
yoda
ejb/lawyer
*/
Java Codes
Beginners
Core Java
Date Time
Java2D
Java Applets
Java AWT
Mathematics
Networking
Servlets
Session
Sound
Swing
Threads
Util Package
JDBC
Learning
Other
Security
XML