国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

tomcat 5.09 中文問題解決全攻略

2019-11-18 16:38:55
字體:
來源:轉載
供稿:網友

  我開發基于 eclipse3.2+j2ee5.0 +tomcat5.09+MySQL5.0

一、表單POST的數據的中文亂碼解決

       這類型的數據中文可以通過filters來實時過濾。filters代碼如下:

package filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;

public class SetCharacterEncodingFilter implements Filter ...{
 PRotected String encoding = null;
 protected FilterConfig filterConfig = null;
 protected boolean ignore = true;

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

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

 // Conditionally select and set the character encoding to be used
 if (ignore || (request.getCharacterEncoding() == null)) ...{
  String encoding = selectEncoding(request);
  if (encoding != null)
   request.setCharacterEncoding(encoding);
 }

 // Pass control on to the next filter
 chain.doFilter(request, response);

}

public void init(FilterConfig filterConfig) throws ServletException ...{

 this.filterConfig = filterConfig;
 this.encoding = filterConfig.getInitParameter("encoding");
 String value = filterConfig.getInitParameter("ignore");
 if (value == null)
  this.ignore = true;
 else if (value.equalsIgnoreCase("true"))
  this.ignore = true;
 else if (value.equalsIgnoreCase("yes"))
  this.ignore = true;
 else
  this.ignore = false;

}

protected String selectEncoding(ServletRequest request) ...{
 return (this.encoding);
}

}
 

  filters配置(配置web.xml):

 

<filter>
 <filter-name>Set Character Encoding</filter-name>
 <filter-class>filters.SetCharacterEncodingFilter</filter-class>
 <init-param>
  <param-name>encoding</param-name>
  <param-value>GBK</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>Set Character Encoding</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>


 

二、將中文數據存入數據庫亂碼問題

  以mysql為例,改寫連接字符串即可:

jdbc:mysql://localhost:3306/workshopdb? useUnicode=true&characterEncoding=GBK
三、 通過url傳遞參數和識別中文文件名問題

  問題表現:1、通過url傳遞參數,例如:  

http://localhost:81/crjy/admin/articlelist.jsp?levelId=64&levelName=學生黨建
 
通過request.getParameter("levleName")得到的值為亂瑪。
      2、識別中文文件名,例如:

<img src="./pic/四川地圖.jpg">  圖片不能顯示。
解決之道:

  1、如果只想解決第一個問題那很簡單,兩句代碼即可:

String role=request.getParameter("chara");
role=new String(role.getBytes("ISO-8859-1"),"GB2312");
out.println(role);
  因為tomcat服務器默認用 ISO-8859-1 字符集的。但是這樣只能解決第一個問題不能解決中文文件名問題

  2、兩個問題一起解決,修改server.xml,找到下列語句添加URIEncoding="GB18030",這樣兩個問題就一起解決了(不需要role=new String(role.getBytes("ISO-8859-1"),"GB2312");轉化,得到的參數即為正常的中文) 

    <Connector acceptCount="100" connectionTimeout="20000" debug="0" disableUploadTimeout="true" enableLookups="false" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="81"  redirectPort="8443"/>
 另外許多文章介紹可以添URIEncoding="UTF-8",這樣是可以解決中文文件名問題,但是通過String role=request.getParameter("chara");得到url傳遞的參數時,得到的是UTF-8編碼的,需要轉為GB2312比較麻煩。

  以上是本人在使用中的總結,希望大家提供寶貴意見。
http://blog.csdn.net/lijiuu/archive/2007/02/25/1514354.aspx


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 秦安县| 海晏县| 萝北县| 什邡市| 定边县| 台南县| 确山县| 雅江县| 呼伦贝尔市| 会昌县| 班玛县| 安新县| 哈密市| 延边| 瓦房店市| 杭锦旗| 沙雅县| 泸定县| 古交市| 平遥县| 百色市| 子长县| 周宁县| 彰化县| 沅陵县| 巴里| 峨边| 红桥区| 利津县| 政和县| 保山市| 灵石县| 永新县| 康平县| 稷山县| 阳江市| 丰台区| 邛崃市| 遂宁市| 姜堰市| 石阡县|