There are a couple of mistakes.

- The factory method should be public.

- You're casting to "database" and not "Database".

Regards,

Subrahmanyam


Matthias Haberkorn wrote:
>
> Hello developers,
>
> hmm, I hope this is not a "long time discussed" problem!
> If so, please forgive me - I'm new to this mailinglist ;-)
> Ok, here is my problem:
> I have a class for accessing my database-routines with an
> instance like this:
> -------------------------
> public class Database
> {
>         private static Database onlyInstance = null;
>
>         /* Private no-args constructor */
>         private Database() {
>                 init();
>         }
>
>         /** Private factory method (make a single instance of this
> class) */
>         private static Database instance() {
>                 if (onlyInstance == null)
>                         onlyInstance = new Database();
>                 return onlyInstance;
>         }
>
>         /** Init (Connect to DB) */
>         public void init()
>         {
>                 try {....
> --------------------------
>
> ...and I have a servlet which tries to get an instance of this class:
>
> --------------------------
> public class testdb extends HttpServlet
> {
>
>     /** Init the Servlet (get instance of db) */
>     public void init() throws ServletException
>     {
>         Database database =
>                 (database)getServletContext().getAttribute("database");
>
>         if (database == null) {
>                 getServletContext().setAttribute("database",
> database.instance() );
>         }
>     }
>
>         public void doGet() {...
> --------------------------
>
> If I try to get the instance back in one of the doGet/Post-methods, I
> get a "ClassCastException" for Database. This is how I try to get the
> instance:
>
> --------------------------
> ....
>             Database database =
>                 (Database)getServletContext().getAttribute("Database");
>
>             ResultSet rs = database.getSomething(id);
> ....
> --------------------------
>
> It looks like that the getAttribute-methode doesn't give back a object
> of type Database. Or is there something complete wrong with that code ??
> (..or likely with my concept of interservlet-communication ??)
>
> I use a Linux box with the Blackdown-JDK 1.1.7v3 with a Jetty-Webserver.
> Thanks for all contributions to this question.
>
> bye
>
>         MaZe
>
> -------------------------------------------------------------
>  BSB Software GmbH       mailto:[EMAIL PROTECTED]
> Mayfarthstra�e 25-27    Internet: http://www.bsb-software.com
>   60385 Frankfurt
>       Tel: +49 69 94 414 718 - Fax: +49 69 94 414 719
>
>     Internet  *  Netzwerke  *  Consulting  *  Schulungen
> -------------------------------------------------------------
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

--
-----------------------------------------------------------------------
Subrahmanyam A.V.B.                          Wipro Infotech (EC/FS)
Associate Consultant                         1st Floor, Basappa Complex
Tel: 91-80-2270439x211                       40/1A Lavelle Road
Fax: 91-80-2278597                           Bangalore 560 001
-----------------------------------------------------------------------

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to