• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Session HashMap

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might be the thread to which you were referring:
https://coderanch.com/t/360149/Servlets/java/know-which-users-system
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic