標籤:des blog http java get strong
參考Tomcat伺服器目錄webapps的examples樣本
簡單配置步驟:
1.在項目web.xml檔案添加過濾器標記<filter>和<filter-mapping>;
2.實現過濾器代碼;
3.對Tomcat伺服器conf目錄裡的Server.xml檔案配置URIEncoding;
4.前台版面設定contentType的charset值與web.xml裡設定的值一致。
詳細配置步驟:
1.配置web.xm檔案,在web-app標籤內添加過濾器的配置<filter>和<filter-mapping>
[html] view plaincopy
<!--中文亂碼處理過濾-->
- <filter>
- <filter-name>EncodingFilter</filter-name><!--過濾器名稱,自己起 -->
- <filter-class>com.filters.SetCharacterEncodingFilter</filter-class><!--過濾器類名稱,包括包名一致、寫全 -->
- <init-param><!--初始化參數,要指定的字元集編碼 -->
- <param-name>encoding</param-name><!--參數名稱 -->
- <param-value>GBK</param-value><!--參數值 -->
- </init-param>
- <init-param><!--初始化參數,指定是否忽略大小寫 -->
- <param-name>ignore</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>EncodingFilter</filter-name>
- <url-pattern>/*</url-pattern> <!--“/*”表示項目下所有資源 -->
- </filter-mapping>
- <!--中文亂碼處理過濾 -->
2.實現過濾器代碼,建立一個SetCharacterEncodingFilter類
[java] view plaincopy
- package com.filters;
- 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;
- /**
- * 自訂字元處理過濾器
- * 參考自Tomcat目錄webapps\examples\WEB-INF\classes\filters
- * @author coderlu
- * @since 2012-12-10
- */
- public class SetCharacterEncodingFilter implements Filter {
- protected FilterConfig filterConfig = null; //初始化配置
- protected String encoding = null; //接收字元編碼
- protected boolean ignore = true; //是否忽略大小寫
- /* 解構函式
- * @see javax.servlet.Filter#destroy()
- */
- @Override
- public void destroy() {
- this.encoding = null;
- this.filterConfig = null;
- }
- /* 執行過濾操作
- * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
- */
- @Override
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- if (ignore || (request.getCharacterEncoding() == null)) {
- String encoding = selectEncoding(request); //如果為空白先從web.xml中得到
- if (encoding != null){
- request.setCharacterEncoding(encoding); //設定字元集編碼
- }
- }
- chain.doFilter(request, response);
- }
- /* 初始化過濾器
- * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
- */
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- this.filterConfig = filterConfig;
- this.encoding = filterConfig.getInitParameter("encoding"); //從web.xml檔案中讀取encoding的值
- String value = filterConfig.getInitParameter("ignore"); //從web.xml檔案中讀取ignore的值
- //以下三種情況均為忽略大小寫
- if (value == null)
- this.ignore = true;
- else if (value.equalsIgnoreCase("true"))
- this.ignore = true;
- else if (value.equalsIgnoreCase("yes"))
- this.ignore = true;
- else
- this.ignore = false;
- }
- /**
- * 得到字元編碼
- * @param request
- * @return
- */
- protected String selectEncoding(ServletRequest request) {
- return (this.encoding);
- }
- }
3.修改Tomcat伺服器conf\Server.xml檔案,加入一個URIEncoding="GBK"
[html] view plaincopy
- <!--URIEncoding 設定管理員反應編碼集-->
- <Connector port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443"
- URIEncoding="GBK"/>
4.設定前台頁面字元編碼集
[html] view plaincopy
- <%@ page contentType="text/html;charset=GBK" %>
//此處的charset的值要和web.xml裡的 <param-value></param-value>值一樣
=================================完=====================================
參考自:
Tomcat伺服器目錄webapps的examples樣本
jsp中文亂碼的Filter過濾器配置:http://www.cnblogs.com/zhuboxingzbx/articles/1208118.html
JSP中文亂碼,用過濾器方式解決:http://www.zhouwenze.com/archives/20110729-411.html
JSP中文亂碼過濾器執行個體:http://blog.csdn.net/wkupaochuan/article/details/7461208
http://blog.csdn.net/lutinghuan/article/details/8277422
========================================================================