File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Session Utility class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Utility class" Watch "Session Utility class" New topic
Author

Session Utility class

Tmmet Johnson
Ranch Hand

Joined: Nov 03, 2004
Posts: 56
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

Joined: Jan 10, 2002
Posts: 60992
    
  65

A utility class of all static functions is an anti-pattern that I try to avoid.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tmmet Johnson
Ranch Hand

Joined: Nov 03, 2004
Posts: 56
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

Joined: Jan 10, 2002
Posts: 60992
    
  65

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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session Utility class