Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Servlets Code Examples

Access an ejb from a servlet

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
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 * * <web-app> ... <ejb-ref> <ejb-ref-name>yoda</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>com.ack.j2ee.ejb.session.LawyerHome</home> <remote>com.ack.j2ee.ejb.session.Lawyer</remote> </ejb-ref> ... </web-app> * * 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 * <weblogic-web-app> ... <reference-descriptor> ... <ejb-reference-description> <ejb-ref-name>yoda</ejb-ref-name> <jndi-name>ejb/lawyer</jndi-name> </ejb-reference-description> ... </reference-descriptor> ... </weblogic-web-app> */ 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 * <web-app> ... <ejb-ref> <description>bring forth the yoda lawyer</description> <ejb-ref-name>yoda</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>com.ack.j2ee.ejb.session.LawyerHome</home> <remote>com.ack.j2ee.ejb.session.Lawyer</remote> </ejb-ref> </web-app> * * 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, * <weblogic-web-app> <reference-descriptor> <ejb-reference-description> <ejb-ref-name>yoda</ejb-ref-name> <jndi-name>ejb/lawyer</jndi-name> </ejb-reference-description> </reference-descriptor> </weblogic-web-app> */