aspose file tools*
The moose likes JSP and the fly likes How to instantiate a singleton class using jsp:useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to instantiate a singleton class using jsp:useBean" Watch "How to instantiate a singleton class using jsp:useBean" New topic
Author

How to instantiate a singleton class using jsp:useBean

Rajani Gummadi
Ranch Hand

Joined: Dec 17, 2010
Posts: 48
Hi,

Suppose I have a class whose constructor is private and I get an instance of it using getInstance() method, which ensures that only one object of that class exists. Now I want the object of this class to be accessed in jsp.



Now I know in jsp, we can use jsp:useBean to get an instance of class and I believe, this expects a public constructor to be available. But If I have a class like above and I want to have an object instantiated in jsp, so that I can invoke some non-static methods, how do I do that? First of all, is it possible ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Nope, it must follow all the rules for a bean.

Why aren't you just creating the instance in the page controller and setting it into request scope before the JSP is forwarded to?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nikhil Pasupukuntla
Greenhorn

Joined: Jun 04, 2009
Posts: 21
Hi Rajani,

First of all the class to be instantiated must follow all the rules of java bean. http://en.wikipedia.org/wiki/JavaBean


Nikhil Pasupukuntla
SCJP 1.5 | OCPJWCD 1.5
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
If it is a singleton bean then I would actually instantiate it into application scope at start-up time, in a servlet.


You then KNOW it will be in scope, and can import it with



Note the use of "type" as opposed to "class" here.
Using it like this, as the object is already guaranteed to be instantiated (because it happened at startup) the useBean tag should never fail.

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Singletons considered harmful. Don't do it. Sure, you can figure out ways to do it, but the basic idea is 100% wrong.

A singleton is just a global wad of data. Its bad programming practice, it breaks unit testing (like JUnit). There is no reason to do it, there are many other ways to implement the same concept that don't have the downside.
Rajani Gummadi
Ranch Hand

Joined: Dec 17, 2010
Posts: 48
Thanks to all for your replies.

The primary reason for me to think about a singleton class, is this class would actually act as a container for few static information, which would not change dynamically and all it does is to have this returned when requested in jsp. Actually for this requirement, I can load all my data in the static block of the class and get the details from a static method. This way, I need not instantiate it explicitly and add that as an attribute at startup. But I was wondering, If I could avoid static methods and do something like getInstance(). Which I mentioned above. But as been advised, I realize that it should follow the bean specs.

If I do not want to instantiate the class and add to request attribute, in startup, can I follow the method i.e using static initializers and static methods. Will I be getting into any troubles with this approach in any worst case ?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

If this is something that will be initialized once and used across the entire application, instantiate the bean in a context listener and put it in application scope. That way, it will always be available to any resource.

I'd avoid static methods. Sloppy.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

I can't think of a single valid reason to have a singleton in a JSP.

Just make a normal class, use a Factory pattern to ensure one-and-only-one instance application wide if you want, and place the class into the Session for access using EL. Then use the normal getters.

You can instantiate it once at application startup, and set the instance into the Session once at login. Any cost of doing that is overwhelmed by your normal processing. It will be free.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Pat Farrell wrote: set the instance into the Session once at login.

Bear's idea is even better. Put it in application scope.
Rajani Gummadi
Ranch Hand

Joined: Dec 17, 2010
Posts: 48
Actually I do not need this bean for the entire application. I need this to be used only for one jsp, one single request/response cycle. Would you still advise me to instantiate at startup or is there any other better and simple approach, I can follow.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

No, if it's only needed for one JSP, instantiate it in the page controller and place it in request scope for the JSP to use.
Rajani Gummadi
Ranch Hand

Joined: Dec 17, 2010
Posts: 48
Thanks to you all.

I'm just curious to know on ill effects that can occur, by using the static methods in jsp. From Bear's comment, he mentioned it as sloppy. But some times, using static methods look tidy and clear. But why not in jsps. Is that because, to avoid any kind of java code as much as possible or any other specific reason.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Firstly, there should never be any Java code in a JSP. Ever. Ever.

Static methods have their place, but this is not one of them. There is no reason not to use a request-scoped bean.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Rajani Gummadi wrote:by using the static methods in jsp.

Why are you using any method calls in JSP? That is so last century. Modern JSP use EL, not scriptletts.
 
Don't get me started about those stupid light bulbs.
 
subject: How to instantiate a singleton class using jsp:useBean