JavaWeb開發01——Servlet

Servlet

一.生命週期

一般狀況下,servlet第一次被訪問的時候在內存中建立對象,在建立後當即調用init()方法進行初始化。web

對於每一次請求都掉用service(req,resp)方法處理請求,此時會用Request對象封裝請求信息,並用Response對象(最初是空的)表明響應消息,傳入到service方法裏供使用。當service方法處理完成後,返回服務器服務器根據Response中的信息組織稱響應消息返回給瀏覽器。瀏覽器

響應結束後servlet並不銷燬,一直駐留在內存中等待下一次請求。直到服務器關閉或web應用被移除出虛擬主機,servlet對象銷燬並在銷燬前調用destroy()方法作一些善後的事情。tomcat

二.ServletConfig: 表明當前Servlet在web.xml中的配置信息

1.String getServletName() -- 獲取當前Servlet在web.xml中配置的名字
ServletConfig config = this.getServletConfig();
String name = config.getServletName();
2.String getInitParameter(String name) -- 獲取當前Servlet指定名稱的初始化參數的值
ServletConfig config = this.getServletConfig();
String value = config.getInitParameter("name");
3.Enumeration getInitParameterNames() -- 獲取當前Servlet全部初始化參數的名字組成的枚舉
ServletConfig config = this.getServletConfig();
Enumeration enumeration = config.getInitParameterNames();
while(enumeration.hasMoreElements()){
        String name = (String)enumeration.nextElement();
        String value = config.getInitParameter(name);
        System.out.println(name+value);
        }
三.ServletContext -- 表明當前web應用

1.作爲域對象能夠在整個web應用範圍內共享數據

void setAttribute(String,Object);
Object getAttribute(String);
void removeAttribute(String);服務器

域對象:在一個能夠被看見的範圍內共享數據用到對象。app

做用範圍:整個web應用範圍內共享數據。this

生命週期:當服務器啓動web應用加載後建立出ServletContext對象後,域產生。當web應用被移除出容器或服務器關閉,隨着web應用的銷燬,域銷燬。spa

ServletDemo1:
ServletContext context = this.getServletContext();
context.getAttribute("apple","red apple");

ServletDemo2:
ServletContext context = this.getServletContext();
String v = (String)context.getAttribute("apple");
System.out.println(v);

ServletDemo2打印結果:red apple
2.用來獲取web應用的初始化參數

初始化參數 initparameter --- 在web.xml中爲Servlet或ServletContext配置的初始化時帶有的基本參數code

web.xml:
<context-param>
    <param-name>username</param-name>
    <param-value>zhang</param-name>
</context-param>
<context-param>
    <param-name>password</param-name>
    <param-value>123</param-name>
</context-param>

ServletDemo3:
ServletContext context = this.getServleyContext();
Enumeration enumeration = context.getInitParameterNames();
while(enumeration.hasMoreElements()){
       String name = (String)enumeration.nextElement();
       String value = context.getInitParameter(name);
       System.out.println(name+value);
       }
       
ServletDemo3 打印結果:
usename:zhang
password:123
3.實現Servlet的轉發

this.getServletContext.getRequestDispatcher("XXXX")xml

ServletDemo5:
response.getWriter().write("111111");

ServeltDemo6:
RequestDispatcher dispatcher = this.getServletContext.getRequestDispatcher("/servlet/ServletDemo5");
dispatcher.forward(request,response);

ServletDemo6 在瀏覽器上打印的結果:111111
Tip:

println是將數據在軟件上打印出來
response.getWriter.write()是將數據在瀏覽器上打印出來對象

4.加載資源文件

在Servlet中讀取資源文件時,若是寫相對路徑和絕對路徑,因爲路徑將會相對於程序啓動的目錄--在web環境下,就是tomcat啓動的目錄即tomcat/bin--因此找不到資源。若是寫硬盤路徑,能夠找到資源,可是隻要一換髮布環境,這個硬盤路徑極可能是錯誤的,一樣不行。

爲了解決這樣的問題ServletContext提供了getRealPath方法。在這個方法中傳入一個路徑,這個方法的底層會在傳入的路徑前拼接當前web應用的硬盤路徑,從而獲得當前資源的硬盤路徑。這種方式即便換了發佈環境,方法的底層也能獲得正確的web應用的路徑從而永遠都是正確的資源的路徑 --this.getServletContext().getRealPath("config.properties")

若是在非Servlet環境下要讀取資源文件時能夠採用類加載器加載文件的方式讀取資源 -- Service.class.getClassLoader().getResource("../../../config.properties").getPath()

config.properties:
usename = zhang
password = 123

Properties prop = new Properties();
prop.load(new FileReader(this.getServletContext().getRealPath("config.properties")));
System.out.println(prop.getProperty("usename"));
System.out.println(prop.getProperty("password"));

打印結果:
zhang
123
相關文章
相關標籤/搜索