サーブレットのサーブレットContextまとめ


前言
前のセッションの学習では,セッションを用いて異なるページのデータ共有を実現できることを知っていたが,注意深く
考えてみれば、この共有は局所的であることがわかる.Webカウンタのような機能を完了する必要がある場合は、
Sessionは要求に達しない.この機能を実現するには、サーブレットContextを使用します.
            ServletContext
サーブレットContextは、すべてのクライアントによってアクセスできるサーバの共有スペースとして理解できます.けつごう
Cookie、Sessionの分布は以下の通りである.
                             
ここで,クライアントのA,B,CはCookie,サーバセグメントのA,B,CはSession,サーバセグメントのDを表す.
サーブレットContextを表します.
Tomcatコンテナは起動時に各Webアプリケーションに対応するサーブレットContextオブジェクトを作成します.
このようなウェブアプリケーションのすべてのサーブレットは、1つのサーブレットContextオブジェクトを共有するので、サーブレットオブジェクト間で
サーブレットContextオブジェクトによる通信を実現します.
また、サーブレットContextのライフサイクルは、作成開始からサーバのシャットダウンまで終了します.
サーブレットContextの使用方法
サーブレットContextオブジェクトの取得方法については、一般的に次の方法があります.
1、サーブレットコンフィグオブジェクトによる取得
ServletContext context= config.getServletContext();

2、HttpServeretで直接取得する
this.getServletContext() 

3、HttpRequestで取得
request.getSession().getServletContext(); 
サーブレットContextとSessionは同様に、キー値ペアとしてデータを格納するための一般的な方法である
次のようになります.
属性の追加:setAttribute(String name,Object obj);取得値:getAttribute(String name);属性の削除:removeAttribute(String name);
サーブレットContextインスタンス学習
ここでは、サーブレットContextの簡単な使用を見るために、Webカウンタを簡単に実装します.
                                
package com.kiritor;  import java.io.IOException;  import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  /**  * Servlet implementation class CounterSerlvet  */ @WebServlet("/CounterSerlvet") public class CounterSerlvet extends HttpServlet { 	private static final long serialVersionUID = 1L;             /**      * @see HttpServlet#HttpServlet()      */     public CounterSerlvet() {         super();         // TODO Auto-generated constructor stub     }  	/** 	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 	 */ 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 		this.doPost(request, response); 	}  	/** 	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 	 */ 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 		ServletContext context=request.getSession().getServletContext(); 		  Integer counter=(Integer)context.getAttribute("counter"); 		  if(null == counter){ 		   context.setAttribute("counter", 1); 		  }else{ 		   context.setAttribute("counter", counter + 1); 		  } 		  request.getRequestDispatcher("counter.jsp").forward(request, response); 		 } 	  } 
                       
                       :       </code></pre>                                : 
   
  <p>                  </p> 
  <p><strong>                                                                                                                                                     By      Kiritor</strong></p> 
  <p><strong>                                                                                                                                                     2012/06/13</strong></p> 
  <p><br/> </p> 
  <p><br/> </p> 
  <p><br/> </p> 
 </div> 
</div>
                            </div>
                        </div>