今天在公司看到有同事在寫filter
jsp開發中的監聽器和過濾器
案例描述
假設你平時生活在北京
你為了從村民那裡帶走盡量多的東西
代碼模擬
恩
<html>
<body>
<h
<a>給紅薯</a><br/>
<a>給錢</a><br/>
</body>
</html>
packagechb
importjava
publicclassBigBag
publicstaticHashMap<String
}
packagechb
importjava
importjavax
importjavax
publicclassMyListenerimplementsServletContextListener
publicvoidcontextDestroyed(ServletContextEventarg
BigBag
System
}
publicvoidcontextInitialized(ServletContextEventarg
BigBag
System
}
}
packagechb
importjava
importjavax
importjavax
importjavax
importjavax
importjavax
importjavax
importjavax
importjavax
publicclassMyFilterimplementsFilter
publicvoiddestroy()
}
publicvoiddoFilter(ServletRequestrequest
FilterChainchain)throwsIOException
HttpServletRequestreq=(HttpServletRequest)request;
HttpServletResponsersp=(HttpServletResponse)response;
if(req
//什麼時候有過錢啊?打回北京去
rsp
System
return;
}elseif(req
if(ntainsKey(
intnum=BigBag
BigBag
System
}else
BigBag
System
}
//村長同意了
chain
}else
//只要不要錢就行
chain
}
}
publicvoidinit(FilterConfigfilterConfig)throwsServletException
}
}
<?xmlversion=
<web
xmlns:xsi=
xsi:schemaLocation=
<filter>
<filter
<filter
</filter>
<filter
<filter
<url
</filter
<listener>
<listener
</listener>
<welcome
<welcome
</welcome
</web
好了
分析結果
我們會觀察到如下結果
當服務啟動過程中
當點擊一下
當點擊一下
怎麼樣?有點明白了?
其實
過濾器就是針對服務器的請求
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27012.html