<?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>Li Guoliang &#187; context-param</title>
	<atom:link href="http://liguoliang.com/tag/context-param/feed/" rel="self" type="application/rss+xml" />
	<link>http://liguoliang.com</link>
	<description>ActionScript Flex Java JEE PHP...</description>
	<lastBuildDate>Mon, 21 May 2012 17:04:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>web.xml中&lt;context-param&gt;与&lt;init-param&gt;的差别及各自的读取方式</title>
		<link>http://liguoliang.com/2009/webxml%e4%b8%adcontext-param%e4%b8%8einit-param%e7%9a%84%e5%b7%ae%e5%88%ab%e5%8f%8a%e5%90%84%e8%87%aa%e7%9a%84%e8%af%bb%e5%8f%96%e6%96%b9%e5%bc%8f/</link>
		<comments>http://liguoliang.com/2009/webxml%e4%b8%adcontext-param%e4%b8%8einit-param%e7%9a%84%e5%b7%ae%e5%88%ab%e5%8f%8a%e5%90%84%e8%87%aa%e7%9a%84%e8%af%bb%e5%8f%96%e6%96%b9%e5%bc%8f/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 11:40:37 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[context-param]]></category>
		<category><![CDATA[init-param]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[ServletContext]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2009/02/748/</guid>
		<description><![CDATA[使用ServletContext.getInitParameter()方法 可以获得web.xml中的<context -param>参数 - 使用范围很广, 能getServletContext就可以使用.

在Servlet的init()函数中使用this.getInitParameter()获得web.xml的<init -param>参数 - 较为局限的参数. 注意逻辑上的层次划分</init></context><p class='read-more'><a href='http://liguoliang.com/2009/webxml%e4%b8%adcontext-param%e4%b8%8einit-param%e7%9a%84%e5%b7%ae%e5%88%ab%e5%8f%8a%e5%90%84%e8%87%aa%e7%9a%84%e8%af%bb%e5%8f%96%e6%96%b9%e5%bc%8f/'></a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>事由: 最近几天看了点Servlet, 打算写一个小程序, 需要在application启动时从web.xml中加载一个参数, 由于仅在Login这个Servlet中使用, 于是我是这样实现的:    </p>
<pre class="xml" name="code">  <servlet>
    <description></description>
    <display-name>Login</display-name>
    <servlet-name>Login</servlet-name>
    <servlet-class>com.insprise.servletstu.Login</servlet-class>
    <init-param>
      <description>maxNumber</description>
<param-name>maxNumber</param-name>
<param-value>3</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
  </servlet-mapping>
  <servlet></pre>
<p>然后在Login中重写init()方法, 如下:</p>
<pre class="java" name="code">Calc.MAX_CALCULATION_TIMES_PER_SESSION = Integer.parseInt(this.getInitParameter(Calc.MAX_CALCULATIONAS_PER_SESSION));</pre>
<p>功能以实现, 将最大值从web.xml中读取出来, 赋值给一个Global的变量.</p>
<p>===============================承上启下的分割线==============================================</p>
<p>但随着学习的深入, 发现容器并不一定会保持Servlet一直存活, 也许会在Request介绍后Destory掉Login Servlet, 然后如果再次Request Login, 则会重新init(), 于是重新读取web.xml, 重新赋值&#8211;明显第一次有效的, 后面的都是无用功.</p>
<p>从逻辑上分析, 该最大值应属于ServletContext级别的数据, 于是应使用Listener, 监听Application初始化, 在Application初始化时读取.</p>
<p>于是我没有改动web.xml, 设置了监听函数:</p>
<pre class="java" name="code">    public void contextInitialized(ServletContextEvent sce) {
        log.info(&quot;Context Started!&quot; + sce.getServletContext());
        ServletContext context = sce.getServletContext();
        Calc.MAX_CALCULATION_TIMES_PER_SESSION = Integer.parseInt(context.getInitParameter(Calc.MAX_CALCULATIONAS_PER_SESSION));
    }</pre>
<p>但不论如何都会有NullPoint的错误. 于是意识到该应在web.xml使用&lt;context-param&gt;设定参数, 而非某个&lt;Servlet&gt;下的&lt;init-param&gt;标签. 修改后的web.xml: </p>
<pre class="xml" name="code"> <context-param>
<param-name>maxNumber</param-name>
<param-value>3</param-value>
</context-param></pre>
<p>=====================================总结性的分割线============================================ </p>
<p>总而言之:<br />
  <br />使用ServletContext.getInitParameter()方法 可以获得web.xml中的&lt;context-param&gt;参数 &#8211; 使用范围很广, 能getServletContext就可以使用.</p>
<p>在Servlet的init()函数中使用this.getInitParameter()获得web.xml的&lt;init-param&gt;参数 &#8211; 较为局限的参数. 注意逻辑上的层次划分</p>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2009/webxml%e4%b8%adcontext-param%e4%b8%8einit-param%e7%9a%84%e5%b7%ae%e5%88%ab%e5%8f%8a%e5%90%84%e8%87%aa%e7%9a%84%e8%af%bb%e5%8f%96%e6%96%b9%e5%bc%8f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

