(Off the JSP topic.) The singleton pattern simply says that you have only
one instance of the class. How you go about implementing it to ensure that
you have only one instance is up to you. One way is to force users to go
through a static accesser, as you suggest. But I can also make a Java Bean,
with the required public constructor, have that constructor check a static
to see if it's being constructed a second time, and throw an exception if
so. That's a perfectly valid singleton, and will then require that all JSPs
put this bean in the application scope. This is one reason why I don't like
design patterns: people get locked into thinking that "singleton" means "has
a static accesser" and forget that there's more than one way to do just
about anything.
(Getting back to the JSP topic.) The other reason that I don't like design
patterns is that, rather than looking at a problem and trying to find the
best solution, people look at a problem and simply find the closest matching
design pattern; if all you ever code with are the specified design patterns,
you've got a very limited toolbox. The original problem, as I understand it,
does not need "the singleton pattern" to solve it. Once you've decided to
move your Hashtable into a bean, you put the originally-desired static
initializer of the Hashtable in the bean, and then use the bean however you
want. This allows more flexibility in how the bean is used and scoped. And
it looked to me like the Hashtable in question is meant to be read-only, so
there shouldn't be any threading issues (in which case, consider using a
HashMap instead, which doesn't synchronize so you'll save some overhead).
--Jim Preston
-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Dowd, Brian
Sent: Friday, August 04, 2000 2:26 AM
To: [EMAIL PROTECTED]
Subject: Re: static initialiser block in JSP page
But surely a Java Bean can't be a singleton (in our typical usage of the
word), because it must have a public default constructor, and
calling <usebean...> on it will not allow you to call the static
XBean.getInstance()
method ? Doesn't making it application scope have the same effect ?
> -----Original Message-----
> From: Ron Chan [SMTP:[EMAIL PROTECTED]]
> Sent: 03 August 2000 18:22
> To: [EMAIL PROTECTED]
> Subject: Re: static initialiser block in JSP page
>
> Hi,
>
> take a look at the Singleton pattern.
>
> ron
>
>
> --- Oren Klichevsky <[EMAIL PROTECTED]>
> wrote:
> > I think that you should approach the problem from
> > another direction.
> > Create a bean that will do all the initilization of
> > the Hashtable on it's
> > instantiation.
> > After that use your bean with the "application"
> > scope within your JSP files.
> > This will assure you that this bean will be
> > inistantiated only one time for
> > the whole application. After doing that you will be
> > able to use all the
> > regular bean syntax of JSP to access it's data.
> >
> >
> > Best Regards,
> > Oren Klichevsky
> > www.mannainc.com <http://www.mannainc.com>
> >
> >
> >
> > -----Original Message-----
> > From: A mailing list about Java Server Pages
> > specification and reference
> > [mailto:[EMAIL PROTECTED]]On Behalf Of
> > Dowd, Brian
> > Sent: Thursday, August 03, 2000 6:12 PM
> > To: [EMAIL PROTECTED]
> > Subject: static initialiser block in JSP page
> >
> >
> > HI All,
> > I'm trying to get a static initializer in my JSP
> > page (such that it will
> > only ever populate
> > a hashtable once in the java file generated).
> > Something *like* this :
> >
> > <%!
> > String code = "X";
> >
> > // static initialiser block to construct
> > hashtable (once ever).
> > static {
> > Hashtable months = new Hashtable();
> > // These values come from the region
> > table in ARM.
> > regions.put("0", "January");
> > regions.put("2", "February");
> >
> > // .... etc
> >
> > }
> >
> > %>
> >
> >
> > <%
> > // Use a month
> > out.println("Jan=" + (String) regions.get("1"));
> >
> > %>
> >
> >
> > It works if I move it out of the <%! .. %> tags and
> > make it a non static,
> > but the
> > point is I want it to be static .... has anyone done
> > anything similar ?
> >
> > Cheers,
> > Brian.
> >
> >
> ==========================================================================
> =
> > To unsubscribe: mailto [EMAIL PROTECTED] with
> > body: "signoff
> > JSP-INTEREST".
> > Some relevant FAQs on JSP/Servlets can be found at:
> >
> > http://java.sun.com/products/jsp/faq.html
> > http://www.esperanto.org.nz/jsp/jspfaq.html
> > http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> >
> >
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
> >
> >
> ==========================================================================
> =
> > To unsubscribe: mailto [EMAIL PROTECTED] with
> > body: "signoff JSP-INTEREST".
> > Some relevant FAQs on JSP/Servlets can be found at:
> >
> > http://java.sun.com/products/jsp/faq.html
> > http://www.esperanto.org.nz/jsp/jspfaq.html
> > http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> >
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
>
> __________________________________________________
> Do You Yahoo!?
> Kick off your party with Yahoo! Invites.
> http://invites.yahoo.com/
>
> ==========================================================================
> =
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets