jsp字元過濾器的設定__js

來源:互聯網
上載者:User

 

本文介紹過濾器來設定字元編碼的問題,通過編寫一個servlet和配置web.xml來即可實現
 這樣不必在每個jsp頁面社自豪字元編碼了,值需要在web.xml配置需要的編碼即可。

web.xml配置內容如下:

 

<!-- 字元過濾器 -->
    <filter>
        <filter-name>encodeFilter</filter-name>
        <filter-class>
            test.servlet.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
      </filter>

    <filter-mapping>
        <filter-name>encodeFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>encodeFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

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

Filter內容如下:

 

package test.servlet;


import javax.servlet.*;
import java.io.IOException;

 

public class CharacterEncodingFilter implements Filter  {

 
 protected String encoding = "UTF-8";

    protected FilterConfig filterConfig = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
     }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // Conditionally select and set the character encoding to be used
        if (request.getCharacterEncoding() == null) {
            String encoding = selectEncoding(request);
            if (encoding != null) {
                request.setCharacterEncoding(encoding);
            }
        }
        // Pass control on to the next filter
        chain.doFilter(request, response);
    }

    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
    }

    public void destroy() {
        this.encoding = null;
        this.filterConfig = null;
    }

}

 

 

轉: 字元版面設定方式:

 

1. pageEncoding:<%@ page pageEncoding="UTF-8"%>

jsp頁面編碼: jsp檔案本身的編碼 

 

2. contentType: <%@ page contentType="text/html; charset=UTF-8"%>

web頁面顯示編碼:jsp的輸出資料流在瀏覽器中顯示的編碼 

 

3. html頁面charset:<META http-equiv="Content-Type" content="text/html; charset=UTF-8">

web頁面輸入編碼: 輸入框輸入的字型編碼   

 

4. setCharacterEncoding:request.setCharacterEncoding(),response.setCharacterEncoding()

web伺服器輸入的請求流: web Server相應瀏覽器的請求資料  

 

5 .setContentType:response.setContentType()

web伺服器輸出的響應流: web Server相應瀏覽器的輸出資料 

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/XinVSYuan/archive/2009/02/05/3864853.aspx

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.