0

How to read value from Session.txt and pass that value to web.xml? I am trying to set session-timeout for a Java web application by placing the Session.txt file outside the project folder and reading the value to web.xml.

<session-config>
        <session-timeout>Value read from Session.txt</session-timeout>
</session-config>

Session.txt
60

Jaap
  • 81,064
  • 34
  • 182
  • 193
Pavithra
  • 29
  • 1
  • 1
  • 14

2 Answers2

1

Imagine this is your web.xml file

 <session-config>
        <session-timeout>Value read from Session.txt</session-timeout>
</session-config>

This code of java replaces tag values You can use more efficient regex if you want or XML parser

 String str = "<session-config>this it to be replaced</session-config>";
    System.out.println(str.replaceAll("(?<=<session-config>)(.*?)(?=</session-config>)", "replacement of value read from text file"));

This code can help you reading text file

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);//replaceable content 
KishanCS
  • 1,357
  • 1
  • 19
  • 38
1

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);
Innocuous
  • 84
  • 2
  • 8