| By Emanuele Tatti | Article Rating: |
|
| February 6, 2008 04:15 AM EST | Reads: |
21,239 |
Usually, in a rich internet application (RIA), a user with a registered account can do two different logins from two different workstations and can maintain two concurrent sessions opened. In some applications we want to limit the users to one session per account, so we have to take countermeasures.
A simple method to check if a user is logged is to set a property in the current HttpSession; in this example in our login function we set
session.setAttribute("username", username);
If there is no username attribute, we will return an error to the user.
When someone starts a session with his account we have to check if there is already a session opened with that account. We can use an HashMap using the username as key and the session as value; obviously we have to use the same hashmap across multiple logins. A very fast and simple solution is to create a singleton that exposes operations on our hashmap:
public class MySessionManager {
public boolean exist(String username) {
if(hashMap.containsKey(username)) {
return true;
}
return false;
}
public boolean addSession(HttpSession session) {
if(hashMap.containsKey(session.getAttribute("username"))) {
return false;
}
hashMap.put((YouthruCorpPrincipal)session.
getAttribute("username"), session);
return true;
}
public HttpSession getSession(String username) {
return hashMap.get(username);
}
public boolean removeSession(String username) {
if(!hashMap.containsKey(username)) {
return false;
}
hashMap.remove(username);
return true;
}
private static MySessionManager instance;
public static MySessionManager getInstance() {
if(instance == null)
instance = new MySessionManager();
return instance;
}
public MySessionManager() {
hashMap = new HashMap();
}
}
In our login function we have to check the existence of a previously created session with the same username, in that case we can logout the user associated with that session:
if(MySessionManager.getInstance.exist(username)) {
logout(MySessionManager.getInstance.getSession(username));
MySessionManager.getInstance.removeSession(username);
}
In this case the logout function takes as argument a session and log out from our application the user associated with that session, then we can do our login routine and at add the current session to the hashmap.
In this example we are checking only for sessions with a username attached, if we want to do some operation every time a session is created/destroyed, we can implement the HttpSessionListener interface:
Classes that implement this interface will be notified of session creation and destruction using the respective functions:
void sessionCreated(HttpSessionEvent se)
void sessionDestroyed(HttpSessionEvent se)
To receive notification events, the implementation class must be configured in the deployment descriptor for the web application; for example:
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/
j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>
myapp.security.MySessionListener
</listener-class>
</listener>
</web-app>
Published February 6, 2008 Reads 21,239
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Emanuele Tatti
Emanuele Tatti is a software engineer at Comtaste, a consulting company and solution provider for Rich Internet Applications (RIA) based in Rome, Italy, and operating internationally (www.comtaste.com/en). His areas of expertise are Flex, Livecycle Data Services, BlazeDS and Java for building enterprise applications. He has been involved in several projects especially related to the financial field and is also chief engineer for YouThruBiz, an innovative enterprise-class rich internet application which allows companies and recruiters to select human resources through multimedia job interviews and e-resumes.
His posts can be found at blog.comtaste.com
- IDEs Belong in the Cloud
- ActiveState Releases Komodo 7, "World's Fiercest IDE"
- eXo Platform 3.5 Now Available: First Cloud-Ready Enterprise Portal and User Experience Platform-as-a-Service (UXPaaS)
- Salesforce.com Announces the Availability of D&B Company Information in Data.com
- Blog Summary for Week of February 6
- MercadoLibre Deploys Opscode Chef® to Automate its OpenStack Private Cloud
- AppFog Enhances User Experience With Additional Add-On Partners Blitz.io and Iron.io
- CloudBees Reduces Cost to Run Java Applications by 62 Percent
- PatientsLikeMe Contributes Free Open-Source Parser to Blue Button Initiative
- BET and CENTRIC Pay Tribute to the Richness and Diversity of the African-American Experience With a Lineup of Dynamic Programming During Black History Month
- Brookfield Homes Calgary Partners with Interior Designer and TV Personality Jillian Harris
- McKinney and EvoApp Kick Off Social Bowl 2012
- IDEs Belong in the Cloud
- ActiveState Releases Komodo 7, "World's Fiercest IDE"
- eXo Platform 3.5 Now Available: First Cloud-Ready Enterprise Portal and User Experience Platform-as-a-Service (UXPaaS)
- Salesforce.com Announces the Availability of D&B Company Information in Data.com
- Blog Summary for Week of February 6
- MercadoLibre Deploys Opscode Chef® to Automate its OpenStack Private Cloud
- AppFog Enhances User Experience With Additional Add-On Partners Blitz.io and Iron.io
- CloudBees Reduces Cost to Run Java Applications by 62 Percent
- PatientsLikeMe Contributes Free Open-Source Parser to Blue Button Initiative
- BET and CENTRIC Pay Tribute to the Richness and Diversity of the African-American Experience With a Lineup of Dynamic Programming During Black History Month
- Brookfield Homes Calgary Partners with Interior Designer and TV Personality Jillian Harris
- McKinney and EvoApp Kick Off Social Bowl 2012
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- Ruby on Rails Won't Make It in 2007 and Forget About AJAX
- The Jury's Still Out On Ruby On Rails (RoR) and AJAX
- The Top 250 Players in the Cloud Computing Ecosystem
- Red Hat Named "Platinum Sponsor" of Virtualization Conference & Expo
- Ruby on Rails Creator Says: "Reduce the Risk, Hire Programmers From Open Source"
- Java Kicks Ruby on Rails in the Butt
- Can Ruby Live Without Rails?
- An Introduction to Ant
- Testing in Ruby on Rails
- 4th International Cloud Computing Conference & Expo Starts Today
- Cloud Expo 2011 East To Attract 10,000 Delegates and 200 Exhibitors
















