在Servlet的配置文件web.xml中,可使用一個或多個<init-param>標籤爲servlet配置一些初始化參數。java
例如:mysql
1 <servlet> 2 <servlet-name>ServletConfigDemo1</servlet-name> 3 <servlet-class>gacl.servlet.study.ServletConfigDemo1</servlet-class> 4 <!--配置ServletConfigDemo1的初始化參數 --> 5 <init-param> 6 <param-name>name</param-name> 7 <param-value>admin</param-value> 8 </init-param> 9 <init-param> 10 <param-name>password</param-name> 11 <param-value>123456</param-value> 12 </init-param> 13 <init-param> 14 <param-name>charset</param-name> 15 <param-value>UTF-8</param-value> 16 </init-param> 17 </servlet>
當servlet配置了初始化參數後,web容器在建立servlet實例對象時,會自動將這些初始化參數封裝到ServletConfig對象中,並在調用servlet的init方法時,將ServletConfig對象傳遞給servlet。進而,咱們經過ServletConfig對象就能夠獲得當前servlet的初始化參數信息。程序員
例如:web
1 package gacl.servlet.study; 2 3 import java.io.IOException; 4 import java.util.Enumeration; 5 import javax.servlet.ServletConfig; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class ServletConfigDemo1 extends HttpServlet { 12 13 /** 14 * 定義ServletConfig對象來接收配置的初始化參數 15 */ 16 private ServletConfig config; 17 18 /** 19 * 當servlet配置了初始化參數後,web容器在建立servlet實例對象時, 20 * 會自動將這些初始化參數封裝到ServletConfig對象中,並在調用servlet的init方法時, 21 * 將ServletConfig對象傳遞給servlet。進而,程序員經過ServletConfig對象就能夠 22 * 獲得當前servlet的初始化參數信息。 23 */ 24 @Override 25 public void init(ServletConfig config) throws ServletException { 26 this.config = config; 27 } 28 29 public void doGet(HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException { 31 //獲取在web.xml中配置的初始化參數 32 String paramVal = this.config.getInitParameter("name");//獲取指定的初始化參數 String pwd = config.getInitParameter("password"); 33 response.getWriter().print(paramVal +": " + pwd); 36 //獲取全部的初始化參數 37 Enumeration<String> e = config.getInitParameterNames(); 38 while(e.hasMoreElements()){ 39 String name = e.nextElement(); 40 String value = config.getInitParameter(name); 41 response.getWriter().print(name + "=" + value + "<br/>"); 42 } 43 } 44 45 public void doPost(HttpServletRequest request, HttpServletResponse response) 46 throws ServletException, IOException { 47 this.doGet(request, response); 48 } 49 50 }
運行結果以下:sql
WEB容器在啓動時,它會爲每一個WEB應用程序都建立一個對應的ServletContext對象,它表明當前web應用。
ServletConfig對象中維護了ServletContext對象的引用,開發人員在編寫servlet時,能夠經過ServletConfig.getServletContext方法得到ServletContext對象。
因爲一個WEB應用中的全部Servlet共享同一個ServletContext對象,所以Servlet對象之間能夠經過ServletContext對象來實現通信。ServletContext對象一般也被稱之爲context域對象。瀏覽器
範例:ServletContextDemo1和ServletContextDemo2經過ServletContext對象實現數據共享app
import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletContextDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "ABC"; /** * ServletConfig對象中維護了ServletContext對象的引用,開發人員在編寫servlet時, * 能夠經過ServletConfig.getServletContext方法得到ServletContext對象。 */ ServletContext context = this.getServletConfig().getServletContext();//得到ServletContext對象 context.setAttribute("data", data); //將data存儲到ServletContext對象中 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletContextDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); String data = (String) context.getAttribute("data");//從ServletContext對象中取出數據 response.getWriter().print(data); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
先運行ServletContextDemo1,將數據data存儲到ServletContext對象中,而後運行ServletContextDemo2就能夠從ServletContext對象中取出數據了,這樣就實現了數據共享,以下圖所示ide
:this
在web.xml文件中使用<context-param>標籤配置WEB應用的初始化參數,以下所示:url
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 3 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 4 <display-name></display-name> 5 <!-- 配置WEB應用的初始化參數 --> 6 <context-param> 7 <param-name>url</param-name> 8 <param-value>jdbc:mysql://localhost:3306/test</param-value> 9 </context-param> 10 </web-app>
獲取Web應用的初始化參數,代碼以下:
3 import java.io.IOException; 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 10 11 public class ServletContextDemo3 extends HttpServlet { 12 13 public void doGet(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 16 ServletContext context = this.getServletContext(); 17 //獲取整個web站點的初始化參數 18 String contextInitParam = context.getInitParameter("url"); 19 response.getWriter().print(contextInitParam); 20 } 21 22 public void doPost(HttpServletRequest request, HttpServletResponse response) 23 throws ServletException, IOException { 24 doGet(request, response); 25 } 26 27 }
運行結果:
ServletContextDemo4 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import javax.servlet.RequestDispatcher; 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class ServletContextDemo4 extends HttpServlet { 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) 15 throws ServletException, IOException { 16 String data = "<h1><font color='red'>abcdefghjkl</font></h1>"; 17 response.getOutputStream().write(data.getBytes()); 18 ServletContext context = this.getServletContext();//獲取ServletContext對象 19 RequestDispatcher rd = context.getRequestDispatcher("/servlet/ServletContextDemo5");//獲取請求轉發對象(RequestDispatcher) 20 rd.forward(request, response);//調用forward方法實現請求轉發 21 } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 } 26 }
ServletContextDemo5 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class ServletContextDemo5 extends HttpServlet { 10 11 public void doGet(HttpServletRequest request, HttpServletResponse response) 12 throws ServletException, IOException { 13 response.getOutputStream().write("servletDemo5".getBytes()); 14 } 15 16 public void doPost(HttpServletRequest request, HttpServletResponse response) 17 throws ServletException, IOException { 18 this.doGet(request, response); 19 } 20 21 }
運行結果:
訪問的是ServletContextDemo4,瀏覽器顯示的倒是ServletContextDemo5的內容,這就是使用ServletContext實現了請求轉發。