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

          tomcat亂碼解決辦法

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

          一、新建一個(gè)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);// /有機(jī)會(huì)執(zhí)行下一個(gè)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>
          三、進(jìn)入http://localhost:port/admin,選擇Tomcat server/Service/Connector(port),把URI Encoding改成UTF-8,Use Body Encoding For URI Query Parameters:設(shè)置為true,點(diǎn)保存然后重新啟動(dòng)Tomcat即可。
          記住,每個(gè)jsp頁(yè)面的編碼都為:
          <%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>

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


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 克山县| 陵水| 麟游县| 法库县| 大厂| 涟水县| 榆树市| 海南省| 伊吾县| 法库县| 桑植县| 镇原县| 涞源县| 连云港市| 宣恩县| 盐池县| 定边县| 海原县| 蒲城县| 海口市| 泽库县| 临桂县| 荆门市| 四子王旗| 黄平县| 普宁市| 梅河口市| 汪清县| 澄迈县| 通化市| 河池市| 大洼县| 广水市| 静乐县| 安岳县| 苗栗县| 温州市| 永定县| 隆安县| 玛多县| 阿图什市|