Initializing Application Attributes During Context Initialization

Many applications require some of their attributes to be initialized much before the application is made available to the user or before it can process a request.

Suppose you’ve got an application with a new version that needs to be present across the application?but it needs to be initialized beforehand. The approach outlined in this tip eliminates the process of retrieving that value from the datastore (or permanent storage location) every time such an initialization is required.

The ServletContextListener interface from the javax.servlet package can perform this operation for you.

First, you need to register the class that implements this interface as a listener element in the web.xml. Then, you implement the methods contextInitialized(ServletContextEvent sce) and contextDestroyed(ServletContextEvent sce) as required.

The class that implements this interface receives notification when the context is initialized, as well as when it gets destroyed.

Here’s a sample code fragment:

public class InitializeData implements ServletContextListener {   public void contextInitialized(ServletContextEvent event) {      ServletContext context = event.getServletContext();      setInitialAttribute(context,"app.version",value);       // value can be either from a properties file or from a datasource          // or any-other source   }   public void contextDestroyed(ServletContextEvent event){      //code that needs to be done when the context is destroyed.      //can be like destroying the thread or closing resources etc.   }   private void setInitialAttribute(ServletContext context,      String initParamName,String initialValue) {         context.setAttribute(initParamName, initialValue);   }   //other methods can also come in}

The attribute that is set can be retrieved across the application using the getServletContext().getAttribute(“app.version”) and used appropriately.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a