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

          tomcat亂碼解決辦法

          Posted on 2006-11-10 19:26 Kevin Meng 閱讀(528) 評論(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)


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


          網站導航:
           
          主站蜘蛛池模板: 江阴市| 屏山县| 石柱| 绥阳县| 青州市| 通州区| 岐山县| 蓬莱市| 潼关县| 财经| 迁安市| 祁门县| 舟山市| 浦东新区| 樟树市| 肇东市| 松阳县| 通河县| 中西区| 玛曲县| 金阳县| 莒南县| 石城县| 渭源县| 竹溪县| 卢龙县| 湖南省| 台北市| 小金县| 武清区| 清涧县| 桦甸市| 广东省| 聂拉木县| 嘉兴市| 弥渡县| 民乐县| 桦甸市| 丰顺县| 资源县| 宣汉县|