servlets - Reading properties file in Java web application -


i using servletcontextlistener set paths of jsps in web application. paths stored context parameters in web.xml , retrieved listener:

    @override     public void contextinitialized(servletcontextevent sce) {                 servletcontext sc = sce.getservletcontext();                                    sc.setattribute("urlofthispage", sc.getinitparameter("urlofthispage"));            sc.setattribute("urlofthatpage", sc.getinitparameter("urlofthatpage"));     

in application servlets, path of particular jsp can retrieved servletcontext.

my question relates handling properties file in same way. read lot on other stackoverflow pages 2161045.

am correct in assuming properties file should read listener , stored in servletcontext using property object? if case, how retrieve particular property properties file?

at moment using sort of code in servlets value of attribute servletcontext.

string url = (string) sc.getattribute("urlofthispage");  // use servletcontext jsp's url.     

but not sure how extend accessing properties file.

i have tried following in servletcontextlistener:

    properties properties = new properties();     properties.setproperty("name", "akechi jinsai");     sc.setattribute("properties", properties); 

and in servlet, using code:

   servletcontext sc = request.getsession().getservletcontext();            properties properties = (properties) sc.getattribute("properties");    system.out.println("here: " + properties.getproperty("name")); 

"here: akechi jinsai" displayed there better way of getting single property in servlet without looking things in way?

simply load properties file in servlet , move values hashmap , store application attribute. access in jsp using javaserver pages standard tag library.

read more load properties file in servlet/jsp.


sample code:

jsp: (different ways access map)

<c:foreach items="${map}" var="entry">     key="${entry.key}" value=${entry.value} </c:foreach>  url of page = ${map.urlofthispage } url of page = ${map.urlofthatpage }   url of page = ${map['urlofthispage'] } url of page = ${map['urlofthatpage'] } 

servletcontextlistener

public class myservletcontextlistener implements servletcontextlistener {      @override     public void contextdestroyed(servletcontextevent sc) {      }      @override     public void contextinitialized(servletcontextevent sce) {         servletcontext sc = sce.getservletcontext();         // load properties file if needed         // read path web.xml init parameter           map<string, string> map = new hashmap<string, string>();         map.put("urlofthispage", sc.getinitparameter("urlofthispage"));         map.put("urlofthatpage", sc.getinitparameter("urlofthatpage"));          sc.setattribute("map", map);     }  } 

web.xml:

<context-param>     <param-name>urlofthispage</param-name>     <param-value>url</param-value> </context-param> <context-param>     <param-name>urlofthatpage</param-name>     <param-value>url</param-value> </context-param>  <listener>     <listener-class>com.x.y.z.myservletcontextlistener</listener-class> </listener> 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -