国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
Filter Servlet實(shí)例應(yīng)用
      Filter的意思是可以在Servlet實(shí)例進(jìn)行實(shí)質(zhì)性的處理之前,起到“過(guò)濾”的作用.

       一個(gè)Filter可以過(guò)濾一個(gè)或多個(gè)Servlet,F(xiàn)ilter必須實(shí)現(xiàn)javax.Servlet.Filter接口,并且必須定義以下三個(gè)方法:init(),destory(),doFilter().

       下面的例子演示記錄客戶端主機(jī)信息的Filter:

程序代碼:
/**
 *  該實(shí)例演示記錄客戶端主機(jī)信息的Filter
 *  www.javatx.cn
 */

package cn.javatx;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * @author ljfan
 *
 */

public class logFilter implements Filter {
private ServletContext context;

/* (non-Javadoc)
 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
 */

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
context = arg0.getServletContext();
}

/* (non-Javadoc)
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */

public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
context.log("[" + arg0.getRemoteHost() + "] request: " +((HttpServletRequest)arg0).getRequestURI());
arg2.doFilter(arg0, arg1);
context.log("[" + arg0.getRemoteHost() + "] done");
}

/* (non-Javadoc)
 * @see javax.servlet.Filter#destroy()
 */

public void destroy() {
// TODO Auto-generated method stub
context = null;
}

}


         當(dāng)前web應(yīng)用的web.xml配置為:

程序代碼:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<filter>
<filter-name>logFilter</filter-name>
<filter-class>cn.javatx.logFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>logFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>


最后用myeclipse插件自動(dòng)部署到tomcat路徑下,啟動(dòng)tomcat,在瀏覽器中輸入
http://localhost:8080/Filter/  (Filter為此web應(yīng)用名稱)

可以在tomcat  目錄的log目錄下的localhost.2006-01-02.log看到以下消息:

2006-1-2  16:25:51  org.apache.catalina.core.ApplicationContext  log
信息:  [127.0.0.1]  request:  /Filter/
2006-1-2  16:25:51  org.apache.catalina.core.ApplicationContext  log
信息:  [127.0.0.1]  done

這表明logFilter已經(jīng)被server調(diào)用成功.
當(dāng)然在實(shí)際應(yīng)用中filter還有其他用處,象登陸驗(yàn)證,編碼轉(zhuǎn)化等.

Java天下社區(qū)
http://www.javatx.cn
歡迎大家上來(lái)交流Java技術(shù)。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Servlet過(guò)濾器
Servlet中的過(guò)濾器Filter詳解
使用Filter對(duì)用戶權(quán)限和session的控制
ueditor1.4.3jsp utf-8版在struts2應(yīng)用中上傳圖片報(bào)"未找到上傳文件"解決方案
springMVC 攔截器如何做登錄檢查及頁(yè)面跳轉(zhuǎn)
提高EXT js的加載速度的方法_YOU'RE TOO PERFECT FOR ME......
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服