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

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

徹底解決Tomcat 5.0.19中文亂碼

2019-11-17 06:12:45
字體:
來源:轉載
供稿:網友

  一、Include的頁面亂碼

  現象:include進來的頁面出現亂碼,其它頁面正常。

  原因:Tomcat在缺省情況下使用ISO-8859-1編碼,但是在include時有時Tomcat不能正確根據外層.jsp文件的編碼解析include進來的文件,造成include進來的文件中的中文亂碼。


  解決:這兒可以有很多解決辦法,但是對于我們的中文環境,從根本上的解決辦法是將Tomcat 5.0.19的核心缺省編碼從ISO-8859-1修改為GBK 。可以在下面地址下載修改過的jar文件,

  jasper-compiler.jar,jasper-runtime.jar位于/common/lib下,其它位于/server/lib下,將新的.jar文件替代原.jar即可。
  
  二、提交的數據亂碼

  現象:通過表單提交的數據出現亂碼。

  原因:原因未明。可能是Tomcat在接收到請求后,并沒有能夠根據request中的信息提前正確的編碼方式。

  解決:可以添加一個設置字符集的Filter。

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);
}

}
  配置web.xml



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白沙| 汝城县| 军事| 贵南县| 葵青区| 舒兰市| 南溪县| 搜索| 肇源县| 长岛县| 晋中市| 嘉禾县| 高安市| 博兴县| 绥芬河市| 曲沃县| 清徐县| 兴业县| 万安县| 大理市| 靖远县| 大港区| 扎兰屯市| 普格县| 峨山| 弥勒县| 昭通市| 会理县| 博兴县| 宁都县| 馆陶县| 缙云县| 新绛县| 沾益县| 遵化市| 乌兰察布市| 吉首市| 武功县| 邛崃市| 西安市| 普定县|