1

I have META-INF/context.xml file, which looks something like below

 <?xml version='1.0' encoding='utf-8'?>
    <Context displayName="Game Application Dev">
     <Environment 
        name="myName" 
        value="HelloWorldApp" 
        type="java.lang.String" 
        description="This is my envName"/>
    </Context>

By any chance is it possible to read this myName entry in my .xhtml file without using any bean (which does the explicit jndi lookup). I know this can be done with the jndi lookup, but my requirement is to achieve the same without using any beans.

Edit 1: My Target server : Apache Tomcat

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ashish Burnwal
  • 626
  • 8
  • 13
  • I suppose it is the context.xml targeted to Apache Tomcat? And you can not rewrite it as ``? – Selaron Oct 08 '19 at 12:28
  • @Selaron: Yes context.xml is targeted to Apache Tomcat. Yes I can rewrite or make changes in context.xml file. – Ashish Burnwal Oct 08 '19 at 13:14
  • Why is it this your requirement? Why without **ANY** bean? And JSF used CDI for all injection/resource etc, so look for a CDI based solution – Kukeltje Oct 08 '19 at 13:16

2 Answers2

3

While it would still be interesting to know why you cannot add a bean to your project, one chance to access String typed parameters from Apache Tomcat context.xml is the getInitParam(String) method from ExternalContext:

Rewrite your <Environment .../> element to a <Parameter .../>:

<Parameter
    name="myName" 
    value="HelloWorldApp" />

And in your xhtml write:

This is a #{facesContext.externalContext.getInitParameter('myName')}!


Another possibility is to create a custom EL-Function. But if you can't create beans, this is likely not an option for you either.

Selaron
  • 6,105
  • 4
  • 31
  • 39
2

You can use initParam['myName' ] The catch point here is that initParam is an implicit function and the equivalent corresponding method is defined in the ServletConfig getInitParameter(String paramName). The accepted answer will also work like charm, and using initParam is another way to get the work done I found an interesting link which explains the fundamentals very clearly.

Servlet config

Little background, when servlet loads, then all the context related information is also loaded. And since you want to access one such info, first you will make sure that you rewrite your element to a :

<Parameter
    name="myName" 
    value="HelloWorldApp" />

And then access it in either two ways.

{facesContext.externalContext.getInitParameter('myName')}

Or

initParam['myName' ]

Amrita Sahoo
  • 118
  • 1
  • 1
  • 6