• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stateless Functions  RSS feed

 
Syed Hussain
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to login a user to our web-site we have a method
public User authenticate(String id, String password);
i.e. it authenticates a User and returns a populated User object otherwise it returns null.
This method is recieved by a JSP which authenticates the User and forward the USer to another JSP.
Now, should the method be static ?
OR
Should the class be Singleton ?
OR
Should the class have Application Scope i.e.
<jsp:useBean class="Authenticate" scope="Application" />
OR
Should we create a new object of the class everytime?
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go with either a singleton or a static method.
To decide between them ask yourself the following:
  • Is the class keeping any state?
  • Will there possibility be a need for more than one instance?
  • Will the class need to extend another class or be extended in the future?
  • Do you need to contol when the class is loaded into memory?


  • If the answer to all of these is no than you are best off going with a static final method as the Singleton pattern is not going to offer any benefits.
    Either way you go, if this is to be used from a JSP then I would wrap it with a custom tag.
    [ September 15, 2002: Message edited by: Chris Mathews ]
     
    Syed Hussain
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the reply, Chris.
    Whats wrong with using a bean with application scope. It will be same as Singleton.
    If we wrap the class with custom tags, it will be same as creating a new bean everytime. So last approach is also correct ?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!