Thursday, October 14, 2010

tomcat & servlet resource

Tomcat::configuration of system properties from webapp

SystemPropertiesListener.java
import java.util.Enumeration;
import javax.servlet.*;

public class SystemPropertiesListener implements
javax.servlet.ServletContextListener {
private ServletContext context = null;

public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
Enumeration params = context.getInitParameterNames();

while (params.hasMoreElements()) {
String param = (String) params.nextElement();
String value = context.getInitParameter(param);
if (param.startsWith("prefix.")) {
System.setProperty(param, value);
}
}
}

public void contextDestroyed(ServletContextEvent event) {
}
}

web.xml

<context-param>
<param-name>prefix.property</param-name>
<param-value>value</param-value>
<param-type>java.lang.String</param-type>
</context-param>

<listener>
<listener-class>servletUtils.SystemPropertiesHelper</listener-class>
</listener>


Resources
[1] http://stackoverflow.com/questions/372686/how-can-i-specify-system-properties-in-tomcat-configuration-on-startup

[2] http://stackoverflow.com/questions/558502/how-a-servlet-can-get-the-absolute-path-to-a-file-outside-of-the-servlet
[3] http://stackoverflow.com/questions/2194930/tomcat-how-can-i-place-parameters-in-web-xml-and-fetch-them-in-my-application

No comments:

PostgreSQL json fields

select name->'es_PE',* from product_template where name->>'es_PE' like '%MEGACI%'   References: [1] https:/...