<?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>My IT-blog &#187; jsp</title>
	<atom:link href="http://dev.eek.be/tag/jsp/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.eek.be</link>
	<description>IT knowledge exchange</description>
	<lastBuildDate>Sun, 18 Dec 2011 10:37:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>servletconfig vs servletcontext</title>
		<link>http://dev.eek.be/2009/10/servletconfig-vs-servletcontext/</link>
		<comments>http://dev.eek.be/2009/10/servletconfig-vs-servletcontext/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 20:15:58 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[server management]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://dev.eek.be/?p=80</guid>
		<description><![CDATA[In this little blogpost, I&#8217;ll try to explain the differences between ServletConfig and ServletContext and when you can use them into a Java Web Application. ServletConfig ServletContext Access deploy-time servlet parameters Access web application parameters One per servlet One per web application ServletConfig ServletConfig is actually very simple. Like stated above, you can retrieve parameters ]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.eek.be/wp-content/uploads/2009/10/java2.jpg" alt="java2" title="java2" width="300" height="225" class="aligncenter size-full wp-image-151" /></p>
<p>In this little blogpost, I&#8217;ll try to explain the differences between ServletConfig and ServletContext and when you can use them into a Java Web Application.</p>
<table cellpadding="2" cellspacing="0" border="1">
<tr>
<td><b>ServletConfig</b></td>
<td><b>ServletContext</b></td>
</tr>
<tr>
<td>Access deploy-time servlet parameters</td>
<td>Access web application parameters</td>
</tr>
<tr>
<td>One per servlet</td>
<td>One per web application</td>
</tr>
</table>
<p><b>ServletConfig</b></p>
<p>ServletConfig is actually very simple. Like stated above, you can retrieve parameters for the servlet which are set at deployment time.<br />
I&#8217;ll explain this with a very simple example.
</p>
<p>
If you have created a servlet with the name &#8220;MyServlet&#8221; you can add the following 2 variables into the servlet:</p>
<pre class="prettyprint" lang-xml>
    &lt;servlet&gt;
         &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
         &lt;servlet-class&gt;servlets.MyServlet&lt;/servlet-class&gt;
         &lt;init-param&gt;
             &lt;param-name&gt;variable1&lt;/param-name&gt;
             &lt;param-value&gt;I'm var1&lt;/param-value&gt;
         &lt;/init-param&gt;
         &lt;init-param&gt;
             &lt;param-name&gt;variable2&lt;/param-name&gt;
             &lt;param-value&gt;I'm var2&lt;/param-value&gt;
         &lt;/init-param&gt;
     &lt;/servlet&gt;
     &lt;servlet-mapping&gt;
         &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
         &lt;url-pattern&gt;/MyServlet&lt;/url-pattern&gt;
     &lt;/servlet-mapping&gt;
</pre>
</p>
<p>
You can retrieve variable2 with the following code:</p>
<pre class="prettyprint" lang-java>
getServletConfig().getInitParameter("variable2");
</pre>
</p>
<p>
Retrieve all the variables of the servlet and loop over them:</p>
<pre class="prettyprint" lang-java>
Enumeration allvars = getServletConfig().getInitParameterNames();
while (allvars.hasMoreElements()) {
     String next = (String) allvars.nextElement();
     out.println(next + " has the value " + getServletConfig().getInitParameter(next));
}
</pre>
</p>
<p>
It&#8217;s also possible to retrieve the servlets name through getServletconfig</p>
<pre class="prettyprint" lang-java>
out.println("The name of the servlet is: " + getServletConfig().getServletName());
</pre>
</p>
<p><b>ServletContext</b></p>
<p>
ServletContext can be used to retrieve application-wide parameters, get serverparameters and store/retrieve/remove application-wide attributes.
</p>
<p>It is possible to call the ServletContext right on or through the ServletConfig. Both are the same so you can choose between them.</p>
<pre class="prettyprint" lang-java>
if (getServletContext().equals(getServletConfig().getServletContext())) {
       out.println("both calls retrieve the same Context");
}
</pre>
</p>
<p>
Just like with ServletConfig, you can set parameters in the Deployment Descriptor. They are initialized on compile time. It&#8217;s not possible to remove or add them on runtime.</p>
<pre class="prettyprint" lang-xml>
&lt;context-param&gt;
        &lt;param-name&gt;variable3&lt;/param-name&gt;
        &lt;param-value&gt;Im var 3&lt;/param-value&gt;
&lt;/context-param&gt;
</pre>
<p>And call them in your servlet just like the ServletConfig parameters:</p>
<pre class="prettyprint" lang-java>
getServletContext().getInitParameter("variable3")
</pre>
</p>
<p>
Unlike ServletConfig, ServletContext can work with attributes. These are parameters or objects which you can set into the servlet and retrieve in every other servlet of your Web Application:</p>
<pre class="prettyprint" lang-java>
getServletContext().setAttribute("test", "I'm an application-wide String");
..
out.println(getServletContext().getAttribute("test"));
..
getServletContext().removeAttribute("test");
</pre>
</p>
<p>
I&#8217;ll end this little short tutorial with an example of how you can retrieve serversettings through the Context:</p>
<pre class="prettyprint" lang-java>
out.println(getServletContext().getContextPath());
out.println(getServletContext().getMajorVersion());
out.println(getServletContext().getRealPath(""));
out.println(getServletContext().getServerInfo());
</pre>
</p>
<p>If there are any more questions, just shoot them <img src='http://dev.eek.be/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://dev.eek.be/2009/10/servletconfig-vs-servletcontext/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

