---
title: "jetty处理URL传中文出现乱码的现象"
author: "evan0625"
date: "2011-07-02T17:24:12Z"
tags: []
categories: ["Web前端"]
slug: "jetty-url"
draft: false
lastmod: "2011-07-02T18:00:16Z"
id: 1113091
url: "https://evan0625.iteye.com/blog/1113091"
---

jetty竟然只支持GB2312,连GBK也不行!?!我测试好几次,最终发现只支持这一种编码.不知道别人找到支持其他编码的办法,我使用的jetty版本为jetty-6.1.22.

      解决办法:在JVM参数中添加相应配置即可,如下所示:

  

```
-Dorg.mortbay.util.URI.charset=gb2312
```

 

      官方的说明:

      [http://docs.codehaus.org/display/JETTY/International+Characters+and+Character+Encodings](http://docs.codehaus.org/display/JETTY/International+Characters+and+Character+Encodings),

不过我没有看,呵呵.

      当然还可以这样做:

      把中文通过URLEncoder转一下,主要代码为:

     

```
System.out.println(URLEncoder.encode("一二三","UTF-8"));
System.out.println(URLDecoder.decode("%E4%B8%80%E4%BA%8C%E4%B8%89","UTF-8"));
```

 

前端url传入的参数是通过URLEncoder.encode转换过后的文字.

servlet接收层只需要通过以下代码设置即可:

   

```
request.setCharacterEncoding("UTF-8");
```

 有时候不设置也可以,看你项目的整体编码以及人品了,哈哈.