Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session Utility class

 
Tmmet Johnson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic