JSF 2 – How to Get a context-param Value from web.xml

Using context-param in web.xml we can declare some constant values which will be used by JSF 2 Application.
In this tutorial we are going to show you how to get web application context parameter values in JSF 2 application by using the following two ways-

-Getting the value from inside a backing bean
-Getting the value directly inside a Facelets page using JSF EL.

Declare the context parameter value inside web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- declaring static value with a param name using context-param -->
    <!-- This value is going to be read in our sample application.-->
    <context-param>
        <param-name>myParamName</param-name>
        <param-value>Static param value</param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

context-param.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Reading context parameter example</title>
    </h:head>
    <h:body>
        <h:form>
            <h:panelGrid id="panel1" columns="2" border="1" 
                         cellpadding="5" cellspacing="1">
                <f:facet name="header">
                    <h:outputText value="Reading context parameter example"/>
                </f:facet>
                <h:outputLabel>Read the context parameter through bean</h:outputLabel>
                <h:outputText value="#{paramBean.contextParameter}"/>
                <h:outputLabel>Read the context parameter with the EL</h:outputLabel>
                <h:outputText value="#{initParam['myParamName']}"/>
            </h:panelGrid>
        </h:form>
    </h:body>
</html>

ParamBean.java

package com.example.javaknowledge;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class ParamBean implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * This method returns the myParamName parameter value.
     * @return
     */
    public String getContextParameter() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        String myConstantValue =
                ctx.getExternalContext().getInitParameter("myParamName");
        return myConstantValue;
    }
}

Result Page:
jsf_context-param

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

11,138` Total Views

One comment

  • Hello sir, I’m Guruputra presently working in one IT company, where the project leader and other experienced persons are not ready tell these concepts at all, really i was struggled much for this and i start to learn by myself, while searching things in internet i got this web site. Now i’m learning all the techniques of jsf that might be CRUD or EL and ext.

    Really i’m all ways thanks full to you.

    Regards
    Guruputra K M

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 1 = six