<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FactoryPattern.com &#187; java snippets</title>
	<atom:link href="http://www.factorypattern.com/category/java-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.factorypattern.com</link>
	<description>Just another Object Oriented Weblog</description>
	<lastBuildDate>Sat, 02 Jan 2010 22:08:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Storing parameters in web.xml: context-param &amp; init-param</title>
		<link>http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/</link>
		<comments>http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 22:48:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[java snippets]]></category>
		<category><![CDATA[java web apps]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/</guid>
		<description><![CDATA[There are two options to store parameters in web.xml:
- context parameters &#8211; available to the entire scope of the web application
- init parameters &#8211; available in the context of a servlet or filter in the web application
Context Parameters

&#60;?xml version="1.0" encoding="UTF-8"?&#62;
&#60;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&#62;
  &#60;context-param&#62;
    &#60;description&#62;This is a context [...]]]></description>
			<content:encoded><![CDATA[<p>There are two options to store parameters in web.xml:<br />
- <strong>context parameters</strong> &#8211; available to the entire scope of the web application<br />
- <strong>init parameters</strong> &#8211; available in the context of a servlet or filter in the web application</p>
<h2>Context Parameters</h2>
<p><span id="more-30"></span></p>
<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  &lt;context-param&gt;
    &lt;description&gt;This is a context parameter example&lt;/description&gt;
    &lt;param-name&gt;ContextParam&lt;/param-name&gt;
    &lt;param-value&gt;ContextParam value&lt;/param-value&gt;
  &lt;/context-param&gt;
...
&lt;web-app&gt;</code></pre>
<p>The following code can be be invoked from a servlet or a filter to retrieve the ContextParam value. The parameter can be read successfully from any servlet or filter class.</p>
<pre><code>@Override
	public void init(ServletConfig config) throws ServletException {
		String contextParam = config.getServletContext().getInitParameter("ContextParam");
	}

//or
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String contextParam = this.getServletContext().getInitParameter("ContextParam");
		...
	}
	...
}

String value = this.getServletContext().getInitParameter("ContextParam");</code></pre>
<h2>Init Parameters</h2>
<pre><code>...
&lt;servlet&gt;
    &lt;servlet-name&gt;A Servlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.controller.TestServlet&lt;/servlet-class&gt;
    &lt;init-param&gt; 
        &lt;description&gt;This is an init parameter example&lt;/description&gt; 
        &lt;param-name&gt;InitParam&lt;/param-name&gt; 
        &lt;param-value&gt;init param value&lt;/param-value&gt; 
    &lt;/init-param&gt; 
&lt;/servlet&gt;
...</code></pre>
<p>The following code can be be invoked to retrieve the value of InitParam. The parameter can be accessed only from com.controller.TestServlet.</p>
<pre><code>public class DefaultController extends HttpServlet
{
	@Override
	public void init(ServletConfig config) throws ServletException {
		String initParam  = config.getInitParameter("InitParam");
	}

//or
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String initParam = getServletConfig().getInitParameter("InitParam");
		...
	}
	...
}</code></pre>
<h2>Iterating through context-params and init-params</h2>
<p>It&#8217;s possible to iterate through the paramters if required:</p>
<pre><code>public class DefaultController extends HttpServlet
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Enumeration contextParams = getServletContext().getInitParameterNames();

		Sytem.out.println("context-params: ");
		while (params.hasMoreElements()) {
			String name = (String) params.nextElement();
			Sytem.out.println(name + " = " + config.getInitParameter(name));
		}

		Enumeration initParams = getServletConfig().getInitParameterNames();

		Sytem.out.println("init-params: ");
		while (params.hasMoreElements()) {
			String name = (String) params.nextElement();
			Sytem.out.println(name + " = " + config.getInitParameter(name));
		}

		...
	}
	...
}</code></pre>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Read/Write Java Properties Files</title>
		<link>http://www.factorypattern.com/how-to-readwrite-java-properties-files/</link>
		<comments>http://www.factorypattern.com/how-to-readwrite-java-properties-files/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 16:18:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java snippets]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java properties]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/how-to-readwrite-java-properties-files/</guid>
		<description><![CDATA[There are several options to handle the Java properties files:
To read java properties files from the classpath:
The properties files can be stored in the classpath. This way they can be put inside jar files and it&#8217;s really useful for web applications when the absolute location of the properties files is not known. When I tested [...]]]></description>
			<content:encoded><![CDATA[<p>There are several options to handle the Java properties files:</p>
<h2>To read java properties files from the classpath:</h2>
<p>The properties files can be stored in the classpath. This way they can be put inside jar files and it&#8217;s really useful for web applications when the absolute location of the properties files is not known. When I tested this in an web application it didn&#8217;t work:<span id="more-27"></span></p>
<pre><code>Properties properties = new Properties() ;
URL url =  ClassLoader.getSystemResource("test.properties");
properties.load(new FileInputStream(new File(url.getFile())));</code></pre>
<h2>To read java properties files from the classpath in a web application:</h2>
<p>The following example works to load the properties files in a web application. This snippet was tested on Tomcat 5.5. The &#8216;/&#8217; represents the root of the class path. Otherwise the properties file location is considered relatively to &#8220;this&#8221; class (or to MyClass for the second example):</p>
<pre><code>Properties properties = new Properties() ;
properties.load(this.getClass().getResourceAsStream("/seoimproved.properties"));</code></pre>
<p>Similar example to use in a static context:</p>
<pre><code>Properties properties = new Properties() ;
properties.load(MyClass.class.getResourceAsStream("/seoimproved.properties"));</code></pre>
<h2>To read java properties files from a specific location:</h2>
<p>The properties files can be loaded from any location.</p>
<pre><code>Properties properties = new Properties() ;
properties.load(new FileInputStream("C:\\tmp\\test.properties"));</code></pre>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/how-to-readwrite-java-properties-files/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/how-to-readwrite-java-properties-files/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/multimap-in-google-collections-library/" title="Multimap in Google Collections Library">Multimap in Google Collections Library</a></li><li><a href="http://www.factorypattern.com/how-to-use-ant/" title="How To Use Ant">How To Use Ant</a></li><li><a href="http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/" title="How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar">How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar</a></li><li><a href="http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/" title="How to Simulate Multiple Inheritance in Java">How to Simulate Multiple Inheritance in Java</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/how-to-readwrite-java-properties-files/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.656 seconds -->
