<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java學習

    java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

     

    javax.servlet.Filter運用

    2008年04月07日 星期一 10:25

    Servlets Filter 是Servlet 2.3 規范中新增加的,它是截取用戶從客戶端提交的請求,在還沒有到達需要訪問的資源時運行的一個類。它操縱來自客戶端的請求,在資源還沒有初發送到客戶端前截取響應,并處理這些還沒有發送到客戶端的響應。

    Filters 有許多的應用場合。Servlet 2.3 規范建議如下的地方可以應用Filter:

    authentication filters
    logging and auditing filters
    image conversion filters
    data compression filters
    encryption filters
    tokenizing filters
    filters that trigger resource access events
    XSL/T filters that transform XML content
    MIME-type chain filters

    例:

    package jasso.web.filter;

    import java.io.IOException;

    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;


    /**
    *
    * @author
    */
    public class SetCharacterEncodingFilter implements Filter {

    private String encoding;

    /**
    * Called by the web container to indicate to a filter that it is being
    * taken out of service.
    *
    * @todo Implement this javax.servlet.Filter method
    */
    public void destroy() {
    }

    /**
    * The <code>doFilter</code> method of the Filter is called by the
    * container each time a request/response pair is passed through the chain
    * due to a client request for a resource at the end of the chain.
    *
    * @param request
    *            ServletRequest
    * @param response
    *            ServletResponse
    * @param chain
    *            FilterChain
    * @throws IOException
    * @throws ServletException
    * @todo Implement this javax.servlet.Filter method
    */
    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
       request.setCharacterEncoding(encoding);
       chain.doFilter(request, response);
    }

    /**
    * @see javax.servlet.Filter#init(FilterConfig)
    */
    public void init(FilterConfig config) throws ServletException {
       encoding = config.getInitParameter("encoding");
    }


    }

    web.xml部分

    ......

    <filter>
       <filter-name>encodingFilter</filter-name>
       <filter-class>my.web.filter.SetCharacterEncodingFilter</filter-class>
         <init-param>
           <param-name>encoding</param-name>
           <param-value>Shift_JIS</param-value>
         </init-param>
    </filter>

    <filter-mapping>
       <filter-name>encodingFilter</filter-name>
       <servlet-name>action</servlet-name>
    </filter-mapping>
    <servlet>
       <servlet-name>action</servlet-name>
       <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
       <init-param>
         <param-name>config</param-name>
         <param-value>/WEB-INF/config/struts-config.xml,/WEB-INF/config/struts-config-contents.xml</param-value>
       </init-param>
       <init-param>
         <param-name>debug</param-name>
         <param-value>2</param-value>
       </init-param>
       <init-param>
         <param-name>detail</param-name>
         <param-value>2</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
    </servlet>

    ----------------------------------------------

    javax.servlet.filter的用法

    如何實現一個過濾器呢?
    1.所在的類實現Filter接口
    public interface Filter
    public void init(FilterConfig filterConfig)throws ServletException
    過濾器初始化是在容器啟動時自動初始化
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletExcepton
    在標準Servlet中的doGet或doPost 方法中,傳遞參數為:HttpServletRequest、HttpServletResponse
    過濾器銷毀
    public void destroy()

    package cn.mldn.lxh.filter
    import java.io.*;
    import javax.servlet.*;
    public class FirstFilter implements Filter
    {
           public void init(FilterConfig config)throws ServletException
           {
       System.out.println("過濾器初始化");
           }
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException
    {
    System.out.println(過濾doFilter);
    }
       public void destroy()
    {
    System.out.println(過濾器銷毀);
    }
    }

    web.xml文件配置
    <filter>
    <filter-name> first </filter-name>
    <filter-class> cn.mldn.lxh.filter.FirstFilter </filter-class>
    </filter>

    <filter-mapping>
    <filter-name> first </filter-name>

    與servlet中的url-pattern不一樣,此處表示對那個頁面進行過濾,如果全部都過濾,則"/*"
    <url-pattern> /* </url-pattern>
    </filter-mapping>

    如果過濾器要將內容傳遞到目的地,則需要FilterChain,將請求繼續向下轉發

    過濾器也會執行兩次:FilterChain這前執行一次,之后再執行一次

    1.過濾非法文字:
    package cn.mldn.lxh.filter;
    import java.io.*;
    import javax.servlet.*;
    public class CharFilter implements Filter
    {
    public void init(FilterConfig filterConfig)throws ServletException
    {
        //System.out.println("init");
    }
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException
    {
    String content=request.getParameter("content");
    //如果indexOf返回-1則表示沒有查到所要的內容
    if(content!=null)
    if(content.indexOf("AAA")==-1)
    {
    chain.doFilter(request,response);
    }
    esle
    {
    System.out.println("有非法文字");
    //如果需要的話,此處依然可以使用RequestDispatcher進行跳轉
    }
    }
    else
    {
    chain.doFilter(request,response);
    }
    }
    public void destroy()
    {
    //System.out.println("過濾器銷毀");
    }

    <filter>
    <filter-name> char </filter-name>
    <filter-class> cn.mldn.lxh.filter.CharFilter </filter-class>
    </filter>

    <filter-mapping>
    <filter-name> char </filter-name>
    <url-pattern> /* </url-pattern>
    </filter-mapping>

    2.對內容進行統一的編碼
    setCharacterEncoding("GB2312");

    package cn.mldn.lxh.filter;
    import java.io.*;
    import javax.servlet.*;
    public class EncodingFilter implements Filter
    {
    public void init(FilterConfig filterConfig)throws ServletException
    {
    //System.out.println("過濾器初始化");
    }
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException
    {
    try{
    request.setCharacterEncoding("GB2312");
    }
    catch(Exception e)
    {}
    chain.doFilter(request,response);
    }
    public void destroy()
    {
    //System.out.println("銷毀");
    }
    }

    <filter>
    <filter-name> encoding </filter-name>
    <filter-class> </filter-class>
    </filter>

    <filter-mapping>
    <filter-name> encoding </filter-name>
    <url-pattern> /* </url-pattern>
    </filter-mapping>

    3.登陸驗證
    package cn.mldn.lxh.filter;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class LoginFilter implements Filter
    {
    public void init(FilterCoinfig filterConfig)throws ServletException
    {
    //System.out.println("過濾器初始化");
    }
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException
    {
    //Session屬于http范疇,把以ServletRequest對象需要先轉換成HttpServletRequest對象
    HttpServletRequest req=(HttpServletRequest)request;
    HttpSession session =req.getSession();
    //如果session不為空,則可以瀏覽其它頁面
    if(session.getAttribute("uname")!=null)
    {
    chain.doFilter(request,response);
    }
    else
    {
    //通過requestDispatcher跳轉到登陸面
    request.getRequestDispatcher("login.jsp").forward(request,response);
    }
    }
    public void destroy()
    {
    //System,out.println("銷毀");
    }
    }

    <filter>
    <filter-name> login </filter-name>
    <filter-class> cn.mldn.lxh.filter.LoginFilter </filter-class>
       <init-param>
       <param-name> 參數名稱 </param-name>
       <param-value> 參數值 </param-value>
        </init-param>
    </filter>
    <filter-mapping>
    <filter-name> login </filter-name>
    <url-pattern> /* </url-pattern>
    </filter-mapping>

    讀取web.xml中的參數的方法:

    public void init(FilterConfig arg0) throws ServletException {
    String 參數值= arg0.getInitParameter("參數名稱");
    }

    posted on 2008-08-20 16:53 找個美女做老婆 閱讀(4990) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    公告

    本blog已經搬到新家了, 新家:www.javaly.cn
     http://www.javaly.cn

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲av无码专区在线观看亚| 亚洲视频免费播放| AV激情亚洲男人的天堂国语| 无码av免费毛片一区二区| 亚洲精品视频久久| 在线看无码的免费网站| 亚洲白嫩在线观看| 希望影院高清免费观看视频| 亚洲精品国产手机| 成人免费毛片内射美女-百度| 狠狠色伊人亚洲综合网站色| 在线免费观看韩国a视频| 免费无码又爽又黄又刺激网站| 亚洲精品无码久久久| 中文字幕高清免费不卡视频| 久久久久亚洲精品美女| 2021在线永久免费视频| 亚洲毛片无码专区亚洲乱| 免费A级毛片无码无遮挡内射| 日韩亚洲人成在线| 亚洲综合久久夜AV | 免费福利在线视频| 亚洲H在线播放在线观看H| 国产成人免费ā片在线观看| 一区二区三区免费精品视频 | 久久久久亚洲精品天堂久久久久久 | 亚洲永久无码3D动漫一区| 久久国产精品免费视频| 亚洲一卡二卡三卡| www.亚洲一区| 伊人久久免费视频| 亚洲AV色无码乱码在线观看| 亚洲日韩国产精品第一页一区| 8090在线观看免费观看| 亚洲老熟女五十路老熟女bbw| 国产亚洲人成网站在线观看| 国产精品免费观看| caoporm超免费公开视频| 亚洲毛片在线免费观看| 亚洲毛片网址在线观看中文字幕 | 亚洲视频一区在线观看|