Skip to end of metadata
Go to start of metadata


The SessionMap is an extended version of a Map with a sessionID property.  The sessionID property is a unique identifier for a SessionMap.  Attributes are added to the SessionMap using standard Map operations.

Examples of the SessionMap in use may be found in the authoring sections of the chat and noticeboard tools. 

When should a SessionMap be Used

Attributes should be stored in the request scope whenever possible.  If this is not practical, then they may be stored in the session scope if it is certain that only one instance of the module will be used at a time.   For all other cases attributes should be stored in a SessionMap object.

Examples of where the SessionMap object could be used.

  • In learning pages it is possible for two instances of the tool to be running at the same time. If it is necessary to use session variables (ie it just can't be done using the request) then use the SessionMap.
  • Normally the authoring pages use the session as it is very difficult to do the attachment lists without using the session. To avoid potential conflicts (e.g. Monitoring pages may reuse Authoring pages to implement define later) the authoring screens should reuse the session.

How to use a SessionMap

Calling the default constructor will create a SessionMap object with a unique sessionID value.  The unique sessionID can then by used to store the SessionMap object in the HttpSession.

// create
SessionMap map = new SessionMap();
// add attributes to the SessionMap
map.put("myKey", "myValue");
// add the sessionMap to HTTPSession.
request.getSession().setAttribute(map.getSessionID(), map);

To retreive the SessionMap from the HttpSession each client request must include the sessionID as a parameter.

The sessionID can be made available to a page by setting it as a request attribute or placing it with in a struts form.  The following example uses a struts form.

<html:form action="/authoring" styleId="authoringForm" method="post" enctype="multipart/form-data">
    <html:hidden property="sessionMapID" />
(SessionMap) request.getSession().getAttribute(authForm.getSessionMapID());

To access a SessionMap object within a jsp page use the following.

<c:set var="sessionMap" value="${sessionScope[sessionMapID]}"/>

If the sessionMapID is stored within a struts form (in request scope) then use the following.

<c:set var="formBean" value="<%= request.getAttribute(org.apache.struts.taglib.html.Constants.BEAN_KEY) %>" />
<c:set var="sessionMap" value="${sessionScope[formBean.sessionMapID]}"  scope="request"/>

Retrieving elements from the SessionMap is the same for any other Map.


It is important to remove the SessionMap object from the HttpSession when it is no longer needed.  For the authoring pages, the AuthoringButton tag has an attribute customiseSessionID that passes the sessionID to the ClearSessionAction class. So the method in ClearSessionAction will typically be:

public void clearSession(String customiseSessionID,HttpSession session, ToolAccessMode mode) {

Finding out what is in the session

It can sometimes be hard to work out what is in the session. Putting this code in your JSP will show the current session and request scoped objects

<p>Session Scope</p>
  <c:forEach var="obj" items="${sessionScope}">
    <c:out value="${obj}"/><BR/>
<p>Request Scope</p>
  <c:forEach var="obj" items="${requestScope}">
    <c:out value="${obj}"/><BR/>
  • No labels