java request.getparameter为何能取值,为什么在java中使用request.getParameter()时字符被破坏?...

I have such a link in JSP page with encoding big5

http://hello/world?name=婀ㄉ

And when I input it in browser's URL bar, it will be changed to something like

http://hello/world?name=%23%24%23

And when we want to get this parameter in jsp page, all the characters are corrupted.

And we have set this:

request.setCharacterEncoding("UTF-8"), so all the requests will be converted to UTF8.

But why in this case, it doesn't work ?

Thanks in advance!.

解决方案

When you enter the URL in browser's address bar, browser may convert the character encoding before URL-encoding. However, this behavior is not well defined, see my question,

We mostly get UTF-8 and Latin-1 on newer browsers but we get all kinds of encodings (including Big5) in old ones. So it's best to avoid non-ASCII characters in URL entered by user directly.

If the URL is embedded in JSP, you can force it into UTF-8 by generating it like this,

String link = "http://hello/world?name=" + URLEncoder.encode(name, "UTF-8");

On Tomcat, the encoding needs to be specified on Connector like this,

You also need to use request.setCharacterEncoding("UTF-8") for body encoding but it's not safe to set this in servlet because this only works when the parameter is not processed but other filter or valve may trigger the processing. So you should do it in a filter. Tomcat comes with such a filter in the source distribution.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值