on 07-17-2008 11:01 PM
Hello,
How can I do a dependency injection of and EJB into a jsp?
When Iu2019m trying to used it (see below), It doesnu2019t work, What Iu2019m doing wrong?
Also I tried to do a lookup, but I canu2019t do a cast because the classe returned by the lookup is some Proxy class and the only way that Iu2019ve find to access the EJBu2019s methods is through reflection.
Regards,
Janeth
@EJB (name = "crystal.com.co/captura_produccion_ear/CapturaPrimeraEjbBean")
private CapturaPrimeraEjbRemote capPrimeraEjbRemote;
if (capPrimeraEjbRemote != null) {
capPrimeraEjbRemote.test();
}
else{
out.println("null");
}
Hi,
injections are not supported in jsp template or tag files. Please check section 7.1.11 from the jsp spec.
The general rule is that injections are supported for container managed objects which binary format is available at deploy time.
In JSP you can have injections in custom tag handlers or event listeners. This case is also described in the above cited section.
Best Regards
Nikolai Dokovski
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Janeth,
Nikolai is absolutely right about injection in JSP. However, you can still use JNDI lookup. The proxy class you get actually implements the EJB interface, so the cast should work (provided you have classloading stuff set up correctly).
HTH!
\-- Vladimir
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank both for the answer, about the lookup:
Vladimir you are right about implementation, actually I print trough reflection the interfaces of Proxy class, and I obtain this:
co.com.crystal.eficiencia.primera.CapturaPrimeraEjbRemote
javax.ejb.EJBObject
com.sap.engine.services.ejb3.runtime.ComponentInterface com.sap.engine.services.ejb3.runtime.ReplaceableProxy
The common code is:
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sap.engine.services.jndi.InitialContextFactoryImpl");
props.put(Context.PROVIDER_URL, "localhost:50004");
InitialContext ctx = new InitialContext(props);
Object object = ctx.lookup("crystal.com.co/captura_produccion_ear/REMOTE/CapturaPrimeraEjbBean/co.com.crystal.eficiencia.primera.CapturaPrimeraEjbRemote");
out.println(object.getClass());
If I do injection, itu2019s work correctly:
Method findAll = object.getClass().getMethod("test", null);
String listaTurnos = (String) findAll.invoke(object, null);
out.print(listaTurnos);
But if I use narrow or cast, it doesnu2019t work (I get a java.lang.ClassCastException):
co.com.crystal.eficiencia.primera.CapturaPrimeraEjbRemote capPrimera = (co.com.crystal.eficiencia.primera.CapturaPrimeraEjbRemote)PortableRemoteObject.narrow(object, co.com.crystal.eficiencia.primera.CapturaPrimeraEjbRemote.class);
String listaTurnos = capPrimera.test
out.print(listaTurnos);
or the cast:
co.com.crystal.eficiencia.primera.CapturaPrimeraEjbRemote capPrimera = (co.com.crystal.eficiencia.primera.CapturaPrimeraEjbRemote)object;
What I have to do to check the correctly way to do the cast or the narrow? What Iu2019m doing wrong?
Thanks
Janeth
Hi Janeth,
Couple of points regarding your code snippets first:
1. When looking up an EJB in a JSP it's recommended to declare an ejb-ref or ejb-local-ref in the web.xml and look up from the "java:comp/env/" ENC of the web app.
2. If the EJB is running on the same server, you should use local interfaces. You also don't have to provide any props when creating the InitialContext.
Now, if the JSP and the EJB are not part of the same EAR app, you should declare a reference from the JSP app to the EJB app in order to avoid such ClassCastExceptions. This is maintained in the application-j2ee-engine.xml.
HTH!
\-- Vladimir
Hi Vladimir,
Again thank you.
How you can see Iu2019m new in this World, so, I will thank you if you can show me some tutorial or some kind of documentation about it (if you canu2019t, donu2019t worry)
I will try to practice your recommendation and then I will tell you the results.
Regards Janeth
User | Count |
---|---|
88 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.