Web開發基礎_Servlet學習_0030_Filter
阿新 • • 發佈:2018-12-10
Servlet過濾器Filter
案例演示:
工程案例目錄結構
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study</groupId> <artifactId>Jsp06</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> </dependencies> </project>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Jsp06</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>hi</servlet-name> <servlet-class>web.HiServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hi</servlet-name> <url-pattern>/hi</url-pattern> </servlet-mapping> <filter> <filter-name>first</filter-name> <filter-class>web.FirstFilter</filter-class> </filter> <filter-mapping> <filter-name>first</filter-name> <url-pattern>/hi</url-pattern> </filter-mapping> <filter> <filter-name>second</filter-name> <filter-class>web.SecondFilter</filter-class> <!-- 預置一個引數 --> <init-param> <param-name>city</param-name> <param-value>北京</param-value> </init-param> </filter> <!-- 多個Filter之間的順序以mapping順序為準 --> <filter-mapping> <filter-name>second</filter-name> <url-pattern>/hi</url-pattern> </filter-mapping> </web-app>
FirstFilter.java
package web; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class FirstFilter implements Filter{ public void destroy() { System.out.println("銷燬FirstServlet"); } /** * 實現過濾(攔截)的方法 * @param req Filter所過濾的請求的request物件 * @param res Filter所過濾的響應的response物件 * @param chain 過濾鏈物件,用來實現過濾 */ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { System.out.println("First過濾前"); chain.doFilter(req, res); System.out.println("First過濾後"); } public void init(FilterConfig arg0) throws ServletException { System.out.println("初始化FirstFilter"); } }
SecondFilter.java
package web;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SecondFilter implements Filter{
public void destroy() {
System.out.println("銷燬SecondFilter");
}
public void doFilter(ServletRequest req,
ServletResponse res, FilterChain chain)
throws IOException, ServletException {
System.out.println("Second過濾前");
chain.doFilter(req,res);
System.out.println("Second過濾後");
}
/**
* 1.伺服器在呼叫init前會自動建立FilterConfig
* 2.伺服器會自動讀取web.xml中的引數存入FilterConfig
* 3.伺服器在呼叫init時會自動傳入FilterConfg
*/
public void init(FilterConfig cfg) throws ServletException {
System.out.println("初始化SecondFilter");
System.out.println(cfg.getInitParameter("city"));
}
}
HiServlet.java
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class HiServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
System.out.println("呼叫HiServlet。。。。");
HttpSession session = req.getSession();
session.setAttribute("x", 2);
}
}
將Jsp06工程部署到Tomcat上,執行Tomcat啟動案例工程,
A
、
B
可以看到過濾器執行順序以web.xml中mapping定義順序為準
拓展:監聽器
A. web監聽器是一種Servlet中的特殊的類,它們能幫助開發者監聽web中的特定事件,
比如ServletContext,HttpSession,ServletRequest的建立和銷燬;變數的建立、銷燬和修改等。可以在某些動作前後增加處理,實現監控。
B. 監聽器常用的用途
通常使用Web監聽器做以下的內容:
統計線上人數,利用HttpSessionLisener
載入初始化資訊:利用ServletContextListener
統計網站訪問量
實現訪問監控
基於上面的案例來演示
編寫監聽器
MyListener.java
package web;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MyListener implements HttpSessionListener,HttpSessionAttributeListener{
public void attributeAdded(HttpSessionBindingEvent e) {
System.out.println("向session中新增資料");
System.out.println(e.getSession());
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
//session建立事件,建立session時自動觸發
public void sessionCreated(HttpSessionEvent e) {
System.out.println("建立session");
System.out.println(e.getSession());
}
//session銷燬事件,銷燬session時自動觸發
public void sessionDestroyed(HttpSessionEvent e) {
System.out.println("銷燬session");
}
}
在web.xml中新增監聽配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Jsp06</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hi</servlet-name>
<servlet-class>web.HiServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hi</servlet-name>
<url-pattern>/hi</url-pattern>
</servlet-mapping>
<filter>
<filter-name>first</filter-name>
<filter-class>web.FirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>first</filter-name>
<url-pattern>/hi</url-pattern>
</filter-mapping>
<filter>
<filter-name>second</filter-name>
<filter-class>web.SecondFilter</filter-class>
<!-- 預置一個引數 -->
<init-param>
<param-name>city</param-name>
<param-value>北京</param-value>
</init-param>
</filter>
<!-- 多個Filter之間的順序以mapping順序為準 -->
<filter-mapping>
<filter-name>second</filter-name>
<url-pattern>/hi</url-pattern>
</filter-mapping>
<listener>
<listener-class>web.MyListener</listener-class>
</listener>
</web-app>
再次將Jsp06工程部署到Tomcat上,執行Tomcat啟動案例工程,