I've a GWT Servlet running in a Tomcat 6.0 server. This server acts as a proxy to another service. This final service may be running in different IPs and/or ports in my network.
How can I configure my GWT Servlet to connect to any of my services without manually modifying the web.xml file?
I'm initializing my servlet with:
<!-- Servlets -->
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.proxy.MyServletServiceImpl</servlet-class>
<init-param>
<param-name>serverAddress</param-name>
<param-value>192.168.1.10</param-value>
</init-param>
<init-param>
<param-name>serverPort</param-name>
<param-value>55005&l开发者_如何学运维t;/param-value>
</init-param>
</servlet>
From inside my MyServletSerciveImpl.java file I'm doing
private void loadConfig() {
ServletConfig config = this.getServletConfig();
serverAddress = config.getInitParameter("serverAddress");
serverPort = Integer.valueOf(config.getInitParameter("serverPort"));
}
My ideal case would be that this configuration is the default, but applying some configuration file (a properpies file, xml, ini, cfg, .....) I could overwrite the default web.xml values.
Any idea how to do that?
Thanks.
For true dynamic configuration, you can expose a configuration object as a jmx bean, and have your servlet use that bean.
An intermediate solution is to put the configuration in a different file, as xml or properties, or in a db table, and read from it periodically in a background thread.
For completeness:
public class MyServiceImpl extends RemoteServiceServlet implements
MyService {
private void loadConfig() {
InputStream inStream = this.getServletContext().getResourceAsStream("/WEB-INF/config.properties");
Properties properties = new Properties();
try {
properties.load(inStream);
// properties.getProperty("myValue");
} catch (IOException e) {
Log.error(e.getMessage());
e.printStackTrace();
}
}
....
}
精彩评论