SurveyEngine

The database is accessed via the SurveyEngine object.

Figure 10.6. SurveyEngine.java (excerpt)


  private transient SurveyDatabase database;

  public SurveyDatabase getDatabase()
  {
    return database;
  }
  
  protected void setupForRequest(RequestContext context)
  {
    super.setupForRequest(context);
    
    if (database == null)
    {
      String name = "Survey.database";
      ServletContext servletContext;
      
      servletContext = context.getServlet().getServletContext();
      
      database = (SurveyDatabase)servletContext.getAttribute(name);
      
      if (database == null)
      {
        database = new SurveyDatabase();
        servletContext.setAttribute(name, database);
      }
    }
  }

The SurveyDatabase instance is stored as a named attribute of the ServletContext, a shared space available to all sessions.