Skip navigation links

Class WebApp

    • Constructor Detail

      • WebApp

        public WebApp()
        A no-arg constructor public such that the servlet container can instantiate this class.
    • Method Detail

      • contextInitialized

        public void contextInitialized(ServletContextEvent sce)
        Basic implementation of the web application cleanup upon context creation. If this method is subclassed, the subclassed method must also invoke this method via super.contextInitialized. This call should typically be at the beginning of the subclassed method, which allows this implementation to create connections, logs etc, which can then be used by the subclass to finish it's further initialization as needed.
        Specified by:
        contextInitialized in interface ServletContextListener
      • contextDestroyed

        public void contextDestroyed(ServletContextEvent sce)
        Basic implementation of the web application cleanup upon context destruction. If this method is subclassed, the subclassed method must also invoke this method via super.contextInitialized. This invokation should typically be at the end of the subclassed method (which allows the subclass to use connections etc., before they are closed by this superclass method).
        Specified by:
        contextDestroyed in interface ServletContextListener
      • addWebApp

        public static WebApp addWebApp(ServletContext context,
                                       java.lang.String appName,
                                       java.lang.String sconf)
        Add a new WebApp instance manually. A WebApp is typically specified as a context listener, in a particular web.xml, and configures itself as representing that context wide configuration (common to all servlets and pages running inside that context). There is only 1 instance of the WebApp class that is instantiated per context by the servlet container.

        However, for REST services and other API's, it is useful to have several API versions, such as /rest/v1/, /rest/v2/, etc., all of which are tied to seperate servlets in the same webapp. We could also create separate webapps inside separate folders, such as /w3root/rest/v1, /w3root/rest/v2, each of which would have their own web.xml (and hence separate configuration).

        However, we sometimes need to access a particular REST api's classes directly from the "root" document webapp (which is running molly pages, etc). If the REST api's are in separate contexts, the root webapp (a different webapp from all the other REST api versioned webapps) cannot access those api classes directly. This becomes a hassle if we want to use our API directly to show results on a molly web page.

        So we use only one webapp, with separate servlets in that webapp. Example: RESTServletV1, RESTServletV2, etc., to handle and serve different version of the API.

        So then, each of these servlets have to be configured as well with connection pools, loggers, etc, each of them specific to a particular servlet/api. This can be done via servlet init parameters, but since the whole point of WebApp is to set up this configuration easily, it is also useful to create a WebApp instance per servlet instance in the same webapp.

        Servlets can then call this method with different appNames (unique to each servlet) and different configuration files (again unique to each servlet). When the context is destroyed, all these manually added webapps will also be automatically closed.

        This class must be specified as a listener in web.xml (even if the context wide config file has no configuration data, use an empty config file in that case).

        PS: If this makes your head hurt, you are in good company. My head hurts too.

        context - the servlet context the calling servlet is running in
        appName - the name of the WebApp to associate with the calling servlet
        conf - the configuration file for the WebApp
      • getPropertyMgr

        public PropertyMgr getPropertyMgr()
        Returns the property manager associated with this WebApp (properties of the app configuration file can be read via this property mgr)
      • getConnectionMgr

        public ConnectionMgr getConnectionMgr(java.lang.String databasename)
        Returns the connection manager corresponding to the database name. (specified in the dblist property of app.conf).
        java.lang.IllegalArgumentException - if the specified database is not found
      • getConnectionMgr

        public ConnectionMgr getConnectionMgr()
        Returns the connection manager corresponding to the default database name. (specified in the dbdefault property of app.conf). Returns null if no default database has been initialized.
      • getConnection

        public java.sql.Connection getConnection(java.lang.String databasename)
                                          throws java.sql.SQLException
      • getConnection

        public java.sql.Connection getConnection()
                                          throws java.sql.SQLException
      • getForm

        public Form getForm(java.lang.String name)
        Convenience method to return a form stored previously via the putForm method. Returns null if no form with the specified name was found.
      • removeForm

        public void removeForm(java.lang.String name)
      • getDBCache

        public Cache getDBCache()
        Convenience method to get the pre-created application cache. This can be used for caching database results as necessary. Each entry in the cache can be stored for entry-specific time-to-live (see {@link Cache) but if no entry-specific-time-to-live is specified, then entries are cached for a default time of 5 minutes. Of course, cached entries should always be invalidated sooner whenever the database is modified.
      • getThreadLocalNumberFormat

        public ThreadLocalNumberFormat getThreadLocalNumberFormat(java.lang.String name)
        Returns the ThreadLocalNumberFormat object corresponding to the specified name (a new ThreadLocalNumberFormat is created if it does not exist).
      • get

        public java.lang.Object get(java.lang.Object key)
        Returns the specified object from the global application map or null if the object was not found.
      • put

        public void put(java.lang.Object key,
                        java.lang.Object val)
        Puts the specified key/object into the global application map.
      • toString

        public java.lang.String toString()
        toString in class java.lang.Object