Skip to content

Commit 17b8698

Browse files
committed
Site updated: 2018-06-05 00:12:32
1 parent 504bfd8 commit 17b8698

File tree

6 files changed

+143
-4253
lines changed

6 files changed

+143
-4253
lines changed

2017/04/04/JavaWeb-08-ServletContext-HttpServletResponse/index.html

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<meta property="og:site_name" content="geekhoon">
9494
<meta property="og:description" content="ServletContext概述ServletContext是存取数据的域(空间/容器),是一个域对象;ServletContext是Web应用的上下文对象,向下可以管理当前Web应用的资源(包括Servlet动态资源及Html等静态资源),向上可以与服务器进行交互;ServletContext被不同的Servlet共享.">
9595
<meta property="og:image" content="http://oj3s07yy0.bkt.clouddn.com/blog/20170409/010006246.png">
96-
<meta property="og:updated_time" content="2018-05-30T17:31:51.293Z">
96+
<meta property="og:updated_time" content="2018-06-04T16:12:18.418Z">
9797
<meta name="twitter:card" content="summary">
9898
<meta name="twitter:title" content="ServletContext&HttpServletResponse">
9999
<meta name="twitter:description" content="ServletContext概述ServletContext是存取数据的域(空间/容器),是一个域对象;ServletContext是Web应用的上下文对象,向下可以管理当前Web应用的资源(包括Servlet动态资源及Html等静态资源),向上可以与服务器进行交互;ServletContext被不同的Servlet共享.">
@@ -379,13 +379,9 @@ <h1 class="post-title" itemprop="name headline">
379379

380380

