Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to instantiate a singleton class using jsp:useBean

 
Rajani Gummadi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nikhil Pasupukuntla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajani,

First of all the class to be instantiated must follow all the rules of java bean. http://en.wikipedia.org/wiki/JavaBean
 
Stefan Evans
Bartender
Posts: 1721
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic