I think you cannot modify the web.xml file in order to replace certain configuration when your application gets deployed. Based on your web.xml configuration a ServletContext is created first, which allows the servlets to communicate with the container that hosts your application. I don't think there is a way to change the configuration from your web.xml file. To solve your problem you can configure listeners to receive context lifecycle events and perform certain one time initialization such as read a value from external file etc.
One thing you can do is to set the session timeout value programatically.To read a value from external file you can use the servlet context listener intialialization parameters to read the value from a file and store it in some singleton instance -
<listener>
<display-name>ContextLoader</display-name>
<listener-class>com.simple.web.app.ContextLoader</listener-class>
</listener>
<context-param>
<param-name>SessionTimeoutFile</param-name>
<param-value>file_location</param-value>
</context-param>
HttpSession session = request.getSession();
session.setMaxInactiveInterval(value_read_from_text_file);
public class ContextLoader implements ServletContextListener {
/**
* Default constructor.
*/
public ContextLoader() {
// TODO Auto-generated constructor stub
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
ServletContext context = arg0.getServletContext();
System.out.println(context.getInitParameter("SessionTimeoutFile"));
WebProperties.INSTANCE.init(context.getInitParameter("SessionTimeoutFile"))
}
public enum WebProperties {
INSTANCE;
private static Properties PROPERTIES;
public void init(String filePath) {
InputStream inputStream;
try {
inputStream = new FileInputStream(filePath);
if(inputStream != null) {
PROPERTIES = new Properties();
try {
PROPERTIES.load(inputStream);
System.out.println(PROPERTIES.get("value"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public String getConfigurationValue(String key) {
return PROPERTIES.getProperty(key);
}
}
You can then use it in your application by accessing it through the WebProperties -
long sessionValue = Long.parseLong(WebProperties.INSTANCE.getConfigurationValue("value"));
HttpSession session = request.getSession();
session.setMaxInactiveInterval(sessionValue);