381381
<h2 id="ServletContext"><a href="#ServletContext" class="headerlink" title="ServletContext"></a>ServletContext</h2><h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><p>ServletContext是存取数据的域(空间/容器),是一个域对象;<br>ServletContext是Web应用的上下文对象,向下可以管理当前Web应用的资源(包括Servlet动态资源及Html等静态资源),向上可以与服务器进行交互;<br>ServletContext被不同的Servlet共享.<br><a id="more"></a> </p>
382-
<h3 id="ServletContext的作用"><a href="#ServletContext的作用" class="headerlink" title="ServletContext的作用"></a>ServletContext的作用</h3><ol>
383-
<li>用来获得全局初始化参数.</li>
384-
<li>用来获得文件的MIME的类型.</li>
385-
<li>作为域对象存取数据.</li>
386-
<li>用来读取web项目下的文件<h3 id="作用范围"><a href="#作用范围" class="headerlink" title="作用范围"></a>作用范围</h3><strong>整个web工程</strong><br>创建:服务器启动的时候,tomcat服务器为每个web项目创建一个单独ServletContext对象<br>销毁:服务器关闭的时候,或者项目从服务器中移除的时候<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><strong>1.获取ServletContext对象</strong><br>Tomcat创建的该对象,我们只要在需要的Servlet下(因为是共享,所以在任何Servlet下都可以获取)使用如下语句便可获取:<br> ServletContext context = this.getServletContext();<br>不同的Servlet获取的ServletContext具有共同的地址值!<br><strong>2.存取数据 — 不同的Servlet之间实现数据共享</strong><br>存储:setAttribute(String 键名,Object obj) – 值是object类型<br>获取:getAttribute(String 键名) – 获取的值是Object类型,如果没有该键,返回null<br>移除:removeAttribute(String name) – 移除指定数据<br><strong>3.获取Web项目资源(文件下载)</strong><br>getResourceAsStream(String path)—将web项目下的资源转成输入流<br>getRealPath(String path)—获取资源路径(绝对路径)<br>注意路径的写法:以”/“开始,代表当前项目在Tomcat webapps下当前项目对应的文件夹<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">获取Web项目资源:</div><div class="line">ServletContext context = this.getServletContext();</div><div class="line">//获取资源对应的输入流</div><div class="line">InputStream in = context.getResourceAsStream("/resource/a.txt");</div><div class="line">//注意路径,第一个"/"代表当前项目下任意目录,即可以代表resource所在的WebContent文件夹</div><div class="line">byte[] buffer = new byte[1024]'</div><div class="line">int length;</div><div class="line">while((length = in.read(buffer)) != -1)&#123;</div><div class="line"> String str = new String(buffer,0,length);</div><div class="line"> System.out.println(str);</div><div class="line">&#125;</div><div class="line">in.close();</div></pre></td></tr></table></figure>
387-
</li>
388-
</ol>
382+
<h3 id="ServletContext的作用"><a href="#ServletContext的作用" class="headerlink" title="ServletContext的作用"></a>ServletContext的作用</h3><p>1 用来获得全局初始化参数.<br>2.用来获得文件的MIME的类型.<br>3.作为域对象存取数据.<br>4.用来读取web项目下的文件</p>
383+
<h3 id="作用范围"><a href="#作用范围" class="headerlink" title="作用范围"></a>作用范围</h3><p><strong>整个web工程</strong><br>创建:服务器启动的时候,tomcat服务器为每个web项目创建一个单独ServletContext对象<br>销毁:服务器关闭的时候,或者项目从服务器中移除的时候</p>
384+
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><p><strong>1.获取ServletContext对象</strong><br>Tomcat创建的该对象,我们只要在需要的Servlet下(因为是共享,所以在任何Servlet下都可以获取)使用如下语句便可获取:<br> ServletContext context = this.getServletContext();<br>不同的Servlet获取的ServletContext具有共同的地址值!<br><strong>2.存取数据 — 不同的Servlet之间实现数据共享</strong><br>存储:setAttribute(String 键名,Object obj) – 值是object类型<br>获取:getAttribute(String 键名) – 获取的值是Object类型,如果没有该键,返回null<br>移除:removeAttribute(String name) – 移除指定数据<br><strong>3.获取Web项目资源(文件下载)</strong><br>getResourceAsStream(String path)—将web项目下的资源转成输入流<br>getRealPath(String path)—获取资源路径(绝对路径)<br>注意路径的写法:以”/“开始,代表当前项目在Tomcat webapps下当前项目对应的文件夹<br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">获取Web项目资源:</div><div class="line">ServletContext context = this.getServletContext();</div><div class="line">//获取资源对应的输入流</div><div class="line">InputStream in = context.getResourceAsStream("/resource/a.txt");</div><div class="line">//注意路径,第一个"/"代表当前项目下任意目录,即可以代表resource所在的WebContent文件夹</div><div class="line">byte[] buffer = new byte[1024]'</div><div class="line">int length;</div><div class="line">while((length = in.read(buffer)) != -1)&#123;</div><div class="line"> String str = new String(buffer,0,length);</div><div class="line"> System.out.println(str);</div><div class="line">&#125;</div><div class="line">in.close();</div></pre></td></tr></table></figure></p>
389385
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">获取资源路径:(获取的是服务器下的绝对路径,不是工作空间下的路径)</div><div class="line">String realPath = context.getRealPath(<span class="string">"/resource/a.txt"</span>);</div><div class="line">System.out.println(realPath);</div></pre></td></tr></table></figure>
390386
<p><strong>4.操作初始化参数</strong><br>在web.xml中进行配置:比如设置编码集,参数名写encode,值写gbk<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">全局:(在Servlet标签同级设置)</div><div class="line">&lt;context-param&gt;</div><div class="line"> &lt;param-name&gt;参数名&lt;/param-name&gt;</div><div class="line"> &lt;param-value&gt;参数值&lt;/param-value&gt;</div><div class="line">&lt;/context-param&gt;</div><div class="line"></div><div class="line">在Servlet中使用:</div><div class="line"> ServletContext对象.getInitParameter(&quot;参数名&quot;); 获取指定全局初始化参数;</div><div class="line"></div><div class="line"></div><div class="line">局部:(在Servlet标签内部的所有标签最后设置)</div><div class="line">&lt;init-param&gt;</div><div class="line"> &lt;param-name&gt;参数名&lt;/param-name&gt; </div><div class="line"> &lt;param-value&gt;参数值&lt;/param-value&gt;</div><div class="line">&lt;/init-param&gt;</div><div class="line"></div><div class="line">在Servlet中使用:</div><div class="line"> ServletConfig对象.getInitParameter(&quot;参数名&quot;); 获取指定局部初始化参数;</div></pre></td></tr></table></figure></p>
391387
<h3 id="获取输入流的方式"><a href="#获取输入流的方式" class="headerlink" title="获取输入流的方式"></a>获取输入流的方式</h3><p>方式1: 通过类的加载器获取src下的文件<br>InputStream in = 类名.class.getClassloader().getResourceAsStream(“文件名即可”);<br>方式2: 通过ServletContext获取webContent文件夹下的文件<br>InputStream in = this.getServletContext().getResourceAsStream(String)<br>区别:<br>1.文件位置:如果是在src下,使用类加载器;如果在webcontent下,使用ServletContext<br>2.如果是配置信息,则存在src下,如jdbc.properties,c3p0配置文件等;<br> 如果是项目的一些资源,如图片,视频,音频,压缩包等等,放在WebContent下<br>3.项目部署时存储位置的区别:<br>WebContent下的资源存储在项目文件夹下;<br>src下的资源存储在WEB-INF的classes文件夹下<br>拓展:<br>使用ServletContext获取src下资源的输入流:<br>InputStream in = this.getServletContext().getResourceAsStream(“WEB-INF/classes/hehe.properties”);<br>使用前提:必须明确该文件在Tomcat中的文件位置</p>

about/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373

7474

7575

76+
77+
7678

7779
<link rel="alternate" href="/atom.xml" title="geekhoon" type="application/atom+xml" />
7880

0 commit comments

Comments
 (0)