Servlet response.sendRedirect编码问题 [英] Servlet response.sendRedirect encoding problems

查看:167
本文介绍了Servlet response.sendRedirect编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用一个朴素的方式重定向我的用户使用GET:

  response.sendRedirect(/ path / index .jsp?type =+ e.getType()
+& message =+ e.getMessage());

这是工作正常,直到我不得不发送消息,作为实际文本显示给用户。问题是如果消息中有非ASCII字符。我的.jsp文件以UTF-8编码:

 <%@ page language =javacontentType =text / html; charset = UTF-8
pageEncoding =UTF-8%>

因此,'message'中的所有非ASCII字符都会出现乱码。我不想将我的JVM默认编码设置为UTF-8,那么我该如何解决呢?我尝试使用

  response.setCharacterEncoding(UTF-8); 

重定向之前的Servlet,但它不工作。当我尝试执行:

  out.print(request.getCharacterEncoding()); 

在我的.jsp文件上打印'null'。

sendRedirect()方法不会为您编码查询字符串。 你必须自己做。

  response.sendRedirect(/ path / index.jsp?type =+ URLEncoder。 encoding(e.getType(),UTF-8)
+& message =+ URLEncoder.encode(e.getMessage(),UTF-8));

您可能想将模板重构为实用方法,并接受 Map



请注意,我假设服务器配置为使用UTF-8解码GET请求URI。你没有告诉你使用哪个属性,但是在例如Tomcat的情况下,将 URIEncoding =UTF-8属性添加到< Context> 元素。



另请参阅:








无关 language =java是默认已经,只是忽略它。当使用 pageEncoding =UTF-8时使用JSP时, contentType =text / html; charset = UTF-8 / code>,只是忽略它。您真正需要的是<%@ page pageEncoding =UTF-8%> 。注意,这和 response.setCharacterEncoding(UTF-8)实际上是一样的,所以解释为什么它没有效果。 request.getCharacterEncoding()仅涉及POST请求正文,而不是GET请求URI,因此在GET请求的情况下是不相关的。


So I'm redirecting my user using GET in a naive way:

response.sendRedirect("/path/index.jsp?type="+ e.getType() 
   +"&message="+ e.getMessage());

And this was working fine until I had to send messages, as actual text to be shown to users. The problem is if the message has non-ASCII characters in it. My .jsp files are encoded in UTF-8:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

So all non-ASCII characters in 'message' gets garbled. I don't want to set my JVM default encoding to UTF-8, so how do I solve this? I tried to use

response.setCharacterEncoding("UTF-8");

on the Servlet before redirecting, but it doesn't work. when I try to execute:

out.print(request.getCharacterEncoding());

on my .jsp file it prints 'null'.

解决方案

The sendRedirect() method doesn't encode the query string for you. You've to do it yourself.

response.sendRedirect("/path/index.jsp?type=" + URLEncoder.encode(e.getType(), "UTF-8")
    + "&message=" + URLEncoder.encode(e.getMessage(), "UTF-8"));

You might want to refactor the boilerplate to an utility method taking a Map or so.

Note that I assume that the server is configured to decode the GET request URI using UTF-8 as well. You didn't tell which one you're using, but in case of for example Tomcat it's a matter of adding URIEncoding="UTF-8" attribute to the <Context> element.

See also:


Unrelated to the concrete problem, the language="java" is the default already, just omit it. The contentType="text/html; charset=UTF-8" is also the default already when using JSP with pageEncoding="UTF-8", just omit it. All you really need is <%@ page pageEncoding="UTF-8"%>. Note that this does effectively the same as response.setCharacterEncoding("UTF-8"), so that explains why it didn't have effect. The request.getCharacterEncoding() only concerns the POST request body, not the GET request URI, so it is irrelevant in case of GET requests.

这篇关于Servlet response.sendRedirect编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