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);// //設(shè)置request的編碼
          ??}
          ??chain.doFilter(request, response);// /有機會執(zhí)行下一個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:設(shè)置為true,點保存然后重新啟動Tomcat即可。
          記住,每個jsp頁面的編碼都為:
          <%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>

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


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 开原市| 会东县| 蛟河市| 界首市| 故城县| 唐河县| 泸水县| 呼玛县| 商丘市| 东乡县| 阿拉善左旗| 江川县| 留坝县| 确山县| 青阳县| 曲阳县| 毕节市| 建瓯市| 西乌| 郯城县| 元朗区| 潢川县| 新田县| 南丰县| 万盛区| 耿马| 漳浦县| 汝阳县| 两当县| 喀喇沁旗| 蒙自县| 黄龙县| 林周县| 江达县| 伊宁市| 盐津县| 雅江县| 沅江市| 穆棱市| 扬州市| 托里县|