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)航:
           
          主站蜘蛛池模板: 弥渡县| 江都市| 菏泽市| 宁河县| 和顺县| 荥经县| 东光县| 师宗县| 昌邑市| 双流县| 和田市| 大田县| 邵东县| 台南县| 股票| 伊春市| 鄂温| 江孜县| 益阳市| 阳谷县| 定南县| 额济纳旗| 南皮县| 剑川县| 宝山区| 清徐县| 黔江区| 偃师市| 广西| 虞城县| 永靖县| 扶余县| 石嘴山市| 江山市| 洛浦县| 绥滨县| 平阳县| 安岳县| 轮台县| 开原市| 图们市|