This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes How can I access System Properties to determine a param-value for a context-param in Web.xml? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How can I access System Properties to determine a param-value for a context-param in Web.xml?" Watch "How can I access System Properties to determine a param-value for a context-param in Web.xml?" New topic
Author

How can I access System Properties to determine a param-value for a context-param in Web.xml?

George Andrews
Greenhorn

Joined: Feb 24, 2010
Posts: 3
I am currently trying to use System Properties to determine which property file to load for a Generic Servlet as described below. I would like to access the System Property "platform.type" in particular, though if this is possible, I will also use others such as "serverName".



I have already been through multiple posts regarding the following: ContextLoaderListener and ContextLoaderServlet. They will not work for this case because I need those properties accessible when the Generic Servlet accesses the above listed Context Parameter.

I have even created a Wrapper Servlet, let's call it GenericInitializationServlet, in which I attempt to replace the properties by: using a ServletConfig Proxy to access the Context Properties using the getContextParameter method and then substituting the property value with the matching System Property.

Example:



Does anyone know of a way to accomplish this? To reiterate, I need to perform property replacement on a Context Parameter that will be used by a Servlet.

Thanks!


--
Whether you think you can or whether you think you can't, you're right. ~ Henry Ford.
Tomasz Szymanski
Greenhorn

Joined: Jan 11, 2010
Posts: 23
I'm pretty sure you can't manipulate the init parameters in a container-independent way. There is simply no standard API for this.

One of the things I can think of is using your own ServletContext implementation, that would wrap the original one and implement the getInitParameter method appropriately. Is that what you meant by "proxy"? Have you tried it?
George Andrews
Greenhorn

Joined: Feb 24, 2010
Posts: 3
Below you can find the code I was able to get to work:

web.xml:



WrapperServlet.java:



ParameterSubstituter.java:



Props to M Simpson in this post for giving me some direction: http://www.coderanch.com/t/79094/Websphere/environment-variable-referance-Web-xml
Tomasz Szymanski
Greenhorn

Joined: Jan 11, 2010
Posts: 23
OK, so you're constructing a real proxy. I was thinking of a simple wrapper, something like ServletRequestWrapper or ServletResponseWrapper. Something like:



But actually that would be a much longer piece of code than yours, I guess So... what's the problem anyway? Isn't your solution sufficient?
George Andrews
Greenhorn

Joined: Feb 24, 2010
Posts: 3
Below you can find the code I was able to get to work:


Hi Tomasz,

Actually, I was able to get this to work and posted the solution I am currently using. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I access System Properties to determine a param-value for a context-param in Web.xml?