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; }
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: 12268
1
posted
0
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.