1. 程式人生 > >web.xml中的配置,servlet,filter,listener的作用和原理

web.xml中的配置,servlet,filter,listener的作用和原理

首先介紹servlet,filter和listen的原理:

servlet可以說是動態頁面的基石,現在很多開發都是基於spring等各種框架,所以對servlet的瞭解可能少點,下面先用簡單的例子,說明下servlet的作用

MyFirstServlet.java

class MyFirstServlet implements Servlet{

   //該函式用於初始化servelet就是把該servlet裝載到記憶體中
   //該函式只會被呼叫一次
   public void init(ServletConfig config) throws ServletException{

   }

   //得到ServeletConfig物件
   public ServletConfig getServletConfig(){

      return null;
   }  

   //該函式是服務函式,我們的業務邏輯程式碼就是寫在這裡
   //該函式每次都會被呼叫
   public void service(ServletRequest request, ServletResponse response)

             throws ServletException, IOException{
   }

   //g該函式使得到servlet配置資訊
   public String getServletInfo(){
      return null;
   }

   //銷燬該servlet,從記憶體中清除掉,僅會被呼叫一次
   public void destroy(){

   }

}

     web中的配置如下

 <!--在這裡宣告servlet,並且給servlet起別名-->
    <servlet>
       <!--servlet-name是給該servlet指定名字-->
        <servlet-name>MyFirstServlet</servlet-name>
        <!--serlet-class要指明該Servlet放在哪個包下-->
        <servlet-class>com.hsm.MyFirstServlet</servlet-class>
    </servlet>

   <!--Servlet的對映-->
    <servlet-mapping>
         <!--這個servlet-name要和上面的servlet-name相同-->
        <servlet-name>MyFirstServlet</servlet-name>
        <!--URL-PATTERN這裡就是將來訪問該Servlet的資源名部分-->
        <url-pattern>/ABC</url-pattern>
    </servlet-mapping>

訪問路徑如下

http://localhost/*****/ABC

這裡的ABC對應上面servlet-mapping中url-pattern的屬性,所以最終請求會呼叫MyFirstServlet的service

 

Servlet中的過濾器Filter是實現了javax.servlet.Filter介面的伺服器端程式,主要的用途是過濾字元編碼、做一些業務邏輯判斷等。其工作原理是,只要你在web.xml檔案配置好要攔截的客戶端請求,它都會幫你攔截到請求,此時你就可以對請求或響應(Request、Response)統一設定編碼,簡化操作;同時還可進行邏輯判斷,如使用者是否已經登陸、有沒有許可權訪問該頁面等等工作。它是隨你的web應用啟動而啟動的,只初始化一次,以後就可以攔截相關請求,只有當你的web應用停止或重新部署的時候才銷燬,以下通過過濾編碼的程式碼示例來了解它的使用:

package com.hello.web.listener;
 
import java.io.IOException;
 
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
// 主要目的:過濾字元編碼;其次,做一些應用邏輯判斷等.
// Filter跟web應用一起啟動
// 當web應用重新啟動或銷燬時,Filter也被銷燬
public class MyCharsetFilter implements Filter
{
	private FilterConfig config = null;
	
	public void destroy()
	{
		System.out.println("MyCharsetFilter準備銷燬...");
	}
 
	public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain chain) throws IOException, ServletException
	{
		// 強制型別轉換
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		// 獲取web.xm設定的編碼集,設定到Request、Response中
		request.setCharacterEncoding(config.getInitParameter("charset"));
		response.setContentType(config.getInitParameter("contentType"));
		response.setCharacterEncoding(config.getInitParameter("charset"));
		// 將請求轉發到目的地
		chain.doFilter(request, response);
	}
 
	public void init(FilterConfig arg0) throws ServletException
	{
		this.config = arg0;
		System.out.println("MyCharsetFilter初始化...");
	}
}

以下是 MyCharsetFilter.java 在web.xml 中配置:

<filter>
      <filter-name>filter</filter-name>
      <filter-class>dc.gz.filters.MyCharsetFilter</filter-class>
      <init-param>
          <param-name>charset</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
      <init-param>
          <param-name>contentType</param-name>
          <param-value>text/html;charset=UTF-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>filter</filter-name>
      <!-- * 代表截獲所有的請求  或指定請求,如/test.do  /xxx.do -->
      <url-pattern>/*</url-pattern>
  </filter-mapping>

以上的例子簡單的說明了Filter的使用,具體其他的應用可以看具體的場景。

現在來說說Servlet的監聽器Listener,它是實現了javax.servlet.ServletContextListener 介面的伺服器端程式,它也是隨web應用的啟動而啟動,只初始化一次,隨web應用的停止而銷燬。主要作用是: 做一些初始化的內容新增工作、設定一些基本的內容、比如一些引數或者是一些固定的物件等等。下面利用監聽器對資料庫連線池DataSource的初始化演示它的使用:

MyServletContextListener.java
package dc.gz.listeners;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.dbcp.BasicDataSource;
 
/**
 * Web應用監聽器
 */
