aspose file tools*
The moose likes Servlets and the fly likes Session HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session HashMap" Watch "Session HashMap" New topic
Author

Session HashMap

Nikhilesh Fonseca
Ranch Hand

Joined: Aug 23, 2004
Posts: 57
I need to store the session objects on to a hashmap so that it is possible to view all the users logged in.
I created a session Hashmap as like this
import java.util.HashMap;

public class HashMapWrapper {

static HashMap sessionHashmap;


private HashMapWrapper(){

}

public static HashMap getHashMapInstance(){
if(sessionHashmap==null)
sessionHashmap= new HashMap();

return sessionHashmap;
}


}
I need to store the user sessions in this hashmap .
I saw an earlier post but couldn't make out much
Also Do i need to store this Hashmap in the application scope or settings properties. I have no idea on how these work
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
If you need info about all users logged in etc, then you cant store this info in one particular session because as soon as that user 'logs out' that session will be invalidated and you will lose the HashMap.

It makes more sense to put the HashMAp in application scope where it will available to you as long as the server is up, and i guess that is what you want.

You could also put it in properties, but i dont think thats the right approach. Putting it in application scope is a better idea.

To put it in application scope, you need to do something like the following,



if you have not overridden the init() method of your servlet. if you have overridden it, make sure you do a 'super.init(ServletConfig config)' in your init() method
Nikhilesh Fonseca
Ranch Hand

Joined: Aug 23, 2004
Posts: 57
Thanx for your reply.

Is there a limit to the amount of data I can store in to the application scope.As in will it affect performance etc.
Or should i just use the databse. But I really want to try this
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

Use the database if you want a history of all users who have accessed you application.
You could use the application (Context object) if you need to know only the logged in users.
Personally I would use the db anyways - your code's best portable and scalable (from a requirements change perspective) that way.

Thanks,
Ram.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
If you want to store the history of all users to persistent storage, then DB is the right way to go. putting it in the application scope means that the information is available only until the application is available.

however, if you want only only info of users logged in as long as the application is running, i would consider a few points:

1. size vs performance:
there is no limit as such to how much you can store in application scope. it will only be limited by the total memory available.
performance with storing large objects depends on the memory available and the size of the object, right?

2. putting it all in DB: DB hits are always expensive n terms of time and memory. so if you are going to access the data frequently, i would not use a DB. but if the data is not that frequently used, yes, then this may be a viable solution.
OR
if you can segregate the data used more frequently and ata used less frequently, then you might want to use a combination of both - application context and DB.


what essentially happens when you put an object in application scope:



now, what you have done is, placed the object reference in the application context. the actual object is not stored 'in' the application context as such.
so, all that the application context contains is name-value pairs of object names and object reference variables(probably in a HashMap?)

i hope that makes sense to you and is of some help.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This might be the thread to which you were referring:
http://www.coderanch.com/t/360149/Servlets/java/know-which-users-system


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Srinivas Reddy Patelu
Greenhorn

Joined: May 06, 2005
Posts: 5
public class LoggedInUserTable {
/** A Hashtable of all of the active users. */

private static Hashtable LoggedInUsers = new Hashtable ();

public static boolean isLoggedInUser (String username) {
return LoggedInUsers.containsValue(username);
}

public static void addLoggedInUser ( String sessionId, String username) {
LoggedInUsers.put (sessionId, username);
}

public static String getSessionId(String username) {
String sessionId = "";
Enumeration enum = LoggedInUsers.keys();
while(enum!= null && enum.hasMoreElements()){
String key = (String)enum.nextElement();
String value = (String)LoggedInUsers.get(key);
if(value.equals(username))
return key;
}
return sessionId;
}

public static void removeLoggedInUser(String sessionId){
LoggedInUsers.remove(sessionId);
}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Nikhilesh Fonseca:
I need to store the session objects on to a hashmap so that it is possible to view all the users logged in.
I created a session Hashmap as like this
import java.util.HashMap;

public class HashMapWrapper {

static HashMap sessionHashmap;


private HashMapWrapper(){

}

public static HashMap getHashMapInstance(){
if(sessionHashmap==null)
sessionHashmap= new HashMap();

return sessionHashmap;
}


}
I need to store the user sessions in this hashmap .
I saw an earlier post but couldn't make out much
Also Do i need to store this Hashmap in the application scope or settings properties. I have no idea on how these work


Nikhilesh,
If you're still interested in this and haven't implemented it yet, I just put a fully working demo app up on my site that shows how to do this.
http://simple.souther.us/not-so-simple.html
Look for SessionMonitor.war
Nikhilesh Fonseca
Ranch Hand

Joined: Aug 23, 2004
Posts: 57
Thanx Guys
I tried the application scope

public void init() throws ServletException {
super.init();
getServletContext().setAttribute("sessionHashMapWrapper" ,HashMapWrapper.getHashMapInstance());


}
and it works fine.
I initalzed it in the init() method and called it evrytime a user logs in
if(name.equals("IntroductionAction"))
{

try{
HttpSession session=req.getSession(false);
System.out.println("HashMap::b4");
HashMap localHashMap=(HashMap)getServletContext().getAttribute("sessionHashMapWrapper");
localHashMap.put(session.getId(),session);
getServletContext().setAttribute("sessionHashMapWrapper",localHashMap);
System.out.println("HashMap::"+localHashMap);
}
catch(Exception e){
e.printStackTrace();
}
}

I havent really trhot about the duplicate conatraints but i'll figure it out
So thnax a lot
 
 
subject: Session HashMap
 
Similar Threads
Please Help: Putting Vector objects into HashMap
How to get a HttpSession from a Session ID ?
how can i deal with this unchecked warning?
Vector -- how do i use it??
Hashmap