---
title: "Resin转Tomcat过程中的乱码"
author: "caowei3047"
date: "2012-08-21T17:57:31Z"
tags: []
categories: ["编程语言"]
slug: "resin-tomcat"
draft: false
lastmod: "2012-08-21T17:58:26Z"
id: 1648078
url: "https://caowei3047.iteye.com/blog/1648078"
---

最经在把一个工程的服务器从resin替换成tomcat。

过程中遇到了一些问题。主要是web.xml的配置问题。还有一些hibernate的配置检查的更加严格了。

这些都是不需要修改代码的。修改代码的只发现一种情况：

通过PrintWriter将中文输出（以txt、excel文件或页面的形式无关）时，出现了乱码。

用一段代码进行说明吧。

```
PrintWriter out=response.getWriter();
				response.setContentType("text/html; charset=gbk");
				response.setCharacterEncoding("GBK");
				out.write("阿飞");
				out.write(new String(("小李飞刀").getBytes(),"ISO_8859_1"));
				out.close();
```

 输出的结果是：

```
??小李飞刀
```

 

基于查阅资料及自身实践，得出这样的结论：

在直接使用PrintWrite输出时，resin会直接使用其内容作为输出（

```
out.write("阿飞");//在resin下可以正常显示
```

）。

而tomcat则会认为输入给PrintWrite的都是 8859\_1编码格式的，会再做一次 8859\_1--&gt;gb2312的转换。页面会出现“？”。