1.過濾器
Servlet中的過濾器Filter是實現了javax.servlet.Filter接口的服務器端程序,主要的用途是過濾字符編碼、 作一些業務邏輯判斷等。其工做原理是,只要你在web.xml文件配置好要攔截的客戶端請求,它都會幫你攔截到請求,此時你就能夠對請求或響應(Request、Response)統一設置編碼,簡化操做;同時還可進行邏輯判斷,如用戶是否已經登錄、有沒有權限訪問該頁面等等工做。它是隨你的web應用啓動而啓動的,只初始化一次, 之後就能夠攔截相關請求,只有當你的web應用中止或從新部署的時候才銷燬
主要目的:過濾字符編碼;其次,作一些應用邏輯判斷等. 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>
2.監聽器
Servlet容器提供了多種監聽器的接口,使用的時候根據需求選擇特定的接口實現就行。這些接口都是繼承的java.util包中的EventListener接口。 html
能夠看出,監聽器的種類確實不少。java
經常使用的有:mysql
(1)request對象的監聽ServletRequestAttributeListener和ServletRequestListenerweb
ServletRequestListener用於監聽用戶請求的建立和銷燬。spring
ServletRequestAttributeListener則用於監聽ServletRequest(request)範圍內屬性的變化,實現該接口的監聽器須要實現attributeAdded、attributeRemoved、attributeReplaced三個方法,分別監聽Request的增長,刪除和修改。sql
(2)Session對象的監聽:HttpSessionAttributeListener和HttpSessionListener 數據庫
HttpSessionListener監聽HttpSession的操做。當建立一個Session時,激發sessionCreated(SessionEvent se)方法;當銷燬一個 Session時,激發sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener監聽HttpSession中的屬性的操做。當在Session增長一個屬性時,激發 attributeAdded(HttpSessionBindingEvent se) 方法;當在Session刪除一個屬性時,激發 attributeRemoved(HttpSessionBindingEvent se)方法;當在Session屬性被從新設置時,激發 attributeReplaced(HttpSessionBindingEvent se) 方法。編程
(3)ServletContext對象的監聽:ServletContextAttributeListener和ServletContextListener。服務器
ServletContextAttributeListener監聽對ServletContext屬性的操做,好比增長/刪除/修改 session
ServletContextListener監聽ServletContext,當建立ServletContext時,激發 contextInitialized (ServletContextEvent sce)方法;當銷燬ServletContext時,激發 contextDestroyed(ServletContextEvent sce)方法。
監聽器Listener,它是實現了javax.servlet.ServletContextListener 接口的服務器端程序,它也是隨web應用的啓動而啓動,只初始化一次,隨web應用的中止而銷燬。主要做用是: 作一些初始化的內容添加工做、設置一些基本的內容、好比一些參數或者是一些固定的對象等等。下面利用監聽器對數據庫鏈接池DataSource的初始化演示它的使用:
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>
這樣配置好了以後,之後在web應用中就能夠經過ServletContext取得BasicDataSource對象,從而獲取與數據庫的鏈接,提升性能,方便使用。
3.攔截器
攔截器是在面向切面編程中應用的,就是在你的service或者一個方法前調用一個方法,或者在方法後調用一個方法。是基於JAVA的反射機制。