Quick followup. I tried similar JSP in Tomcat 8.0.27, and wow! @Resource 
injection into JSP worked like a charm!

context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/foo" privileged="true">
    <Environment name="foo" value="42" type="java.lang.Integer" 
override="false"/>
</Context>

JSP:

<%!
    @Resource(name = "foo")
    private Integer foo;
%>
<html>
 <body>
  foo = <%= foo %>
 </body>
</html>

This successfully rendered foo = 42.

Things were much more complicated in TomEE. I couldn't find a way to make 
@Resource work in JSP at all.
Whether a resource was defined in WEB-INF/resources.xml, or was it something 
like ManagedExecutorService - @Resource didn't work either.
In the case of ManagedExecutorService, I was able to lookup it manually in JNDI 
(as java:comp/DefaultManagedExecutorService).
In the case of resources.xml-defined entry, I didn't manage to resolve it even 
through JNDI manually.

In Tomcat, on the contrary, user-defined resources are resolvable in JSP both 
via @Resource injection and manual JNDI lookup.

I'm using TomeEE 7.0.0-M3. Didn't try 1.7.4 though.

Dimitri

> > 
> > (that's
> > why hot reloading is a good bad idea and can break after a restart
> > even if "F5" tests were green ;)).
> Oh yeah, I've got obscure errors after hot reloading hundreds of
> times.
> Most of them were JNDI-related, by the way ;)
> 
> > 
> > Hmm, maybe not. JSP support injections and are generated at runtime
> > so
> > it needs some glue code but reusing the same principle would work
> > and
> > this API even if internal is more stable (see InstanceManager of
> > tomcat).
> That's it - JSPs came to my mind even before the idea of generating
> annotated classes. Needless to say I did some testing...
> 
> =============================== 8<
> ===================================
> <%@page import="javax.enterprise.inject.spi.BeanManager"%>
> <%@page import="javax.inject.Inject"%>
> <%@page import="java.util.logging.Logger"%>
> <%@page import="javax.transaction.UserTransaction"%>
> <%@page import="javax.annotation.Resource"%>
> <%@page import="javax.annotation.PostConstruct"%>
> 
> <%@page contentType="text/html" pageEncoding="UTF-8"%>
> 
> <%!
>     @Resource
>     private UserTransaction tx;
>     
>     @Inject
>     private BeanManager bm;
>     
>     private final static Logger LOG = Logger.getLogger("foo.jsp");
> 
>     @PostConstruct
>     public void post() {
>         LOG.info("foo.jsp::post()");
>         LOG.info("tx = " + tx);
>         LOG.info("bm = " + bm);
>     }
> %>
> 
> <%
>     request.setAttribute("tx", tx);
>     request.setAttribute("bm", bm);
> %>
> 
> <!DOCTYPE html>
> <html>
>     <head>
>         <meta http-equiv="Content-Type" content="text/html;
> charset=UTF-8">
>         <title>JSP Page</title>
>     </head>
>     <body>
>         tx = ${requestScope.tx}<br>
>         bm = ${requestScope.bm}<br>
>     </body>
> </html>
> =============================== 8<
> ===================================
> 
> The results were, errr, a bit disappointing.
> 
> TomEE 7.0.0-M3
> ==============
> tx = null
> bm = org.apache.webbeans.container.InjectableBeanManager@b4ecb2
> @PostConstruct: invoked
> 
> WildFly 10.0.0
> ==============
> tx = null
> bm = null
> @PostConstruct: not invoked
> 
> GlassFish/Payara 4.1.1
> ======================
> bm = (had to remove BeanManager since JSP didn't compile)
> tx = null
> @PostConstruct: not invoked
> 
> Is it another blind-spot in the spec? or is it a bug? or am I doing
> it
> totally wrong?
> 
> Dimitri

Reply via email to