cancel
Showing results for 
Search instead for 
Did you mean: 

Dependecy injection EJB into a jsp

Former Member
0 Kudos

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");
	}

Accepted Solutions (1)

Accepted Solutions (1)

NikiD
Employee
Employee
0 Kudos

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

Answers (1)

Answers (1)

Vlado
Advisor
Advisor
0 Kudos

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

Former Member
0 Kudos

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

Vlado
Advisor
Advisor
0 Kudos

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

Former Member
0 Kudos

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