posts - 119, comments - 62, trackbacks - 0, articles - 0

          tomcat亂碼解決辦法

          Posted on 2006-11-10 19:26 Kevin Meng 閱讀(527) 評論(0)  編輯  收藏

          一、新建一個Filter,代碼如下:
          package com.suzhou.util;

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

          public class SetCharacterEncodingFilter implements Filter {
          ?protected String encoding = null;// ///要制定的編碼,在web.xml中配置

          ?protected FilterConfig filterConfig = null;

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

          ?public void doFilter(ServletRequest request, ServletResponse response,
          ???FilterChain chain) throws IOException, ServletException {

          ??if (request.getCharacterEncoding() == null) {
          ???String encoding = getEncoding();// //得到指定的編碼名字
          ???if (encoding != null)
          ????request.setCharacterEncoding(encoding);// //設置request的編碼
          ??}
          ??chain.doFilter(request, response);// /有機會執行下一個filter
          ?}

          ?public void init(FilterConfig filterConfig) throws ServletException {

          ??this.filterConfig = filterConfig;
          ??this.encoding = filterConfig.getInitParameter("encoding");// /得到在web.xml中配置的編碼
          ?}

          ?protected String getEncoding() {

          ??return (this.encoding);// /得到指定的編碼

          ?}

          }
          二、修改web.xml,添加:
          <filter>
          ??<filter-name>SetCharacterEncoding</filter-name>
          ??<filter-class>com.suzhou.util.SetCharacterEncodingFilter</filter-class>
          ??<init-param>
          ???<param-name>encoding</param-name>
          ???<param-value>GBK</param-value>
          ??</init-param>
          ?</filter>
          ?<filter-mapping>
          ??<filter-name>SetCharacterEncoding</filter-name>
          ??<url-pattern>/*</url-pattern>
          ?</filter-mapping>
          三、進入http://localhost:port/admin,選擇Tomcat server/Service/Connector(port),把URI Encoding改成UTF-8,Use Body Encoding For URI Query Parameters:設置為true,點保存然后重新啟動Tomcat即可。
          記住,每個jsp頁面的編碼都為:
          <%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>

          這樣子不但能徹底解決中文參數問題(如http://menglikn:4000/suzhou/getMap.do?ditu=市區圖),而且還能解決中文URL問題(如http://menglikun:4000/suzhou/空白頁.html)


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


          網站導航:
           
          主站蜘蛛池模板: 漠河县| 金坛市| 康定县| 福建省| 陇南市| 隆德县| 油尖旺区| 兰坪| 乐业县| 两当县| 百色市| 绥化市| 本溪| 中牟县| 曲阳县| 白玉县| 西乌珠穆沁旗| 措美县| 通河县| 旬邑县| 五常市| 炎陵县| 那曲县| 崇仁县| 平远县| 马关县| 桃园市| 库伦旗| 庆元县| 石首市| 遵义市| 丽江市| 改则县| 广汉市| 德安县| 察雅县| 定南县| 淮安市| 诸暨市| 繁峙县| 呼玛县|