Okay, your goal is to put beans in the application scope.  The
application scope is initialized where Robert said it is, but from the
sounds of it, you are really only wanting to do something much more
simple.

Just like you can put beans in the request scope, page scope, or session
scope, you can put beans in the application scope.

If you were to add a bean to the session, you would do:
session.setAttribute("myBeanName",new BeanObject());

You would do the same for application scope, but it's not called
application, it's called the ServletContext.

servletContext.setAttribute("myBeanName",new BeanObject());

You get the servletContext by accessing the protected field of the
Action as:

servlet.getServletContext();

Once your bean is in the servlet context, you can do:

BeanObject myBean = (BeanObject)
servletContext.getAttribute("myBeanName");

If you are worried about initialization of your bean, simply instantiate
it in your code, set your properties of the bean, and then add it to the
context.

One pattern I use with objects that are used throughout my application
is that I create a singleton for the servlet context via:

ApplicationStuff as = ApplicationStuff.getInstance(ServletContext);

The method is as follows:

public static ApplicationStuff getInstance(ServletContext sc)
{
        ApplicationStuff as = (ApplicationStuff)
sc.getAttribute("APP_KEY");
        if (as == null)
        {
                synchronized (_lock)
                {
                        as = (ApplicationStuff)
sc.getAttribute("APP_KEY");
                        if (as == null)
                        {
                                as = new ApplicationStuff();
                                sc.setAttribute("APP_KEY",as);
                        }
                }
        }
        return as;
}

What this allows is for only a single instance of ApplicationStuff for
the servlet context.  The synchronized method in there makes sure that
you don't get multiple copies the first time a bunch of threads try to
get an instance for the first time.

-Jacob


| -----Original Message-----
| From: Howard Miller [mailto:[EMAIL PROTECTED]]
| Sent: Thursday, August 15, 2002 9:14 AM
| To: 'Struts Users Mailing List'
| Subject: RE: Initialisation
| 
| Errrr.....
| 
| sorry, out of my depth again. I read the documentationfor
| ServletContextListener, and I'm not sure... Does this mean that all I
have
| to do is implement the interface, drop the .class file in
WEB-INF/classes
| and that's it... the code will run when the servlet starts up? Sounds
too
| easy!
| 
| How do I override the ActionServlet.init(). I can write the code but
where
| do I put it?
| 
| Bootstrap servlet - what's that?
| 
| Plugins - Is there any documentation?
| 
| So many questions - sorry!
| 
| -----Original Message-----
| From: Robert Taylor [mailto:[EMAIL PROTECTED]]
| Sent: 15 August 2002 14:58
| To: Struts Users Mailing List
| Subject: RE: Initialisation
| 
| 
| Howard, look at javax.servlet.ServletContextListener. It is a
bootstrap
| class for servlet containers. If you wanted to use servlets you could
| override ActionServlet.init() or create your own bootstrap servlet or
use
| Struts plug ins.
| 
| I use ServletContextListener because it is a standard way for
| bootstrapping
| web applications and init parameters can be defined in web.xml.
| 
| robert
| 
| > -----Original Message-----
| > From: Howard Miller [mailto:[EMAIL PROTECTED]]
| > Sent: Thursday, August 15, 2002 9:51 AM
| > To: '[EMAIL PROTECTED]'
| > Subject: Initialisation
| >
| >
| > Errr... stupid newbie question coming up.
| >
| > If I want to create objects in the Application Scope the design
notes
| for
| > Struts says.... "application scope beans are initialized in the
init()
| > method of a startup servlet".
| >
| > But, and I'm sure I'm missing something here, I haven't got an
| > init() method
| > because the servlet belongs to Struts. So where do I put my
| > innitialisation
| > code?
| >
| > I noticed plugins in earlier posts, but there isn't any
| > documentation that I
| > can find - is this what I'm looking for?
| >
| > Sounds like a fundamental thing to want to do, which is why I'm sure
I'm
| > missing something really obvious!
| >
| > Cheers...
| >
| > Howard
| >
| > --
| > To unsubscribe, e-mail:
| > <mailto:[EMAIL PROTECTED]>
| > For additional commands, e-mail:
| > <mailto:[EMAIL PROTECTED]>
| >
| 
| 
| --
| To unsubscribe, e-mail:
| <mailto:[EMAIL PROTECTED]>
| For additional commands, e-mail:
| <mailto:[EMAIL PROTECTED]>
| 
| --
| To unsubscribe, e-mail:   <mailto:struts-user-
| [EMAIL PROTECTED]>
| For additional commands, e-mail: <mailto:struts-user-
| [EMAIL PROTECTED]>
| 
| ---
| Incoming mail is certified Virus Free.
| Checked by AVG anti-virus system (http://www.grisoft.com).
| Version: 6.0.381 / Virus Database: 214 - Release Date: 8/2/2002
| 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.381 / Virus Database: 214 - Release Date: 8/2/2002
 


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to