geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <david.blev...@visi.com>
Subject Re: Injecting EJB directly into a JSP
Date Wed, 02 Jul 2008 19:22:08 GMT

On Jul 2, 2008, at 11:27 AM, purdticker wrote:

>
> So what's the recommended solution to pulling data from database and  
> giving
> that ResultSet to jsp?  Currently, I'm using struts, I'd like to be  
> able to
> access my datasource from the action class and throw it in
> request.setAttribute().
>
> However, I can't inject ejb into action class. I also can't seem to  
> inject
> the datasource into action class...
>
> 	@Resource(name="jdbc/EBSDS")
> 	DataSource ds;
>
> So how do I access my datasource from action class?  If geronimo  
> doesn't
> support this, what is recommended method?  I can't just create  
> servlets for
> everything.

One easy trick is to create a single servlet class and annotate it  
with all the things you need.  Since all injected resources also wind  
up in JNDI and everything in the webapp shares the same JNDI context,  
you can use it as a clever way to declare resources that you need in  
your JSPs.

For example, with the @Resource usage you have above, you can lookup  
via JNDI in your JSP as follows:

   InitialContext context = new InitialContext();

   DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/ 
EBSDS");


-David


Mime
View raw message