servlet過(guò)濾器(Filter)詳解(九)
本文實(shí)例為大家分享了servlet過(guò)濾器的具體代碼,供大家參考,具體內(nèi)容如下
1.servlet過(guò)濾器產(chǎn)生背景
項(xiàng)目中我們會(huì)遇到這樣一類的需求,對(duì)訪問(wèn)服務(wù)器的用戶ip實(shí)施過(guò)濾,只有在允許名單中的ip才能訪問(wèn)服務(wù),為了實(shí)現(xiàn)需求,每當(dāng)有客戶端請(qǐng)求時(shí),我們都會(huì)寫校驗(yàn)ip的代碼,客戶端能夠訪問(wèn)到的servlet我們都需要這樣做,很明顯這樣做有一個(gè)缺點(diǎn),就是代碼冗余,維護(hù)不方便,如果驗(yàn)證規(guī)則改變,修改起來(lái)也特別麻煩。
為了解決以上問(wèn)題,F(xiàn)ilter 技術(shù)應(yīng)運(yùn)而生。
2.servlet過(guò)濾器是什么?
servlet過(guò)濾器是在java servlet 2.3 中定義的。它能夠?qū)ervlet容器傳給servlet組件的ServletRequest 對(duì)象和 ServletResponse對(duì)象進(jìn)行檢查和修改。
3.案例
所有servlet過(guò)濾器都要實(shí)現(xiàn)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");
//校驗(yàn)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("請(qǐng)求ip:"+ip);
System.out.println("ipTable 黑名單:"+ipTable);
if(ip.indexOf(ipTable) == -1){
System.out.println("校驗(yàn)不通過(guò)");
response.setContentType("text/html");
PrintWriter out = null;
try {
out = response.getWriter();
out.print("<h1>對(duì)不起,你的ip不能訪問(wèn)服務(wù)器</h1>");
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("已經(jīng)通過(guò)了過(guò)濾器");
}
}
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>
結(jié)果如下:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)前端驗(yàn)證碼圖片生成和校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)前端驗(yàn)證碼圖片生成和校驗(yàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
特殊數(shù)據(jù)結(jié)構(gòu)之使用Java實(shí)現(xiàn)單調(diào)棧示例
這篇文章主要為大家介紹了特殊數(shù)據(jù)結(jié)構(gòu)之使用Java實(shí)現(xiàn)單調(diào)棧示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Spring Cloud Config實(shí)現(xiàn)分布式配置中心
這篇文章主要介紹了Spring Cloud Config實(shí)現(xiàn)分布式配置中心,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Java中ArrayList和LinkedList之間的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java中ArrayList和LinkedList之間的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
idea插件之如何使用JarEditor編輯Java JAR文件
JarEditor是一款用于在IntelliJIDEA中直接編輯JAR文件的插件,支持反編譯查看和編輯.class文件,并提供即時(shí)編譯與保存功能,通過(guò)JarEditor,用戶可以在IDE內(nèi)一站式完成JAR文件的編輯、管理和打包操作,提高開(kāi)發(fā)效率,但在生產(chǎn)環(huán)境中使用前,請(qǐng)確保備份并測(cè)試修改2025-01-01
詳解Java圖形化編程中的鼠標(biāo)事件設(shè)計(jì)
這篇文章主要介紹了Java圖形化編程中的鼠標(biāo)事件設(shè)計(jì),是Java的GUI開(kāi)發(fā)中的基礎(chǔ)部分,需要的朋友可以參考下2015-10-10

