Servlet過濾器從字面上的字意理解爲景觀一層次的過濾處理才達到使用的要求,而其實Servlet過濾器就是服務器與客戶端請求與響應的中間層組件,在實際項目開發中Servlet過濾器主要用於對瀏覽器的請求進行過濾處理,將過濾後的請求再轉給下一個資源。javascript
過濾器的基本概念css
package com.oumyye.過濾器; 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 SimpleFilter implements Filter { public void init(FilterConfig config) throws ServletException { // 初始化過濾器 String initParam = config.getInitParameter("ref"); // 取得初始化參數 System.out.println("** 過濾器初始化,初始化參數 = " + initParam); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 執行過濾 System.out.println("** 執行doFilter()方法以前。"); chain.doFilter(request, response); // 將請求繼續傳遞 System.out.println("** 執行doFilter()方法以後。"); } public void destroy() { // 銷燬過濾 System.out.println("** 過濾器銷燬。"); } }
配置web.xmlhtml
<filter> <filter-name>simple</filter-name> <filter-class>com.oumyye.過濾器.SimpleFilter</filter-class>
<init-param>
<param-name>ref</param-name>
<param-value>HELLOMLDN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>simple</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
過濾器的應用 —— 編碼過濾 java
package com.oumyye.過濾器; 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 EncodingFilter implements Filter { private String charSet; // 設置字符編碼 public void init(FilterConfig config) throws ServletException { this.charSet = config.getInitParameter("charset"); // 取得初始化參數 } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(this.charSet); // 設置統一編碼 } public void destroy() {
} }
配置web.xml文件 web
<filter> <filter-name>encoding</filter-name> <filter-class>com.oumyye.過濾器.EncodingFilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
過濾器的應用---登錄驗證 sql
package com.oumyye.過濾器; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class FilterLogin extends HttpServlet implements Filter { private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException { HttpSession session=((HttpServletRequest)request).getSession(); response.setCharacterEncoding("gb2312"); //響應客戶端類型 if(session.getAttribute("user")==null){ //判斷session中是否有user這個對象 PrintWriter out=response.getWriter(); //建立一個輸出流 //若是爲空則經過javaScript腳本出輸出提示並跳轉到index.jsp頁面 out.print("<script language=javascript>alert('您尚未登陸!!!');window.location.href='../index.jsp';</script>"); }else{ filterChain.doFilter(request, response);//不然繼續執行 } } public void destroy() { } }
User.java瀏覽器
package com.mr.filter; public class User { private String username; private String password; public String getUsername() { return username; } public String getPassword() { return password; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } }
配置web.XML服務器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>filterUser</filter-name> <filter-class>com.oumyye.過濾器.FilterLogin</filter-class> </filter> <filter-mapping> <filter-name>filterUser</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
jsp頁面:session
index.jspapp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <link href="css/style.css" rel="stylesheet" type="text/css" > <script language="javascript" type=""> function checkEmpty(){ if(document.form.name.value==""){ alert("用戶名不能爲空") document.form.name.focus(); return false; } if(document.form.password.value==""){ alert("密碼不能爲空") document.form.password.focus(); return false; } } </script> <title>使用過濾器身份驗證</title> </head> <body> <h3> </h3> <p align="center">使用過濾器身份驗證</p> <form name="form" method="post" action="loginresult.jsp" onSubmit="return checkEmpty()"> <table width="220" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="808080"> <tr> <td align="center">用戶名:</td> <td ><input name="name" type="text"></td> </tr> <tr> <td align="center">密 碼:</td> <td><input name="password" type="password"></td> </tr> <tr> <td align="center" colspan="2"> <input type="submit" name="Submit" value="登陸"> <input type="submit" value="退出"/> </td> </tr> </table><br> </form> </body> </html>
loginresult.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %> <%@ page import="com.mr.filter.User"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>使用過濾器身份驗證</title> </head> <% request.setCharacterEncoding("gb2312"); String name=request.getParameter("name"); String password=request.getParameter("password"); User user=new User(); user.setUsername(name); user.setPassword(password); session.setAttribute("user",user); response.sendRedirect("filter/loginsuccee.jsp"); %> <body> </body> </html>
loginsuccee.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <%@ page import="com.mr.filter.User"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>使用過濾器身份驗證</title> </head> <body><div align="center"> <table width="333" height="285" cellpadding="0" cellspacing="0"> <tr> <td align="center"> <p>您己成功登陸</p> <p><br> <a href="backtrack.jsp">返回</a> </p></td> </tr> </table> </div> </body> </html>
backtrack.jsp
<% session.invalidate(); out.print("<script language='javascript'>window.location.href='../index.jsp';</script>"); %>
小結: