You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Adam Connors <ad...@cognima.com> on 2002/08/20 11:15:56 UTC

How to put things in the ServletContext

Hello,

Apologies if this is an obvious one I've missed, (I'm a newbie and a bit bewildered by the onslaught of new information). But I want to put the home interface for an ejb into the servlet context to save time with multiple jndi lookups.

Traditionally this is easily done in the servlet init() method and by making it a static member of the servlet. But I don't have access to this anymore since everything goes through the action servlet.

Is there a way of putting stuff into the init() method of the action servlet without faffing around with subclassing it?

I've found the DataSource and connection pool stuff, is this what I need? Can I have application context without the pool? Would there be any consequences of creating a pool with just one entry?

Many thanks for your help,

Adam.

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: How to put things in the ServletContext

Posted by Jonas Bjornerstedt <jo...@bjornerstedt.org>.
Given the recent discussion of Struts and connection pooling, it might be a
good idea to include a connection pool example as an application.

Jonas

> -----Original Message-----
> From: Craig R. McClanahan [mailto:craigmcc@apache.org]
> Sent: den 20 augusti 2002 20:24
> To: Struts Users Mailing List
> Subject: RE: How to put things in the ServletContext
>
>
>
>
> On Tue, 20 Aug 2002, Robert Taylor wrote:
>
> > Date: Tue, 20 Aug 2002 05:33:09 -0400
> > From: Robert Taylor <rt...@mulework.com>
> > Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> > To: Struts Users Mailing List <st...@jakarta.apache.org>
> > Subject: RE: How to put things in the ServletContext
> >
> > Adam, take a look at javax.servlet.ServletContextListener. You
> have access
> > to ServletContext via the ServletContextEvent in contextInitialized().
> >
>
> ServletContextListener is designed for exactly this sort of thing, but
> requires a Servlet 2.3 container.  If you're stuck on 2.2, Struts 1.1
> includes a new PlugIn facility that is designed to "plug things in" to the
> init() and destroy() methods without having to subclass ActionServlet.
>
> The Struts example app in 1.1 demonstrates how the API is used, by using a
> plugin to load its pseudo-database.
>
> Craig
>
>
> >
> >
> >      /**
> > 	 * Called just after the web application has been initialized
> > 	 *
> > 	 * @param event the ServletContextEvent containing the
> ServletContext
> > 	 */
> > 	public void contextInitialized(ServletContextEvent event) {
> >
> > 		ServletContext context = event.getServletContext();
> > 		Object o = //get home interface
> >
> >
> > 		context.setAttribute("someName", o);
> >
> >             // other stuff here
> > 	}
> >
> >
> > HTH,
> >
> > robert
> >
> >
> > > -----Original Message-----
> > > From: Adam Connors [mailto:adam.connors@cognima.com]
> > > Sent: Tuesday, August 20, 2002 5:16 AM
> > > To: struts-user@jakarta.apache.org
> > > Subject: How to put things in the ServletContext
> > >
> > >
> > > Hello,
> > >
> > > Apologies if this is an obvious one I've missed, (I'm a newbie
> > > and a bit bewildered by the onslaught of new information). But I
> > > want to put the home interface for an ejb into the servlet
> > > context to save time with multiple jndi lookups.
> > >
> > > Traditionally this is easily done in the servlet init() method
> > > and by making it a static member of the servlet. But I don't have
> > > access to this anymore since everything goes through the
> action servlet.
> > >
> > > Is there a way of putting stuff into the init() method of the
> > > action servlet without faffing around with subclassing it?
> > >
> > > I've found the DataSource and connection pool stuff, is this what
> > > I need? Can I have application context without the pool? Would
> > > there be any consequences of creating a pool with just one entry?
> > >
> > > Many thanks for your help,
> > >
> > > Adam.
> > >
> > > --
> > > To unsubscribe, e-mail:
> > > <ma...@jakarta.apache.org>
> > > For additional commands, e-mail:
> > > <ma...@jakarta.apache.org>
> > >
> >
> >
> > --
> > To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> >
> >
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: How to put things in the ServletContext

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 20 Aug 2002, Robert Taylor wrote:

