=== Tomcat 的編碼問題 ===
Tomcat 的 server.xml ( 位於 conf 目錄下)
【Get 請求部份】
找出
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改為
<Connector port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8"
connectionTimeout="20000"
redirectPort="8443" />
【Post 請求部份】
Tomcat 6.x 使用 SetCharacterEncodingFilter 這個 Filter
將 webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.class
複製到你的webapp資料夾中
yourwebapp/WEB-INF/classes/filters/SetCharacterEncodingFilter.class
並在web.xml設定filter
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<!--將全部的請求都mapping給filter-->
<url-pattern>/*</url-pattern>
</filter-mapping>
Tomcat 7.x以及5.5.36、6.0.36之後的版本
在Tomcat 7.x以及5.5.36、6.0.36之後的版本中,已經將SetCharacterEncodingFilter加入Tomcat核心中,因此不需要再自行複製class到自己的webapp資料夾中,在設定filter時直接指定class即可。
<filter>
<filter-name>Set_Character_Encoding</filter-name>
<!--差異處-->
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set_Character_Encoding</filter-name>
<!--將全部的請求都mapping給filter-->
<url-pattern>/*</url-pattern>
</filter-mapping>
Maven dependency 依照所使用的 Tomcat 版本而設定
<properties>
<tomcat-catalina.version>7.0.41</tomcat-catalina.version>
</properties>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>${tomcat-catalina.version}</version>
<scope>provided</scope>
</dependency>
=== Struts2 的編碼問題 ===
action 接收中文參數亂碼解決方法
首先確定 struts.xml 配置檔是否設定好
<constant name="struts.locale" value="zh_TW"/>
<!-- 定義系統預設編碼集 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
【針對 post 部分】
設置 編碼過濾器
package org.filter;
import java.io.IOException;
import java.net.URLDecoder;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CharsetEncodingFilter implements Filter {
@Override
public void doFilter(ServletRequest sRequest, ServletResponse sResponse,
FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) sRequest;
HttpServletResponse response = (HttpServletResponse) sResponse;
// 設置編碼集
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
filterChain.doFilter(request, response);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
接著將其加入到 Web.xml 中以便生效
<!-- 過濾所有的請求 編碼為 utf-8,需寫在 struts2 filter 之前 -->
<filter>
<filter-name>CharsetEncodingFilter</filter-name>
<filter-class>org.filter.CharsetEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
【針對 get 部分】
如 http:///xxx?name=威廉
由parameter 中取值之後,將其轉碼
String str = request.getParameter(“name”);
str = new String(str.getBytes(“ISO-8859-1”), “UTF-8”);
沒有留言 :
張貼留言