written by Ivan Cerrato
next
J2EE Tomcat JbossJNDI
back to home
J2EE: Override an env-entry. Suppose you have an env-entry in your deploymet descriptor (aka web.xml) that binds some value of some type. Suppose, for example,  that your entry is something like     anEnvEntryInteger   10   java.lang.Integer   so you have anEnvEntryInteger, as Integer, with value of 10. Now you would certainly retireve that value to use it in your code. The following code pernits to  retrieve code from the entry: public String getStr_anEnvEntryInteger() {   InitialContext ic;   try {     ic = new InitialContext();     str_anEnvEntryInteger = "” +      (Integer)ic.lookup("java:comp/env/anEnvEntryInteger");     logger.info("str_anEnvEntryInteger: " + str_anEnvEntryInteger);   } catch (NamingException e) {     e.printStackTrace();   }   return str_anEnvEntryInteger; } note: str_anEnvEntryInteger is defined at class level. How you can notice, the integer value retrieved from the entry has been  casted to String and returned to the caller. If you call the method you’ll obtain something like that Spring_5.service.Service_1Impl - str_anEnvEntryInteger: 10  What’s happened ? We just retrieved, by the means of jndi, the value of the entry. But suppose we want that the env-entry assume a predefined value when the web app is deployed in the production environment, that is, whatever value put in the web.xml be overwritten by the context of the production environment. We will consider Jboss AS and Tomcat.   
back
JBoss AS 6. First of all,  the  .war must  be made with the presence of jboss-web.xml; after you put it in the WEB-INF root directory, the file must contains  a xml structure like that [if gte mso 9]> 0 0 1 52 303 extech 2 1 354 14.0 Normal 0 false false false IT JA X-NONE        configuration_path      configuration_path     EndFragment where configuration_path, is the name of the  entry in the web.xml :       configuration_path    /usr/some_user/some_path    java.lang.String   And now we make the bind in the xxxxxx-service.xml, where it’s mandatory to put                        /Users/SomeUser/jboss-6.0.0.Final/server/default/                        This way we will have the entry overwritten at runtime.
Tomcat 7. We will overwrite the entry only in the context.xml. You can generalize inserting the new entry in the server.xml, and linking it in the context,xml.We still mention the env-entry for clarity:       anEnvEntryInteger    10    java.lang.Integer   Ok, now all that we have to do is create an Environment entry in the context.xml. This way we willl overwrite the value of anEnvEntryInteger.         WEB-INF/web.xml