Search code examples
javaxmljakarta-eewebservletcontextlistener

loading the property file values into web.xml using servletContextListener


i want load the values form property file in to my web.xml

this is my web.xml file

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">


    <!-- Property listeners -->
    <listener>       
      <listener-class>com.kpowd.utility.PropertyReading</listener-class>
   </listener>  

    <display-name>JSF 2 + Spring 3 Integration example</display-name>
    <!-- The welcome page -->
    <welcome-file-list>
        <welcome-file>login.xhtml</welcome-file>
    </welcome-file-list>

    <!-- Spring listeners -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-security.xml,
            /WEB-INF/applicationContext.xml            
        </param-value>
    </context-param>
    <!-- Change the primeface theme -->

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>${primefacestheme}</param-value>
    </context-param>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Start JSF -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- JSF URL mapping -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

</web-app>

and this is my servelet context listner class

package com.kpowd.utility;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class PropertyReading implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // TODO Auto-generated method stub

    }

    @Override
    public void contextInitialized(ServletContextEvent event) {

        final String props = "/config.properties";
        final Properties propsFromFile = new Properties();
        try {               
            propsFromFile.load(getClass().getResourceAsStream(props));

        } catch (final IOException e) {
            e.printStackTrace();
        }
        for (String prop : propsFromFile.stringPropertyNames())
          {
             if (System.getProperty(prop) == null)
             {
                 System.setProperty(prop, propsFromFile.getProperty(prop));
             }
          }

    }

}

when i print the values form this class it shows me the proprety values but in web.xml when im trying to access ${primefacestheme} this value it doesn't load

and this is my config.properties file

primefacestheme=glass-x
wellcomepage=accdenied.xhtml

please help me


Solution

  • Can you check with change in web.xml:

    <context-param>
       <param-name>primefacestheme</param-name> 
       <param-value>${primefacestheme}</param-value>
    </context-param>
    

    As config.properties having property key name as primefacestheme and in code we are checking with that key name and if its null then setting property value to that key name.

    In short <param-name> and property key name need to be same.