> Date: Tue, 20 Aug 2002 05:33:09 -0400
> From: Robert Taylor <rt...@mulework.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: RE: How to put things in the ServletContext
>
> Adam, take a look at javax.servlet.ServletContextListener. You have access
> to ServletContext via the ServletContextEvent in contextInitialized().
>

ServletContextListener is designed for exactly this sort of thing, but
requires a Servlet 2.3 container.  If you're stuck on 2.2, Struts 1.1
includes a new PlugIn facility that is designed to "plug things in" to the
init() and destroy() methods without having to subclass ActionServlet.

The Struts example app in 1.1 demonstrates how the API is used, by using a
plugin to load its pseudo-database.

Craig


>
>
>      /**
> 	 * Called just after the web application has been initialized
> 	 *
> 	 * @param event the ServletContextEvent containing the ServletContext
> 	 */
> 	public void contextInitialized(ServletContextEvent event) {
>
> 		ServletContext context = event.getServletContext();
> 		Object o = //get home interface
>
>
> 		context.setAttribute("someName", o);
>
>             // other stuff here
> 	}
>
>
> HTH,
>
> robert
>
>
> > -----Original Message-----
> > From: Adam Connors [mailto:adam.connors@cognima.com]
> > Sent: Tuesday, August 20, 2002 5:16 AM
> > To: struts-user@jakarta.apache.org
> > Subject: How to put things in the ServletContext
> >
> >
> > Hello,
> >
> > Apologies if this is an obvious one I've missed, (I'm a newbie
> > and a bit bewildered by the onslaught of new information). But I
> > want to put the home interface for an ejb into the servlet
> > context to save time with multiple jndi lookups.
> >
> > Traditionally this is easily done in the servlet init() method
> > and by making it a static member of the servlet. But I don't have
> > access to this anymore since everything goes through the action servlet.
> >
> > Is there a way of putting stuff into the init() method of the
> > action servlet without faffing around with subclassing it?
> >
> > I've found the DataSource and connection pool stuff, is this what
> > I need? Can I have application context without the pool? Would
> > there be any consequences of creating a pool with just one entry?
> >
> > Many thanks for your help,
> >
> > Adam.
> >
> > --
> > To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> >
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: How to put things in the ServletContext

Posted by Robert Taylor <rt...@mulework.com>.
Adam, take a look at javax.servlet.ServletContextListener. You have access
to ServletContext via the ServletContextEvent in contextInitialized().



     /**
	 * Called just after the web application has been initialized
	 *
	 * @param event the ServletContextEvent containing the ServletContext
	 */
	public void contextInitialized(ServletContextEvent event) {

		ServletContext context = event.getServletContext();
		Object o = //get home interface


		context.setAttribute("someName", o);

            // other stuff here
	}


HTH,

robert


> -----Original Message-----
> From: Adam Connors [mailto:adam.connors@cognima.com]
> Sent: Tuesday, August 20, 2002 5:16 AM
> To: struts-user@jakarta.apache.org
> Subject: How to put things in the ServletContext
>
>
> Hello,
>
> Apologies if this is an obvious one I've missed, (I'm a newbie
> and a bit bewildered by the onslaught of new information). But I
> want to put the home interface for an ejb into the servlet
> context to save time with multiple jndi lookups.
>
> Traditionally this is easily done in the servlet init() method
> and by making it a static member of the servlet. But I don't have
> access to this anymore since everything goes through the action servlet.
>
> Is there a way of putting stuff into the init() method of the
> action servlet without faffing around with subclassing it?
>
> I've found the DataSource and connection pool stuff, is this what
> I need? Can I have application context without the pool? Would
> there be any consequences of creating a pool with just one entry?
>
> Many thanks for your help,
>
> Adam.
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>