




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Javafilter中的chain.doFilter使用詳解目錄filter中的chain.doFilter使用chain.doFilter作用舉例說(shuō)明Filter中chain.doFilter(req,res)的理解用法
filter中的chain.doFilter使用
chain.doFilter作用
1.一般filter都是一個(gè)鏈,web.xml里面配置了幾個(gè)就有幾個(gè)。一個(gè)一個(gè)的連在一起
request-filter1-filter2-filter3-….-requestresource.
2.chain.doFilter將請(qǐng)求轉(zhuǎn)發(fā)給過(guò)濾器鏈下一個(gè)filter,如果沒(méi)有filter那就是你請(qǐng)求的資源
舉例說(shuō)明
1.input.jsp是用來(lái)提交輸入的:當(dāng)提交后,過(guò)濾器檢測(cè)姓名和年齡.
2.如果正常的話會(huì)提交給output.jsp如果不正常提交給erroroutput.jsp.
3.在此,同時(shí)也有一個(gè)過(guò)濾器,防止亂碼問(wèn)題存在,該過(guò)濾器檢測(cè)一個(gè)頁(yè)面是否設(shè)置了字符編碼,如果沒(méi)有則進(jìn)行設(shè)置。
1.input.jsp
formaction="output.jsp"name="form"method="post"
table
tdname/td
tdinputtype="text"name="name"http://td
/tr
tdage/td
tdinputtype="text"name="age"http://td
/tr
tdinputtype="submit"name="ok"value="ok"http://td
/tr
/table
/form
2.web.xml
filter
description/description
display-nameencodefilter/display-name
filter-nameencodefilter/filter-name
filter-classservletbean.encodefilter/filter-class
init-param
param-nameencoding/param-name
param-valueGB2312/param-value
/init-param
/filter
filter-mapping
filter-nameencodefilter/filter-name
url-pattern/*/url-pattern
/filter-mapping
filter
description/description
display-namemyfilter/display-name
filter-namemyfilter/filter-name
filter-classservletbean.myfilter/filter-class
/filter
filter-mapping
filter-namemyfilter/filter-name
url-pattern/output.jsp/url-pattern
/filter-mapping
3.encodefilter.java
packageservletbean;
publicclassencodefilterimplementsFilter{
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{
if(request.getCharacterEncoding()==null){
System.out.println(encoding);
request.setCharacterEncoding(encoding);
chain.doFilter(request,response);//到下一個(gè)鏈
publicvoidinit(FilterConfigfConfig)throwsServletException{
this.config=fConfig;
encoding=fConfig.getInitParameter("encoding");//獲得目標(biāo)編碼格式
}
3.myfilter.java
packageservletbean;
importjavax.swing.JOptionPane;
publicclassmyfilterimplementsFilter{
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{
response.setContentType("text/html");
response.setCharacterEncoding("GB2312");
PrintWriterout=response.getWriter();
Stringname="";
Stringage="";
intage1;
name=request.getParameter("name");
age=request.getParameter("age");
RequestDispatcherdispatch=request.getRequestDispatcher("erroroutput.jsp");
if(name==null||name==""||name==""||age==null){
JOptionPane.showMessageDialog(null,"用戶名和年齡輸入錯(cuò)誤!");
dispatch.forward(request,response);
return;
else{
try{
age1=Integer.parseInt(age);
}catch(Exceptione){
//JOptionPane.showMessageDialog(null,"年齡必須為數(shù)字!");
dispatch.forward(request,response);
return;//如果是錯(cuò)誤頁(yè)面就到erroroutput.jsp中
//這里表示是正確的,也就是說(shuō),他回去找下一個(gè)鏈,但是它下面已經(jīng)沒(méi)有了,所以就會(huì)去跳轉(zhuǎn)頁(yè)面了,此跳轉(zhuǎn)的頁(yè)面就是action="output.jsp"了
chain.doFilter(request,response);
Filter中chain.doFilter(req,res)的理解
在寫(xiě)代碼中,每次看到Filter(過(guò)濾器)中chain.doFilter(req,res);都不懂為什么要加這一句,他的作用是什么;
代碼下面也有:
@Override
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{
ContentCachingRequestWrapperreq=newContentCachingRequestWrapper((HttpServletRequest)request);
ContentCachingResponseWrapperres=newContentCachingResponseWrapper((HttpServletResponse)response);
longstartTime=System.currentTimeMillis();
StringserverUrl=req.getServletPath();
//不懂這句什么意思
chain.doFilter(req,res);
longendTime=System.currentTimeMillis();
try{
intstatus=res.getStatus();
Stringcharset=res.getCharacterEncoding();
//響應(yīng)體
StringresponsePayload=getPayLoad(res.getContentAsByteArray(),res.getCharacterEncoding());
res.copyBodyToResponse();
if(writeLogService==null){
writeLogService=(WriteLogService)ServiceLocator.getInstance().getCtx().getBean("writeLogServiceImpl");
writeLogService.writeLog(startTime,endTime,serverUrl,postparams,status,responsePayload,charset);
}catch(Exceptione){
e.printStackTrace();
}
用法
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- T/ZBH 027-2023晶硅光伏組件用材料第4部分:玻璃標(biāo)準(zhǔn)尺寸
- T/YNIA 010-2022呼吸器用聚丙烯熔噴法非織造材料
- 2025年綜合素質(zhì)評(píng)價(jià)與個(gè)人發(fā)展能力測(cè)試的考試試題及答案
- 會(huì)展策劃師職業(yè)資格考試題及答案2025年
- 2025年外語(yǔ)翻譯專業(yè)考試題及答案
- 2025年數(shù)據(jù)分析師職業(yè)考試試卷及答案
- 2025年數(shù)字營(yíng)銷實(shí)務(wù)與策略考試試題及答案
- 2025年城市規(guī)劃與設(shè)計(jì)課程畢業(yè)考試試題及答案
- 2025年電氣工程及其自動(dòng)化考試試題及答案
- 指揮中心效果方案設(shè)計(jì)
- 2024年江蘇省無(wú)錫市中考?xì)v史真題(原卷版)
- 金礦合作協(xié)議書(shū)
- 山東科技大學(xué)投資經(jīng)濟(jì)學(xué)(專升本)期末復(fù)習(xí)題
- “1+X”證書(shū)制度試點(diǎn)職業(yè)技能等級(jí)證書(shū)全名錄
- 《守株待兔》“課本劇”背景PPT
- 流體包裹體及應(yīng)用PPT演示課件
- 防波堤課程設(shè)計(jì)
- 主動(dòng)脈夾層PPT課件
- 甲狀腺相關(guān)性眼病(2)
- (完整版)拉管施工工藝
- 工程部績(jī)效考核表94061
評(píng)論
0/150
提交評(píng)論