ServletConfig是一個由Tomcat服務器在初始化Servlet的時候創建并傳遞進來的一個對象。
該對象主要描述的時候一個servlet的配置信息。
如:
<servlet> ? 配置一個servlet <servlet-name>helloservlet</servlet-name> ? 指定servlet的名字(任意) ? 指定servlet具體的類的全限定名(包名.類名) <servlet-class>cn.itcast.servlets.HelloServlet</servlet-class></servlet><servlet-mapping> ? 映射一個servlet <servlet-name>helloservlet</servlet-name> ? 需要映射的servlet名 <url-pattern>/helloservlet</url-pattern> ? 瀏覽器需要訪問的路徑</servlet-mapping>
以上配置就是一個servlet的配置信息,如果開發者需要獲取其中的數據,那么tomcat已經創建好的ServletConfig接口對象來方面開發者獲取信息。
舉例1:獲取指定servlet的配置的名字。
public class Demo1 extends HttpServlet { PRivate ServletConfig config = null; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 獲取servlet的配置名 String name = this.config.getServletName(); response.getOutputStream().write(name.getBytes()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } // 重寫init方法 @Override public void init(ServletConfig config) throws ServletException { super.init(config); this.config = config; }}舉例2:獲取servlet的配置參數。
在實際的編程過程中需要將所有可變的數據封裝到配置文件中去,這樣便于軟件的后期的維護。Android。
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
// 獲取servlet的配置名
String name = this.config.getServletName();
response.getOutputStream().write(name.getBytes());
// 獲取配置參數
String ip = this.config.getInitParameter("ip");
String dbtype = this.config.getInitParameter("dbtype");
response.getOutputStream().write("<br/>".getBytes());
response.getOutputStream().write((ip+" , "+dbtype).getBytes());
// 使用循環獲取所有的配置參數
Enumeration<String> names = this.config.getInitParameterNames();
while(names.hasMoreElements()){
String ini_name = names.nextElement();
String ini_value = this.config.getInitParameter(ini_name);
response.getOutputStream().write((ini_name+"="+ini_value+"<br/>").getBytes());
}
}
總結:發現tomcat創建并傳遞過來的servletconfig對象接收比較繁瑣。
// 獲取servletconfig對象ServletConfig config = this.getServletConfig();System.out.println(this.config == config);
為了方便開發者獲取servletconfig對象,那么HttpServlet直接提供了getServletConfig(),
兩種方式獲取的對象是同一個對象。
ServletContext接口ServletContext接口對象主要實現的是servlet之間的通信。該對象可以和web服務器進行通信。轉發請求。
ServletContext接口是對網站的抽象描述。一個ServletContext代表一個web應用(網站)。
該對象是由Tomcat創建出來連同ServletConfig對象一并通過初始化方法傳遞過來的。
1實現servlet的通信
1. 放數據
// 獲取ServletContext對象 public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { ServletContext context1 = this.getServletConfig().getServletContext(); ServletContext context2 = this.getServletContext(); System.out.println(context1 == context2); // true // 主要作用之一是實現servler之間的數據通信 context1.setAttribute("news_title", "北京的灰霾天氣"); }2. 取數據
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { String name = (String) this.getServletContext().getAttribute("news_title"); response.getOutputStream().write(name.getBytes()); }2 實現服務器通信
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 和tomcat進行通信獲取MIME類型 ServletContext context = this.getServletContext(); String type = context.getMimeType("/jnb.doc"); System.out.println(type); }1 獲取網站資源(重點)
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 獲取網站對象 ServletContext context = this.getServletContext(); // 獲取網站中的靜態資源 InputStream in = context.getResourceAsStream("/123.bmp"); // 獲取輸出流 FileOutputStream out = new FileOutputStream(new File("d:/123.bmp")); // 邊讀 邊寫 byte[] bs = new byte[1024]; int len = 0; while((len = in.read(bs)) != -1){ out.write(bs, 0, len); out.flush(); } // 釋放資源 in.close(); out.close(); }3請求轉發
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 獲取網站對象 ServletContext context = this.getServletContext(); // 轉發 System.out.println("不好意思沒錢!,找老湯去...."); context.getRequestDispatcher("/lt").forward(request, response); }請求轉發:請求轉發是服務器自己做的事情,那么在客戶端是展現不出來的,因此瀏覽器的地址欄是不發生變化的。
新聞熱點
疑難解答