Re: loading properties file from WEB-INF instead of WEB-INF/classes
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 David, On 4/18/2010 2:27 PM, David Smith wrote: ServletContext.getResource( path ) takes path as relative to the current webapp and returns a URL for opening the resource or null if the resource was not found. also there is -- ServletContext.getResourceAsStream( path ) takes a path as relative to the current webapp and returns an i/o stream for reading the file. Yes, please use ServletContext.getResourceAsStream. both of these are well documented in the servlet spec. And in the Servlet API itself if you don't want to dig into the spec just to read the javadoc. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkvMyzoACgkQ9CaO5/Lv0PAG5QCgmmoU7Blc/z1Gzkl3uwOvEUmu K7wAoJwSb/lD3sVxaYpcg0gq/323BMH3 =N2lO -END PGP SIGNATURE- - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: loading properties file from WEB-INF instead of WEB-INF/classes
2010/4/17 Thufir hawat.thu...@gmail.com getPropsFromWebINF works so long as the properties file is within the package of the class (/. However, I'd like to put the properties file under WEB-INF: public void getPropsFromWebINF() throws IOException { Properties p = new Properties(); InputStream is; is = getClass().getResourceAsStream(sqljdbc4.properties); p.load(is); log(p.toString()); } Class.getResourceAsStream() will delegate to the classloader to find the resource (searches the classpath) , see http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String) The tomcat site has an excellent description of Tomcat's classloading : http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html , this should bring you to the conclusion that the above won't work. You should put your sqljdbc4.properties file in WEB-INF/classes, or in WEB-INF/lib if packaged in a jar file. If you reference it from your application, make sure you prepend a / (or it will search in your servlet's package) . I've seen mention of using: System.getenv(APP_PROPERTIES); in conjunction with a context (context.xml under META-INF) along the lines of: ?xml version=1.0 encoding=UTF-8? Context antiJARLocking=true path=/A00720398sat Environment name=APP_PROPERTIES description=The APP Properties File override=false type=java.lang.String value=/WEB-INF/app.properties / /Context however, I'm not able to get the environment, I just get a null value. can you be a bit more specific where you get a null value (show some lines of code)... although the above won't bring you to the content of the property file, you only have defined a key/value pair. regards, Harry What's the correct, and simple, idiom? (Staying away from jndi and dbcp for now, and, oddly enough, servlets in this case.) Actually, I suppose in a sense it's in WEB-INF: dtc01l0376-06:~ a00720398$ dtc01l0376-06:~ a00720398$ jar -tfv NetBeansProjects/A00720398sat/dist/A00720398sat.war | grep sql 498 Sat Apr 17 14:36:14 PDT 2010 WEB-INF/classes/controller/sqljdbc4.properties however, I'd like to move it from WEB-INF/classes up to just WEB-INF. thanks, Thufir - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: loading properties file from WEB-INF instead of WEB-INF/classes
props file can be placed in src package and then placed along with classes in the Web-INF directory when compiling with the destination flag .This method has been used since jdk 1 for internationalization and is understood by new and old devs. On a side not perhaps someone can help me with a related question regarding how we intwenationalize images? or a link:P -- From: Harry Metske harry.met...@gmail.com Sent: Sunday, April 18, 2010 7:32 PM To: Tomcat Users List users@tomcat.apache.org Subject: Re: loading properties file from WEB-INF instead of WEB-INF/classes 2010/4/17 Thufir hawat.thu...@gmail.com getPropsFromWebINF works so long as the properties file is within the package of the class (/. However, I'd like to put the properties file under WEB-INF: public void getPropsFromWebINF() throws IOException { Properties p = new Properties(); InputStream is; is = getClass().getResourceAsStream(sqljdbc4.properties); p.load(is); log(p.toString()); } Class.getResourceAsStream() will delegate to the classloader to find the resource (searches the classpath) , see http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String) The tomcat site has an excellent description of Tomcat's classloading : http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html , this should bring you to the conclusion that the above won't work. You should put your sqljdbc4.properties file in WEB-INF/classes, or in WEB-INF/lib if packaged in a jar file. If you reference it from your application, make sure you prepend a / (or it will search in your servlet's package) . I've seen mention of using: System.getenv(APP_PROPERTIES); in conjunction with a context (context.xml under META-INF) along the lines of: ?xml version=1.0 encoding=UTF-8? Context antiJARLocking=true path=/A00720398sat Environment name=APP_PROPERTIES description=The APP Properties File override=false type=java.lang.String value=/WEB-INF/app.properties / /Context however, I'm not able to get the environment, I just get a null value. can you be a bit more specific where you get a null value (show some lines of code)... although the above won't bring you to the content of the property file, you only have defined a key/value pair. regards, Harry What's the correct, and simple, idiom? (Staying away from jndi and dbcp for now, and, oddly enough, servlets in this case.) Actually, I suppose in a sense it's in WEB-INF: dtc01l0376-06:~ a00720398$ dtc01l0376-06:~ a00720398$ jar -tfv NetBeansProjects/A00720398sat/dist/A00720398sat.war | grep sql 498 Sat Apr 17 14:36:14 PDT 2010 WEB-INF/classes/controller/sqljdbc4.properties however, I'd like to move it from WEB-INF/classes up to just WEB-INF. thanks, Thufir - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: loading properties file from WEB-INF instead of WEB-INF/classes
An alternative exists in the ServletContext class -- ServletContext.getResource( path ) takes path as relative to the current webapp and returns a URL for opening the resource or null if the resource was not found. also there is -- ServletContext.getResourceAsStream( path ) takes a path as relative to the current webapp and returns an i/o stream for reading the file. For example, using this within a servlet: try { java.io.InputStream res_stream = this.getServletContext().getResourceAsStream('/WEB-INF/my-config.ini') ; // read the data } catch (java.io.IOException ioe) { // handle the problem } both of these are well documented in the servlet spec. --David On 4/19/2010 2:02 PM, Yucca Nel wrote: props file can be placed in src package and then placed along with classes in the Web-INF directory when compiling with the destination flag .This method has been used since jdk 1 for internationalization and is understood by new and old devs. On a side not perhaps someone can help me with a related question regarding how we intwenationalize images? or a link:P -- From: Harry Metske harry.met...@gmail.com Sent: Sunday, April 18, 2010 7:32 PM To: Tomcat Users List users@tomcat.apache.org Subject: Re: loading properties file from WEB-INF instead of WEB-INF/classes 2010/4/17 Thufir hawat.thu...@gmail.com getPropsFromWebINF works so long as the properties file is within the package of the class (/. However, I'd like to put the properties file under WEB-INF: public void getPropsFromWebINF() throws IOException { Properties p = new Properties(); InputStream is; is = getClass().getResourceAsStream(sqljdbc4.properties); p.load(is); log(p.toString()); } Class.getResourceAsStream() will delegate to the classloader to find the resource (searches the classpath) , see http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String) The tomcat site has an excellent description of Tomcat's classloading : http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html , this should bring you to the conclusion that the above won't work. You should put your sqljdbc4.properties file in WEB-INF/classes, or in WEB-INF/lib if packaged in a jar file. If you reference it from your application, make sure you prepend a / (or it will search in your servlet's package) . I've seen mention of using: System.getenv(APP_PROPERTIES); in conjunction with a context (context.xml under META-INF) along the lines of: ?xml version=1.0 encoding=UTF-8? Context antiJARLocking=true path=/A00720398sat Environment name=APP_PROPERTIES description=The APP Properties File override=false type=java.lang.String value=/WEB-INF/app.properties / /Context however, I'm not able to get the environment, I just get a null value. can you be a bit more specific where you get a null value (show some lines of code)... although the above won't bring you to the content of the property file, you only have defined a key/value pair. regards, Harry What's the correct, and simple, idiom? (Staying away from jndi and dbcp for now, and, oddly enough, servlets in this case.) Actually, I suppose in a sense it's in WEB-INF: dtc01l0376-06:~ a00720398$ dtc01l0376-06:~ a00720398$ jar -tfv NetBeansProjects/A00720398sat/dist/A00720398sat.war | grep sql 498 Sat Apr 17 14:36:14 PDT 2010 WEB-INF/classes/controller/sqljdbc4.properties however, I'd like to move it from WEB-INF/classes up to just WEB-INF. thanks, Thufir - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: loading properties file from WEB-INF instead of WEB-INF/classes
Yucca Nel wrote: props file can be placed in src package and then placed along with classes in the Web-INF directory when compiling with the destination flag .This method has been used since jdk 1 for internationalization and is understood by new and old devs. On a side not perhaps someone can help me with a related question regarding how we intwenationalize images? or a link:P Not quite sure what you mean by this, but assuming you mean that there is a link to country-flag.gif, and depending on the user's origin, you want to send back the appropriate one, in my simple mind I would create property files with the appropriate translation to the real thing, and use them in my application to return the appropriate image (or redirect to it). - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
loading properties file from WEB-INF instead of WEB-INF/classes
getPropsFromWebINF works so long as the properties file is within the package of the class (/. However, I'd like to put the properties file under WEB-INF: public void getPropsFromWebINF() throws IOException { Properties p = new Properties(); InputStream is; is = getClass().getResourceAsStream(sqljdbc4.properties); p.load(is); log(p.toString()); } I've seen mention of using: System.getenv(APP_PROPERTIES); in conjunction with a context (context.xml under META-INF) along the lines of: ?xml version=1.0 encoding=UTF-8? Context antiJARLocking=true path=/A00720398sat Environment name=APP_PROPERTIES description=The APP Properties File override=false type=java.lang.String value=/WEB-INF/app.properties / /Context however, I'm not able to get the environment, I just get a null value. What's the correct, and simple, idiom? (Staying away from jndi and dbcp for now, and, oddly enough, servlets in this case.) Actually, I suppose in a sense it's in WEB-INF: dtc01l0376-06:~ a00720398$ dtc01l0376-06:~ a00720398$ jar -tfv NetBeansProjects/A00720398sat/dist/A00720398sat.war | grep sql 498 Sat Apr 17 14:36:14 PDT 2010 WEB-INF/classes/controller/sqljdbc4.properties however, I'd like to move it from WEB-INF/classes up to just WEB-INF. thanks, Thufir - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org