標籤:javaweb servlet tomcat
一、ServletConfig類
代表當前Servlet在web.xml中的配置資訊
String getServletName() -- 擷取當前Servlet在web.xml中配置的名字
String getInitParameter(String name) -- 擷取當前Servlet指定名稱的初始化參數的值
Enumeration getInitParameterNames() -- 擷取當前Servlet所有初始化參數的名字組成的枚舉
***(重要)ServletContext getServletContext() -- 擷取代表當前web應用的ServletContext對象
public class SConfigServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//1.擷取ServletConfig對象ServletConfig servletConfig = this.getServletConfig();//2.擷取servlet名字 String getServletName() String servletName = servletConfig.getServletName();System.out.println(servletName);//3.擷取初始化參數值String value1 = getInitParameter("name1");String value2 = getInitParameter("name2");System.out.println(value1);System.out.println(value2);System.out.println("-----------------");//4.擷取當前Servlet所有初始化參數的名字組成的枚舉Enumeration namesEnum = this.getInitParameterNames();while (namesEnum.hasMoreElements()) {String name = (String) namesEnum.nextElement();String value = servletConfig.getInitParameter(name);System.out.println(name+":"+value);}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
二、ServletContext
代表當前web應用
1.做為域對象可以在整個web應用範圍內共用資料
域對象:在一個可以被看見的範圍內共用資料用到對象
作用範圍:整個web應用範圍內共用資料
生命週期:當伺服器啟動web應用載入後建立出ServletContext對象後,域產生。當web應用被移除出容器或伺服器關閉,隨著web應用的銷毀域銷毀。
void setAttribute(String,Object);
Object getAttribute(String);
void removeAttribute(String);
//設定屬性
public class Demo2Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.getServletContext().setAttribute("chairman", "XiDaDa");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}} //擷取屬性值
public class Demo3Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String v = (String) this.getServletContext().getAttribute("chairman");System.out.println(v);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
控制台輸出:XiDaDa
2.用來擷取web應用的初始化參數
整個web應用的初始化參數,位於web.xml檔案中的<web-app>節點下。
請求參數 parameter --- 瀏覽器發送過來的請求中的參數資訊
初始化參數 initparameter --- 在web.xml中為Servlet或ServletContext配置的初始化時帶有的基本參數
域屬性 attribute --- 四大作用於中存取的鍵值對
public class Demo4Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/** * 擷取整個web應用的初始化資訊 * ServletContext是整個web應用唯一的,所有的servlet都共用整個context * ServletConfig是每個servlet可以擷取的,從而得到當前servlet的一些資訊 */ServletContext sContext = this.getServletContext();Enumeration names = sContext.getInitParameterNames();while (names.hasMoreElements()) {String name = (String) names.nextElement();String value = sContext.getInitParameter(name);System.out.println(name+":"+value);}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
3.實現Servlet的轉寄
重新導向 : 302+Location (比喻:你來借我錢,我沒有,我讓你去找我的助理借錢)
請求轉寄 : 伺服器內不進行資源流轉 (比喻:你來找我借錢,我沒有,我幫你找我的助理借錢)
*請求轉寄是一次請求一次響應實現資源流轉.請求重新導向兩次請求兩次響應.
public class Demo5Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {RequestDispatcher requestDispatcher = this.getServletContext().getRequestDispatcher("/servlet/Demo6Servlet"); //轉寄到Demo6ServletrequestDispatcher.forward(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}Demo6Servlet檔案:
public class Demo6Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.getWriter().write("$99999999999999999999");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
4.載入資源檔
①第一種情況是在在Servlet中讀取資源檔。
在Servlet中讀取資源檔時:
如果寫相對路徑和絕對路徑,由於路徑將會相對於程式啟動的目錄--在web環境下,就是tomcat啟動的目錄即tomcat/bin--所有找不到資源
如果寫硬碟路徑,可以找到資源,但是只要一換髮布環境,這個硬碟路徑很可能是錯誤的,同樣不行.
為瞭解決這樣的問題ServletContext提供了getRealPath方法,在這個方法中傳入一個路徑,這個方法的底層會在傳入的路徑前拼接當前web應用的硬碟路徑從而得到當前資源的硬碟路徑,這種方式即使換了發布環境,方法的底層也能得到正確的web應用的路徑從而永遠都是正確的資源的路徑
this.getServletContext().getRealPath("config.properties")
舉例:先在專案檔下建立config.properties檔案,在其中寫入以下代碼:
username=zhang
password=123
servlet存取碼:
public class Demo7Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Properties prop = new Properties();//prop.load(new FileReader("D:\\tomcat6\\webapps\\Day0202\\config.properties")); //這種方法可以,但是不好!!!prop.load(new FileReader(this.getServletContext().getRealPath("config.properties")));//推薦使用的方法System.out.println(prop.getProperty("username"));System.out.println(prop.getProperty("password"));}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
②第二種情況是在非Servlet環境下
比如,我又單開一個包,包中有個service方法,需要載入資源檔。如何解決?
如果在非Servlet環境下要讀取資源檔時可以採用類載入器負載檔案的方式讀取資源
Service.class.getClassLoader().getResource("../../../config
包com.jnu509中的檔案:
package com.jnu509;import com.jnu509.service.Service;public class Demo8Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Service service = new Service();service.method();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
包com.jnu509.service中的檔案,可以看到,我們現在要訪問項目中的config.properties檔案。
package com.jnu509.service;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Properties;public class Service {public void method() throws FileNotFoundException, IOException {Properties prop = new Properties();prop.load(new FileReader(Service.class.getClassLoader().getResource("../../config.properties").getPath()));System.out.println(prop.getProperty("user"));System.out.println(prop.getProperty("password"));}}
上面提到,要使用java的類載入器,getClassLoader,類載入器既然能將類載入進記憶體那麼也可以載入資源檔。具體訪問方式看: