• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Session Utility class

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can I have all methods related to session in one Utility class as below?Is there any disadvantage or issue on using the below approach?If so, what are the cons?
Thanks in advance,
public class SessionUtil {
private SessionUtil{}
public static HttpSession createNewSession(HttpServletRequest request) {
//create a new session, if one does not exists
HttpSession session = request.getSession();
putSessionValue(request, CONSTANTS.KEY, CONATNVALUE);
return session;
}

public static HttpSession getSession(HttpServletRequest request) {
return request.getSession(false);
}
public static void putSessionValue(HttpServletRequest request, String key, Object value) {
HttpSession session = request.getSession(false);
session.setAttribute(key, value);
}
public static Object getSessionValue(HttpServletRequest request, String key) {
HttpSession session = request.getSession(false);
return session.getAttribute(key);
}
public static void cleanSession(HttpServletRequest request) {
HttpSession session = request.getSession(false);
session.invalidate();
}
}
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A utility class of all static functions is an anti-pattern that I try to avoid.
 
Tmmet Johnson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!
Could you please post what is the disadvantage or loophole on having a class with all static methods?
Thanks in advance,
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not very OO, is it?

A more detailed discussion of the utility class anti-pattern is not a servlet issue and should probably take place in Java in General.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get rid of the ugly you get it to create instance. If you lookclosely, you keep passing the request in, so why not instantiate with the request. Then what you have is a wrapper on a request and all of your methods become methods on the instance and use the internal request.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that your "utility" methods make the assemption that the response already has a session. If this is not true they will throw NullPointerException

I can't see any reason for this class, it won't even save you any typing and will make errors harder to find.

Bill
reply
    Bookmark Topic Watch Topic
  • New Topic