Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing parameter from web.xml inside a MessageResource class

 
Alfredo Delgado Sanchez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have created a class that extends PropertyMessageResources to read Property-Files from outside the Webapplication called MyMessageResources. It works fine. Now I want to store the url or the Path in web.xml like this:

<context-param>
<param-name>LOCATION_EXT_MSG_RES</param-name>
<param-value>http://localhost:8080/bundles</param-value>
<description>Location for external Message-Resources</description>
</context-param>

How can access this value inside the new class MyMessageResources?

Best Regards

ADS
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

you can read the web.xml when your app starts, and then give the parameter you want to MyMessageResources (call a constructor or a setter).

Herman
 
Alfredo Delgado Sanchez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

can you please explain me a little more? I haven't enough experience with Struts. Please help me with this topics:

- Where should I put the code to read the property from web.xml and set to MyMessageResource? You say "when your app starts". Should I extends a class? Which place is this?

- It means that after the MessageResourceFactory creates MyMessageResources I should read the value from the web.xml and set this value on MyMessageResources, right? Should I write my own code to read the web.xml file? When no, hoy can I read on this context (start from app) the web.xml file?

Many thanks

Best Regards

ADS
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest you use an environment vaiable rather than a context parameter. To access a context parameter, you have to have a reference to the ServletContext object, while you can access an environment variable from any class as long as it's runnning in the web application. Example:

web.xml

code in MyMessageResource (probably in the constructor)
 
Alfredo Delgado Sanchez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I used the <env-entry> from web.xml and works fine. I haven't test it, but I read that if some changes will do in web.xml this kind of entries will not be updated. A Tomcat-Server restart is needed.

Many thanks and Best Regards

ADS
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic