您好,登錄后才能下訂單哦!
本文實例為大家分享了servlet過濾器的具體代碼,供大家參考,具體內容如下
1.servlet過濾器產生背景
項目中我們會遇到這樣一類的需求,對訪問服務器的用戶ip實施過濾,只有在允許名單中的ip才能訪問服務,為了實現需求,每當有客戶端請求時,我們都會寫校驗ip的代碼,客戶端能夠訪問到的servlet我們都需要這樣做,很明顯這樣做有一個缺點,就是代碼冗余,維護不方便,如果驗證規則改變,修改起來也特別麻煩。
為了解決以上問題,Filter 技術應運而生。
2.servlet過濾器是什么?
servlet過濾器是在java servlet 2.3 中定義的。它能夠對servlet容器傳給servlet組件的ServletRequest 對象和 ServletResponse對象進行檢查和修改。
3.案例
所有servlet過濾器都要實現javax.servlet.Filter接口。
NoteFilter
package com.learn; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/29. */ public class NoteFilter implements Filter { private FilterConfig config = null; private String ipTable = null; //ip名單 @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("note filter initial"); this.config = filterConfig; this.ipTable = config.getInitParameter("ipTable"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("do filter starting"); //校驗ip地址 if(!veryfyIP(request,response)) return; long befor = System.currentTimeMillis(); config.getServletContext().log("before call note Filter"); chain.doFilter(request,response); config.getServletContext().log("after call note Filter"); long after = System.currentTimeMillis(); String name =""; if(request instanceof HttpServletRequest){ name = ((HttpServletRequest) request).getRequestURI(); } config.getServletContext().log("Note Filter : name:"+name +" time :"+(after -befor)+"ms"); } @Override public void destroy() { } private boolean veryfyIP(ServletRequest request, ServletResponse response){ String ip = request.getRemoteAddr(); System.out.println("請求ip:"+ip); System.out.println("ipTable 黑名單:"+ipTable); if(ip.indexOf(ipTable) == -1){ System.out.println("校驗不通過"); response.setContentType("text/html"); PrintWriter out = null; try { out = response.getWriter(); out.print("<h2>對不起,你的ip不能訪問服務器</h2>"); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } return false; } else { return true; } } }
NoteServlet
package com.learn; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by Administrator on 2017/09/29. */ public class NoteServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("已經通過了過濾器"); } }
web.xml配置
<filter> <filter-name>ip</filter-name> <filter-class>com.learn.NoteFilter</filter-class> <init-param> <param-name>ipTable</param-name> <param-value>127.0.0.1</param-value> </init-param> </filter> <filter-mapping> <filter-name>ip</filter-name> <url-pattern>/note</url-pattern> </filter-mapping>
<servlet> <servlet-name>note</servlet-name> <servlet-class>com.learn.NoteServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>note</servlet-name> <url-pattern>/note</url-pattern> </servlet-mapping>
結果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。