public class MyServletContextListener implements ServletContextListener {
 
    // 應用監聽器的銷燬方法
    public void contextDestroyed(ServletContextEvent event) {
        ServletContext sc = event.getServletContext();
        // 在整個web應用銷燬之前呼叫,將所有應用空間所設定的內容清空
        sc.removeAttribute("dataSource");
        System.out.println("銷燬工作完成...");
    }
 
    // 應用監聽器的初始化方法
    public void contextInitialized(ServletContextEvent event) {
        // 通過這個事件可以獲取整個應用的空間
        // 在整個web應用下面啟動的時候做一些初始化的內容新增工作
        ServletContext sc = event.getServletContext();
        // 設定一些基本的內容;比如一些引數或者是一些固定的物件
        // 建立DataSource物件,連線池技術 dbcp
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName("com.mysql.jdbc.Driver");
        bds.setUrl("jdbc:mysql://localhost:3306/hibernate");
        bds.setUsername("root");
        bds.setPassword("root");
        bds.setMaxActive(10);//最大連線數
        bds.setMaxIdle(5);//最大管理數
        //bds.setMaxWait(maxWait); 最大等待時間
        // 把 DataSource 放入ServletContext空間中,
        // 供整個web應用的使用(獲取資料庫連線)
        sc.setAttribute("dataSource", bds);
        System.out.println("應用監聽器初始化工作完成...");
        System.out.println("已經建立DataSource...");
    }
}

 

web.xml中配置如下,很簡單:

<!-- 配置應用監聽器  -->
<listener>
	<listener-class>dc.gz.listeners.MyServletContextListener</listener-class>
</listener>

通過如下方式可以獲取datasource

        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        ServletContext servletContext = webApplicationContext.getServletContext();
        servletContext.getAttribute("dataSource");
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();

有了上面的鋪墊,更容易理解spring中web.xml所包含的配置和作用

<?xml version="1.0" encoding="UTF-8"?>  
<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 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  

    <!-- 在Spring框架中是如何解決從頁面傳來的字串的編碼問題的呢?
    下面我們來看看Spring框架給我們提供過濾器CharacterEncodingFilter  
     這個過濾器就是針對於每次瀏覽器請求進行過濾的,然後再其之上添加了父類沒有的功能即處理字元編碼。  
      其中encoding用來設定編碼格式,forceEncoding用來設定是否理會
 request.getCharacterEncoding()方法,設定為true則強制覆蓋之前的編碼格式。-->  
    <filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    <!-- 專案中使用Spring 時,applicationContext.xml配置檔案中並沒有BeanFactory,要想在業
務層中的class 檔案中直接引用Spring容器管理的bean可通過以下方式-->  
    <!--1、在web.xml配置監聽器ContextLoaderListener-->  
    <!--ContextLoaderListener的作用就是啟動Web容器時,自動裝配ApplicationContext的配置資訊。因為
它實現了ServletContextListener這個介面,在web.xml配置這個監聽器,啟動容器時,就會預設執行它實現的方法。
使用servletContextListener介面,開發者能夠在為客戶端請求提供服務之前向servletContext中添任何物件,
這個物件在servletcontxxt啟動的時候被初始化,然後在ervletContext的整個執行期間都是可見的。
    在ContextLoaderListener中關聯了ContextLoader這個類,所以整個載入配置過程由ContextLoader來完成。  
    它的API說明  
    第一段說明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。  
    如果檢視ContextLoaderServlet的API,可以看到它也關聯了ContextLoader這個類而且它實現了HttpServlet這個介面  
    第二段,ContextLoader建立的是 XmlWebApplicationContext這樣一個類,它實現的介面是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->  
    BeanFactory這樣一來spring中的所有bean都由這個類來建立  
     IUploaddatafileManager uploadmanager = (IUploaddatafileManager)    ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager");
     -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!--2、部署applicationContext的xml檔案-->  
    <!--如果在web.xml中不寫任何引數配置資訊,預設的路徑是"/WEB-INF/applicationContext.xml,  
    在WEB-INF目錄下建立的xml檔案的名稱必須是applicationContext.xml。  
    如果是要自定義檔名可以在web.xml里加入contextConfigLocation這個context引數:  
    在<param-value> </param-value>裡指定相應的xml檔名,如果有多個xml檔案,可以寫在一起並以“,”號分隔。  
    也可以這樣applicationContext-*.xml採用萬用字元,比如這那個目錄下有applicationContext-ibatis-base.xml,  
    applicationContext-action.xml,applicationContext-ibatis-dao.xml等檔案,都會一同被載入。  
    在ContextLoaderListener中關聯了ContextLoader這個類,所以整個載入配置過程由ContextLoader來完成。-->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring/applicationContext.xml</param-value>  
    </context-param>  

    <!--如果你的DispatcherServlet攔截"/",為了實現REST風格,攔截了所有的請求,那麼同時對*.js,*.jpg等靜態檔案的訪問也就被攔截了。-->  
    <!--方案一:啟用Tomcat的defaultServlet來處理靜態檔案-->  
    <!--要寫在DispatcherServlet的前面, 讓 defaultServlet先攔截請求,這樣請求就不會進入Spring了,我想效能是最好的吧。-->  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.css</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.swf</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.gif</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.jpg</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.png</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.js</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.html</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.xml</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.json</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.map</url-pattern>  
    </servlet-mapping>  
    <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一個Servlet,,所以可以配置多個DispatcherServlet-->  
    <!--DispatcherServlet是前置控制器,配置在web.xml檔案中的。攔截匹配的請求,Servlet攔截匹配規則要自已定義,把攔截下來的請求,依據某某規則分發到目標Controller(我們寫的Action)來處理。-->  
    <servlet>  
        <servlet-name>DispatcherServlet</servlet-name><!--在DispatcherServlet的初始化過程中,框架會在web應用的 WEB-INF資料夾下尋找名為[servlet-name]-servlet.xml 的配置檔案,生成檔案中定義的bean。-->  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <!--指明瞭配置檔案的檔名,不使用預設配置檔名,而使用dispatcher-servlet.xml配置檔案。-->  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <!--其中<param-value>**.xml</param-value> 這裡可以使用多種寫法-->  
            <!--1、不寫,使用預設值:/WEB-INF/<servlet-name>-servlet.xml-->  
            <!--2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>-->  
            <!--3、<param-value>classpath*:dispatcher-servlet.xml</param-value>-->  
            <!--4、多個值用逗號分隔-->  
            <param-value>classpath:spring/dispatcher-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup><!--是啟動順序,讓這個Servlet隨Servletp容器一起啟動。-->  
    </servlet>  
    <servlet-mapping>  
        <!--這個Servlet的名字是dispatcher,可以有多個DispatcherServlet,是通過名字來區分的。每一個DispatcherServlet有自己的WebApplicationContext上下文物件。同時儲存的ServletContext中和Request物件中.-->  
        <!--ApplicationContext是Spring的核心,Context我們通常解釋為上下文環境,我想用“容器”來表述它更容易理解一些,ApplicationContext則是“應用的容器”了:P,Spring把Bean放在這個容器中,在需要的時候,用getBean方法取出-->  
        <servlet-name>DispatcherServlet</servlet-name>  
        <!--Servlet攔截匹配規則可以自已定義,當對映為@RequestMapping("/user/add")時,為例,攔截哪種URL合適?-->  
        <!--1、攔截*.do、*.htm, 例如:/user/add.do,這是最傳統的方式,最簡單也最實用。不會導致靜態檔案(jpg,js,css)被攔截。-->  
        <!--2、攔截/,例如:/user/add,可以實現現在很流行的REST風格。很多網際網路型別的應用很喜歡這種風格的URL。弊端:會導致靜態檔案(jpg,js,css)被攔截後不能正常顯示。 -->  
        <url-pattern>/</url-pattern> <!--會攔截URL中帶“/”的請求。-->  
    </servlet-mapping>  

    <welcome-file-list><!--指定歡迎頁面-->  
        <welcome-file>login.html</welcome-file>  
    </welcome-file-list>  
    <error-page> <!--當系統出現404錯誤,跳轉到頁面nopage.html-->  
        <error-code>404</error-code>  
        <location>/nopage.html</location>  
    </error-page>  
    <error-page> <!--當系統出現java.lang.NullPointerException,跳轉到頁面error.html-->  
        <exception-type>java.lang.NullPointerException</exception-type>  
        <location>/error.html</location>  
    </error-page>  
    <session-config><!--會話超時配置,單位分鐘-->  
        <session-timeout>360</session-timeout>  
    </session-config>  
</web-app>

借鑑:

https://www.cnblogs.com/wkrbky/p/5929943.html

https://blog.csdn.net/pg_guo/article/details/9118633