Class WebApp
- All Implemented Interfaces:
jakarta.servlet.ServletContextListener, EventListener
Initializes and stores various variables useful for all servlets/pages
running in our JVM. Implements ServletContextListener
and initializes itself when informed by the servlet container's context initialization event.
It's optional to use this class. If it is used, it's configured by adding the following to the appropriate sections of WEB-INF/web.xml:
<context-param>
<param-name>configfile</param-name>
<param-value>app.conf</param-value>
</context-param>
<context-param>
<param-name>appName</param-name>
<param-value>some-arbitrary-string-unique-across-all-webapps</param-value>
</context-param>
<listener>
<listener-class>fc.web.servlet.WebApp</listener-class>
</listener>
Important:If this class is used invalid input: '<'font and its initialization is not successful, it tries to shut down the entire servlet JVM by calling System.exit. (The idea being it's better to fail early and safely then continue beyond this point).
If used, this class requires the following context configuration parameter:
- configfile: Path/name of the application configuration file. If the path starts with a '/', it is an absolute file system path. Otherwise, it is relative to this context root's WEB-INF directory.
- appName: Some arbitrary (but unique) name associated with this webapp
This class can also be subclassed to initialize/contain website specific data and background/helper processing threads. Alternatively, along with this class as-is, additional independent site-specific ServletContextListener classes can be created and used as necessary.
-
Field Summary
FieldsModifier and TypeFieldDescriptionA (initially empty) Map that servlets can use to store a reference to themselves.ALogobject.long -
Constructor Summary
ConstructorsConstructorDescriptionWebApp()A no-arg constructor public such that the servlet container can instantiate this class. -
Method Summary
Modifier and TypeMethodDescriptionstatic WebAppAdd a new WebApp instance manually.voidcontextDestroyed(jakarta.servlet.ServletContextEvent sce) Basic implementation of the web application cleanup upon context destruction.voidcontextInitialized(jakarta.servlet.ServletContextEvent sce) Basic implementation of the web application cleanup upon context creation.Returns the specified object from the global application map or null if the object was not found.getConnection(String databasename) Returns the connection manager corresponding to the default database name.getConnectionMgr(String databasename) Returns the connection manager corresponding to the database name.Convenience method to get the pre-created application cache.Convenience method to return a form stored previously via theputForm(fc.web.forms.Form)method.static WebAppgetInstance(String appName) Returns the property manager associated with this WebApp (properties of the app configuration file can be read via this property mgr)Returns the default ThreadLocalCalendar object (a new ThreadLocalCalendar is created if it does not exist).getThreadLocalCalendar(String name) Returns theThreadLocalCalendarobject corresponding to the specified name (a new ThreadLocalCalendar is created if it does not exist).Returns the default threadLocalDateFormat object (a new ThreadLocalDateFormat is created if it does not exist).Returns theThreadLocalDateFormatobject corresponding to the specified name (a new ThreadLocalDateFormat is created if it does not exist).Returns the defaultThreadLocalNumberFormatobject (a new ThreadLocalNumberFormat is created if it does not exist).Returns the ThreadLocalNumberFormat object corresponding to the specified name (a new ThreadLocalNumberFormat is created if it does not exist).getThreadLocalObject(String name) Returns theThreadLocalObjectcorresponding to the specified name (a new ThreadLocalObject is created if it does not exist).Returns the default ThreadLocalRandom object (a new ThreadLocalRandom is created if it does not exist).getThreadLocalRandom(String name) Returns theThreadLocalRandomobject corresponding to the specified name (a new ThreadLocalRandom is created if it does not exist).voidPuts the specified key/object into the global application map.voidvoidremoveForm(String name) toString()
-
Field Details
-
connectionManagers
-
defaultConnectionManager
-
default_dbcache_time
-
default_tlcal
-
default_tldf
-
default_tlnf
-
default_tlrand
-
appMap
-
tlcalMap
-
tldfMap
-
tlnfMap
-
tlrandMap
-
tlMap
-
appLog
-
propertyMgr
-
allServletsMap
A (initially empty) Map that servlets can use to store a reference to themselves. This is required because the servlet API has deprecated a similar API call (the servlet API authors are brain damaged 'tards).
-
-
Constructor Details
-
WebApp
public WebApp()A no-arg constructor public such that the servlet container can instantiate this class.
-
-
Method Details
-
getInstance
-
contextInitialized
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:
contextInitializedin interfacejakarta.servlet.ServletContextListener
-
contextDestroyed
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:
contextDestroyedin interfacejakarta.servlet.ServletContextListener
-
addWebApp
public static WebApp addWebApp(jakarta.servlet.ServletContext context, String appName, 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.
- Parameters:
context- the servlet context the calling servlet is running inappName- the name of the WebApp to associate with the calling servletconf- the configuration file for the WebApp
-
getPropertyMgr
Returns the property manager associated with this WebApp (properties of the app configuration file can be read via this property mgr) -
getConnectionMgr
Returns the connection manager corresponding to the database name. (specified in the dblist property of app.conf).- Throws:
IllegalArgumentException- if the specified database is not found
-
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
- Throws:
SQLException
-
getConnection
- Throws:
SQLException
-
getAppLog
-
getForm
Convenience method to return a form stored previously via theputForm(fc.web.forms.Form)method. Returns null if no form with the specified name was found. -
putForm
-
removeForm
-
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 invalid input: '{@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.' -
getThreadLocalDateFormat
Returns theThreadLocalDateFormatobject corresponding to the specified name (a new ThreadLocalDateFormat is created if it does not exist). -
getThreadLocalDateFormat
Returns the default threadLocalDateFormat object (a new ThreadLocalDateFormat is created if it does not exist). -
getThreadLocalNumberFormat
Returns the ThreadLocalNumberFormat object corresponding to the specified name (a new ThreadLocalNumberFormat is created if it does not exist). -
getThreadLocalNumberFormat
Returns the defaultThreadLocalNumberFormatobject (a new ThreadLocalNumberFormat is created if it does not exist). -
getThreadLocalCalendar
Returns theThreadLocalCalendarobject corresponding to the specified name (a new ThreadLocalCalendar is created if it does not exist). -
getThreadLocalCalendar
Returns the default ThreadLocalCalendar object (a new ThreadLocalCalendar is created if it does not exist). -
getThreadLocalRandom
Returns theThreadLocalRandomobject corresponding to the specified name (a new ThreadLocalRandom is created if it does not exist). -
getThreadLocalRandom
Returns the default ThreadLocalRandom object (a new ThreadLocalRandom is created if it does not exist). -
getThreadLocalObject
Returns theThreadLocalObjectcorresponding to the specified name (a new ThreadLocalObject is created if it does not exist). -
get
-
put
-
toString
-