servletContext對象是Servlet三大域對象之一,每一個Web應用程序都擁有一個ServletContext對象,該對象是Web應用程序的全局對象或者上下文。Tomcat服務器在啓動時,會自動建立一個ServletContext對象,在關閉時,會自動銷燬這個ServletContext對象。每一個Web應用程序只擁有一個ServletContext對象,ServletContext對象能夠在整個Web應用中共享數據資源。html
1 import java.io.IOException; 2 3 import javax.servlet.ServletConfig; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 //演示獲取servletContext對象 10 /** 11 * 有三種方式獲取servletContext對象 12 * 1. 採用servletConfig對象獲取 13 * 2. 採用servlet實例對象獲取 14 * 3. 採用request對象獲取 15 * @author Administrator 16 * 17 */ 18 public class ServletContext1 extends HttpServlet { 19 20 ServletContext sc ; 21 22 @Override 23 public void init(ServletConfig config) throws ServletException { 24 super.init(config) ; 25 sc = config.getServletContext() ;//1. 26 } 27 28 public void doGet(HttpServletRequest request, HttpServletResponse response) 29 throws ServletException, IOException { 30 //第二種方式 31 ServletContext sc1 = this.getServletContext() ; 32 System.out.println(sc); 33 System.out.println(sc1 == sc); 34 //第三種方式 35 ServletContext sc2 = request.getSession().getServletContext() ; 36 System.out.println(sc2 == sc); 37 } 38 39 public void doPost(HttpServletRequest request, HttpServletResponse response) 40 throws ServletException, IOException { 41 doGet(request, response); 42 } 43 }
1) 實現數據共享java
2) 獲取全局配置參數web
3) 請求轉發apache
a) 拿取請求轉發器,而後轉發瀏覽器
4) 獲取資源文件服務器
5) 用來得到文件的MIME的類型.app
下面我將經過例子說明:登陸成功後,5秒後跳轉到某個頁面,在頁面中顯示您是第x次登陸成功
框架
1 public class VisitServlet extends HttpServlet { 2 @Override 3 public void init() throws ServletException { 4 ServletContext context = getServletContext(); 5 context.setAttribute("times", 0); 6 } 7 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 8 ServletContext context = getServletContext(); 9 10 int times = (Integer)context.getAttribute("times"); 11 times ++; 12 context.setAttribute("times", times);
response.setHeader("Refresh", "5;url=/test/showTimeServlet");ide
13 } 14 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 doGet(request, response); 16 } 17 }
1 public class ShowTimeServlet extends HttpServlet { 2 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 ServletContext context = getServletContext(); 4 //獲取訪問的次數 5 int times = (Integer)context.getAttribute("times"); 6 7 response.setContentType("text/html;charset=utf-8"); 8 PrintWriter out = response.getWriter(); 9 out.println("<h1>VisitServlet共被訪問了"+times+"次</h1>"); 10 } 11 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 12 doGet(request, response); 13 } 14 }
得到Servlet的web.xml配置信息
1 <context-param> 2 <param-name>username</param-name> 3 <param-value>蒼老師</param-value> 4 </context-param> 5 <context-param> 6 <param-name>password</param-name> 7 <param-value>123</param-value> 8 </context-param>
獲取web.xml配置信息中this
import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 獲取web應用的初始化信息 */ public class ServletContextDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到ServletContext域對象 ServletContext context = this.getServletContext(); //得到單個數據(username) String name = context.getInitParameter("username") ; // System.out.println(name); //得到全部數據 Enumeration enumeration = context.getInitParameterNames(); while(enumeration.hasMoreElements()){ String name = (String) enumeration.nextElement(); String value = context.getInitParameter(name); System.out.println(name+":"+value); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
在javax.servlet包中,定義了ServletConfig接口。Servlet容器使用ServletConfig對象在Servlet初始化時向其傳遞配置信息。
所謂的Serlvet配置信息,就是在Web應用程序中web.xml文件中配置有關Servlet的內容。
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>app.java.servlet.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/servlet/ServletConfigTest</url-pattern> </servlet-mapping> <init-param> <param-name>name</param-name> <param-value>yl</param-value> </init-param> <init-param> <param-name>blog</param-name> <param-value>http://www.longestory.com</param-value> </init-param>
public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException { ServletConfig servletConfig = this.getServletConfig(); String servletName = servletConfig.getServletName(); System.out.println(servletName); }
運行Web應用程序,在控制檯中打印「ConfigServletTest」。
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { ServletConfig servletConfig = getServletConfig(); String name = servletConfig.getInitParameter("name"); String blog = servletConfig.getInitParameter("blog"); System.out.println(name + "'s blog is " + blog); }
運行Web應用程序,在控制檯中打印「yl's blog is http://www.longestory.com」。
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { ServletConfig servletConfig = getServletConfig(); Enumeration enumeration = servletConfig.getInitParameterNames(); while (enumeration.hasMoreElements()) { String element = (String) enumeration.nextElement(); String value = servletConfig.getInitParameter(element); System.out.println(element + ": " + value); } }
<!-- Standard Action Servlet Configuration (with debugging) --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml, /WEB-INF/struts-config-Wildcard.xml </param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
2017-05-12