Servlet之ServletContext總結

            前言

                         在前面Session的學習中,咱們知道的是使用Session能夠實現不一樣頁面的數據共享,不過仔細html

                     思考能夠知道的是,這種共享是局部性的。當咱們須要完成網頁計數器相似的功能時,使用java

                     Session就不能達到要求了。實現這種功能咱們能夠藉助ServletContext來完成。web

            ServletContext

                          ServletContext能夠理解爲服務器的一個共享空間,它能夠被全部的客戶端訪問。結合服務器

                   Cookie、Session其分佈狀況以下:app

                             

                           其中客戶端的A、B、C表明Cookie,服務器段的A、B、C表明Session,服務器段的D
jsp

                       則表明了ServletContext。ide

                           Tomcat容器在啓動時,它會爲每一個web應用程序都建立一個對應的ServletContext對象,學習

                        這樣一個web應用中的全部Servlet共享一個ServletContext對象,所以Servlet對象之間能夠ui

                        經過ServletContext對象來實現通信。this

                           並且ServletContext的生命週期是從建立開始到服務器關閉的時候結束的。

               如何使用ServletContext

                                 對於ServletContext對象如何獲取,通常狀況下能夠有如下幾種方式:

                   一、經過ServletConfig對象獲取      

ServletContext context= config.getServletContext();

                   二、在HttpServlet中直接獲取

this.getServletContext() 

                   三、經過HttpRequest獲取            

request.getSession().getServletContext(); 
                              ServletContext和Session相似也是以鍵值對的形式存放數據的,具體經常使用方法

                          以下:

                               添加屬性:setAttribute(String name,Object obj);
                               獲得值:getAttribute(String name);
                               刪除屬性:removeAttribute(String name);

                ServletContext實例學習

                                 這裏簡單的實現一個網頁計數器來看看ServletContext的簡單使用。

                                

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); 		 } 	  } 
                       
<%@ page language="java" contentType="text/html; charset=utf-8"     pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title></title> </head>     <body>         計數器: <%=application.getAttribute("counter")%>     </body> </html>
                            運行結果:

                 

                                                                                                                                                     By      Kiritor

                                                                                                                                                     2012/06/13

相關文章
相關標籤/搜索