Listing 1: UserSession's getValues() and setValues()
public Hashtable getValues() throws Exception
{
// Pass a copy instead of the reference to this.sessionData
return (Hashtable) sessionData.clone();
}
public void setValues(Hashtable values) throws Exception
{
// After setting, reference to the local copy instead of
// pointing to client's hashtable object
sessionData = (Hashtable) values.clone();
}
Listing 2: Sample JSP page for accessing the session values locally (same JVM)
<%@ page import=" java.util.*, sharedSessionSample.local.* " %>
<%
String user = request.getParameter("user");
if(user == null) user = "JohnDoe";
// First instantiate the session, then try to load it.
// If not found (i.e. could not be loaded), let's create it.
UserSession userSession = new UserSession();
userSession.setPath("./sessions");
try
{
userSession.load(user);
}
catch(Exception e)
{
userSession.create(user);
}
// Now increment it.
// If null (not found), let's start from zero.
int i = 0;
if(userSession.getValue("i") != null)
{
i = ((Integer) userSession.getValue("i")).intValue();
i++;
}
userSession.setValue("i", new Integer(i));
%>
The value of the 'i' for user '<%= user %>' is now <%= i %>.
<%
// Finally save the whole session
data including the 'i'.
userSession.save();
%>
Listing 3: Implementation of the ejbCreate() method of the EJB
try
{
setPath();
create(userId);
return userId;
}
catch(java.io.IOException ioe)
{
throw new DuplicateKeyException(userId);
}
catch(Exception e)
{
throw new CreateException(e.toString());
}
Listing 4: findByPrimaryKey() and ejbLoad() methods
public String ejbFindByPrimaryKey(String userId)
throws ObjectNotFoundException
{
setPath();
try
{
load(userId);
return userId;
// Ok, now the ejbLoad() is called by the WebLogic
}
catch(Exception e)
{
throw new ObjectNotFoundException(userId);
}
}
public void ejbLoad()
throws EJBException
{
try
{
setPath();
load((String) ctx.getPrimaryKey());
}
catch(Exception e)
{
throw new EJBException(e.toString());
}
}
Listing 5: Sample JSP page using the EJB for remote and shared access
<%@ page import=" javax.naming.*, javax.ejb.*,
java.rmi.RemoteException, java.rmi.Remote,
java.util.*, sharedSessionSample.remote.* "%>
<%
String user = request.getParameter("user");
if(user == null) user = "JohnDoe";
Context ctx = new InitialContext();
UserSessionHome home = (UserSessionHome)
ctx.lookup("sharedSessionSample.UserSession");
UserSession userSession = null;
// Try to locate the session. If not found, create it.
try
{
userSession = home.findByPrimaryKey(user);
}
catch(ObjectNotFoundException onfe)
{
userSession = home.create(user);
}
// Now increment the value, if found.
// Otherwise, let's initialize it to zero.
int i = 0;
if(userSession.getValue("i") != null)
{
i = ((Integer) userSession.getValue("i")).intValue();
i++;
}
userSession.setValue("i", new Integer(i));
%>
The value of the 'i' for user '<%= user %>' is now <%= i %>.
Listing 6: Caching the EJB home within application scope
// Try to locate the session. If not found, create it.
UserSessionHome home = null;
If(application.getAttribute("UserSessionHome") == null)
{
Context ctx = new InitialContext();
home = (UserSessionHome)
ctx.lookup("sharedSessionSample.UserSession");
application.setAttribute("UserSessionHome", home);
}
else
{
home =(UserSessionHome)
application.getAttribute("UserSessionHome");
}
Listing 7: Caching the EJB reference within session scope
// Try to locate the EJB reference. If not found, create it.
UserSession userSession = null;
If(session.getAttribute("UserSession") == null)
{
try
{
userSession = home.findByPrimaryKey(user);
}
catch(ObjectNotFoundException onfe)
{
userSession = home.create(user);
}
session.setAttribute("UserSession", userSession);
}
else
{
userSession =(UserSession)
session.getAttribute("UserSession");
}