package e4s.tutorial; import java.io.*; import java.util.*; import java.text.*; import java.net.*; import java.sql.*; import e4s.html.*; import e4s.servlet.*; import e4s.application.sysmodule.E4ModuleSystem; /** * This example illustrates the usage of a module, that is called several times * by the e4s framework. Each call causes the framework to create * a new instance of this module, so any values stored in member variables are * lost after each call. * * There are workarounds throught that: * * 1) Make a variable static, but you loose the ability to hold different values * for each user. Anyway, it can make sense, e.g. when using image or font * objects that are independent of the user. * * 2) Declare the whole module as session object (see other example in this tutorial). * * Or covered here: * * 3) You can concentrate data in one or more classes (objects) and use the {@link e4s.servlet.E4ServletImplementation_Intf#setSessionObject} * or {@link e4s.servlet.E4ServletImplementation_Intf#setSessionObject} which is basically also the mechanism how * more complex objects (trees, sorted lists) in the e4s environment are working. * * {@tutorial Example_PersistantModule} * * @see e4s.servlet.E4ModuleImplementation#E4ModuleImplementation(E4ServletImplementation_Intf,boolean) * @see Example_SessionObjects */ public class Example_SessionObjects extends E4ModuleImplementation { public static E4Method start = null; private int m_test = 0; /** * This is the entry point into this module. * * Store the current time in two local member variables (one for the first call, * and another for the last call). Place a link to the second function within this * module. * * @param html the output to be created by this function */ public void start( HTML html ) { java.util.Date first_date = (java.util.Date)getSessionObject("TUTORIAL.STARTED"); java.util.Date last_date = (java.util.Date)getSessionObject("TUTORIAL.LASTTIME"); // get call counter long called = getSessionObject_Long("TUTORIAL.CALLED"); if (last_date == null) { html.println("This function was now called for the first time, "); html.println("a session object of type java.util.Date named TUTORIAL.STARTED will be stored now"); // remember the time when it was first started setSessionObject("TUTORIAL.STARTED", new java.util.Date()); } else { html.println("This function was called " + called + " times before, the first call has been at " + first_date.toString()); html.println(" and the last call at " + last_date.toString()); html.P(); html.println("The local variable m_test still has a value of " + m_test + " as this module will be instantiated each call and not each session"); } // remember the last date called setSessionObject("TUTORIAL.LASTTIME", new java.util.Date()); // increment & store call counter called++; setSessionObject("TUTORIAL.CALLED", called); // this won't work the way we expect m_test++; // add some links html.P(); // open system function to display session objects in a new window A hrefList = html.A(E4ModuleSystem.listSessionObjects); hrefList.openInNewWindow(A.WINDOW_PARAM_RESIZE|A.WINDOW_PARAM_SCROLL); hrefList.println("[view objects for the current session]"); // link again to call this function A hrefAgain = html.A(start); hrefAgain.println("[do it again]"); } }