System.getProperty(“catalina.base”) There can be scenario where client may use any other server

前端 未结 3 1064
情歌与酒
情歌与酒 2021-01-11 11:12

I am reading a properties file from the Tomcat\\conf\\somename.properties directory using

String demo = System.getProperty(\"catalina.base\") +         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 11:16

    Simply put, don't rely on catalina.base, that is your first problem :)

    Or, more precisely, the java servlet api gives you access to resources inside your web application, so your app is truly portable (not only between servers, but also you can put it anywhere on the file system, zipped as a war or exploded)

    Say you put your file at /WEB-INF/somename.properties, then this is what you do in your servlet, listener, or other web-aware classes:

    getServletContext().getResourceAsStream("/WEB-INF/somename.properties");
    

    See similar question here.

    Another alternative is to use regular java api to search for files in your classpath, e.g. this.getClass().getResource("/somename.properties"). In the case of a web application, this will find such a file located under /WEB-INF/class/ or any jar under /WEB-INF/lib/.

    Finally, if you can't put the file inside your web application, you can put it anywhere on the hard drive and use some config param (e.g. a system property, or a context parameter in web.xml) to refer to it.

提交回复
热议问题