CDI injection in a JSP

Within a JSP it is possible to use CDI managed beans using EL expressions such as ${myBean.myAttribute}. No problem here.

I would like to use "regular injection" (i.e. without using EL expressions) with @Inject in JSP files, for example : <%! @Inject MyBean myBean; %> then later <%= myBean.getMyAttribute() %>. Even if that example can be achieved using EL expressions, some other use cases cannot.

This does not seem to be completely supported by app servers:
- JBoss 6.0.0, JBoss 6.1.0, Resin 4.0.22: OK, it works just fine.
- JBoss 7.0.1, GlassFish 3.x (several versions were tested): FAILS, myBean remains null.

It should work fine in a JSP since :
(1) it works fine in servlets according to the various concerned specifications and
(2) JSP get translated to servlet during runtime.

Do you guys know if what I'm trying to do is supported or not? Any inside/implementation information maybe?

Best regards.

Thank you for your time.

-------------Problems Reply------------

Interesting question, if you hadn't been testing it, I would have bet some money on the fact the it does not work ;-)

CDI builds on managed beans (JSR 316). The corresponding definition is pretty relaxed (on purpose):

From the spec:

A Managed Bean can be declared by annotating its class with the javax.annotation.ManagedBean annotation. A Managed Bean must not be: a final class, an abstract class, a non-static inner class. A Managed Bean may not be serializable, unlike a regular JavaBean component.

In the basic component model, Managed Beans must provide a no-argument constructor, but a specification that builds on Managed Beans, such as CDI (JSR-299), can relax that requirement and allow Managed Beans to provide constructors with more complex signatures,

What's probably happening is that the container scans the classpath and happens to find the compiled JSP servlets. It's a while since I last saw one, but I remember that the code is generated and everything (including scriptlets) lands in doGet() or doPost()...!? So, even though they formally do not disqualify in terms of the definition, I doubt that a JSP scriplet is anything that you want to consider a managed bean. It feels terribly wrong, honestly ;-)

I'm following the CDI / Weld / Seam mailing lists since quite a while, and don't recall that JSP has ever been mentioned. Same with googling this connection.

As a consequence you should not rely on CDI working with scriptlets. IMHO this behaviour has more of a side effect than of something intenional and can be dropped in future releases without notice (or even without being noticed :-)

So, +1 for the proposal of JB Nizet: Use servlets with CDI, but not JSPs.

