I'm developing a web system using servlets that allows users to log in. When they log in they are assigned a HTTP session ID, and every time the access a new page (via a new servlet) the time between the last access from that session and the curretn time is checked. If its more than 10 minutes, it returns the user to the home page, and tells them to login again.
Works great.... no problems at all.
Once the session has been set, as a user navigates to new pages via new servlets, their information is loaded into the new servlet from a call such as Patient.load(session) wherby it searches through the SQL tables and retreives the patient with that session.
The code below is how i am handling sessions:
public abstract class AbstractPage extends HttpServlet
{
protected String function = null;
protected Connection con = null;
protected HttpSession session = null;
protected boolean nosession = false;
public boolean handleSession(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{ session = request.getSession(true);
String function = request.getParameter("function"
;
if (session.isNew() && !function.equals("login"
)
{
new HomePage().doRequest(request, response);
return false;
}
/**
* Checks the time that the session was last accessed against
* the current system time. If this is greater than 600,000
* millisecond (10 minutes), the session is deemed "expired"
* and user is returned to login again
**/
else
{ long current = System.currentTimeMillis();
long lastAccess = session.getLastAccessedTime();
long inactiveTime = current - lastAccess;
if (inactiveTime > 600000)
{ new HomePage().doRequest(request, response, "Your session has expired"
;
return false;
}
}
return true;
}
The problem i have now is that i want multiple users to access the system. Currently, if two people log on within a short space of time (from the same login servlet)they get the same session ID, so that updates to data are sometimes saved to the wrong patient on the database as they have the same session. Is there a better way to do this? Alternatively, can you "speed up" the renewel of sessions so that the system can keep up with multiple users. Have i even got the right end of the stick in handling sessions?!!!
Of course I have it that when users log out i call session.invalidate(), thus the next user gets a new session.
Many thanks in advance
Works great.... no problems at all.
Once the session has been set, as a user navigates to new pages via new servlets, their information is loaded into the new servlet from a call such as Patient.load(session) wherby it searches through the SQL tables and retreives the patient with that session.
The code below is how i am handling sessions:
public abstract class AbstractPage extends HttpServlet
{
protected String function = null;
protected Connection con = null;
protected HttpSession session = null;
protected boolean nosession = false;
public boolean handleSession(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{ session = request.getSession(true);
String function = request.getParameter("function"
if (session.isNew() && !function.equals("login"
{
new HomePage().doRequest(request, response);
return false;
}
/**
* Checks the time that the session was last accessed against
* the current system time. If this is greater than 600,000
* millisecond (10 minutes), the session is deemed "expired"
* and user is returned to login again
**/
else
{ long current = System.currentTimeMillis();
long lastAccess = session.getLastAccessedTime();
long inactiveTime = current - lastAccess;
if (inactiveTime > 600000)
{ new HomePage().doRequest(request, response, "Your session has expired"
return false;
}
}
return true;
}
The problem i have now is that i want multiple users to access the system. Currently, if two people log on within a short space of time (from the same login servlet)they get the same session ID, so that updates to data are sometimes saved to the wrong patient on the database as they have the same session. Is there a better way to do this? Alternatively, can you "speed up" the renewel of sessions so that the system can keep up with multiple users. Have i even got the right end of the stick in handling sessions?!!!
Of course I have it that when users log out i call session.invalidate(), thus the next user gets a new session.
Many thanks in advance