UPDATE: I tried to help, not to create confusion ;-) My point is: IMHO it feels really really wrong to use CDI in JSPs, but I did not find anything in the relevant specs that proves this. All I can say is that JSPs are never mentioned anywhere - which kind of supports my gut feeling (and fits the observation that some implementations do consider it, others don't).

I don't think there is a portable @Inject available out-of-box for JSP, but it should be possible to implement it (at the container level) the same way it works with servlets.

And while I agree that's it not the best way to utilize CDI, technically I don't see any reason against it. For example, AFAIK @Inject in servlets transparently uses ThreadLocal proxies, why not use this feature in JSP's?

Category:jsp Views:0 Time:2011-09-12

Related post

  • CDI Injection Outside Filters and Servlets in a Servlet 3.0 Container 2011-08-20

    I finally decided to have a look at Weld in Tomcat. When I deploy my app I see in the log: "Tomcat 7 detected, CDI injection will be available in Servlets and Filters" How can for example create an instance of a bean using the BeanManager outside a F

  • CDI Injection of an EJB leads to NullPointerException 2011-11-17

    I am new to Java EE 6 and CDI. I have read a couple of tutorials and the weld documentation. However something that should work from my understanding doesn't so I need help. I have the following situation. I created a Java EE 6 Application with NetBe

  • CDI Injection in Hibernate Entities 2011-12-14

    We are using CDI(JSR 299) in our application (JSF2/Seam3.0/Hibernate 3.5.6/GlassFish 3.1.1) While we are unable to inject resources(Helper POJOs) in our managed beans using @Inject, we cannot do the same in our Hibernate Entity classes. We have a bas

  • Using CDI Injection in a Servlet 2012-03-07

    I am attempting to @Inject a @SessionScoped bean into a Filter @WebFilter("/*") public class IdentityFilter implements Filter, Serializable { @Inject private LoginUser loginUser; ... where LoginUser is @SessionScoped The intention is for loginUser to

  • CDI inject EJB into POJO on Glassfish v3 2011-11-17

    Is it possible to inject EJB 3.1 beans into POJO using CDI on Glassfish v3? My classes (in EJB module): @Singleton @LocalBean @Startup @Named public class NewSingletonBean { @PostConstruct public void init(){ System.out.println("NewSingletonBean INIT

  • When CDI injection into POJO should work? (GlassFish v3) 2011-11-17

    When I inject EJB 3.1 beans into POJO created by @Inject then injection works. When I construct POJO on my own then it doesn't (Glassfish v3). Is it correct behavior? My classes (in EJB module): @Singleton @LocalBean @Startup @Named public class NewS

  • Lib to protect SQL/javascript injection for java/jsp 2008-12-08

    Anyone know a good lib where i can run the strings before they are inserted, that can strip out sql/javascript code? To be run in jsp pages. Idealy the lib would be: Free Lightweight Easy to use Thanks in advance to the SO community who will happily

  • Is dependency injection possible for JSP beans? 2010-04-30

    This may be a long shot question.. I am working on an application that is based on JSP/Javascript only (without a Web framework!) Is there a way to have depencency injection for JSP beans? By jsp beans I mean beans defined like this <jsp:useBean i

  • Injecting a Spring bean using CDI @Inject 2010-11-10

    I'm trying to inject a bean defined in a Spring context into a CDI managed component but I'm not successful. The bean is not injected, instead a new instance gets created each time the injection should be performed. My environment is Tomcat 7 with JB

  • can I use CDI injection into quartz-scheduler jobs? 2011-01-27

    I'm using Glassfish and CDI for injection, (mostly) successfully. I can't seem to get Quartz jobs to work with injection- beans annotated with @Inject never get injected. Is Quartz using some kind of different classloader that's preventing injection

  • Why does CDI injection fail to work in a some modules, but not in others? 2011-07-28

    In my Java EE project, there are several "Java EE" modules and a web module. One of the Java EE modules provides a class to CDI that is to be used by the other modules: @ApplicationScoped public class XFactory { @Produces @Actual public X create() {

  • CDI injection in JAX-WS endpoint does not work, results in NPE 2011-12-15

    Why doesn't the following CDI work in JAX-WS endpoints in glassfish 3.x.x? I get an NPE when accessing the service from the endpoint. @WebService public class JaxWsTestEndpoint { @Inject private MyService service; @WebMethod public String sayHello(St

  • @RequestScoped CDI injection into @MessageDriven bean 2011-12-15

    If I have a request scoped CDI bean injected into a @MessageDriven EJB using JMS, as below, can I assume that any given Foo instance will only be used by a single onMessage invocation at a time? In other words, in the below example, can I safely use

  • CDI @inject null pointer 2012-01-13

    i'm trying to use CDI but the injection doesn't work, the object instance is not created and i'm getting a null pointer on this: greetObj.greet("Champion"); I'm trying to Inject a bean in an EJB: my EJB: public @Stateless class CDIEjbBean implements

  • CDI Injection Within A Constructor 2012-02-28

    I've got my application setup to use CDI and all is going well. Now I'm creating a new bean that extends a class from a 3rd party library. I attempted to create something like the below example: @Named("myNewClass") @ConversationScoped public class M

  • CDI - Injecting Classes at runtime 2012-03-09

    I'm working on a project, where it is needed to load some classes at runtime. The classes to load are parts of CDI-Containers and have to be able to inject some stuff. The "loading class" itself is a part of a CDI-Container as well. Now comes my prob

  • JPA CDI Injecting DAO into an Entity 2012-04-18

    I'm new to JPA and CDI and I'm trying to create an enterprise application using these frameworks. I get how I can inject into beans and keep everything tidy and stateless. I also get that JPA loads relations etc. for me so that I don't have to worry

  • JSF2 Managed Bean Reference Problem: CDI Injection? 2011-07-29

    I have a managed bean called: @ManagedBean(name="configBean") @SessionScoped public class configBean implements Serializable { that instantiates a class/bean (that isn't a managed bean its a standard class): com.package.class variableName = new com.p

  • JBoss Weld CDI : Inject the same instance in two different Objects 2011-12-14

    I have two basis class A and B. B is injected in A. I have a third class C injected in A and B, as follow : class A { @Inject B b; @Inject C c; } class B { @Inject C c; } class C { } I'd like the instance of C contained in A and in B is the same. I c

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.130 (s). 11 q(s)