java 如何解码get url 接收到的参数throw BeanParam
java how to decode get url parameter received throw BeanParam
我收到了对此 Web 服务的 GET 响应
@GET
@Path("/nnnnnn")
public Response pfpfpfpf(@BeanParam NNNNNN n)
classNNNNN
有:
@QueryParam("parameter")
private String parameter;
为此 parameter
有一个获取和设置。
我使用 查询参数 发送了一个获取请求,它被自动绑定到我的选项 NNNNN,一切都很好。
但是,现在我在查询中发送日语字符串 url。我在发送前用 UTF-8 编码参数,我必须使用 UTF-8 解码它们。
但我的问题是 我应该在哪里 调用 URLDecoder?我试图在那个参数的 getter 中调用它,但它没有用,我一直有类似 C3%98%C2%B4%C3%98%C2
的东西而不是日文字符
适合我的解决方案是:
在 servlet 上,我应该这样做:
request.setCharacterEncoding("UTF-8");
然后在 html 页面上我必须添加:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
这是一个很好的问题,它可能会澄清许多关于系统之间如何处理(编码和解码)信息的疑问。
在我继续之前,我必须说对字符集、编码等有一个公平的理解。您可能需要阅读 this answer 以快速提醒。
这必须从 2 个角度来看 - 浏览器和服务器。
编码的浏览器视角
每个浏览器都会呈现 information/text,现在要呈现 information/text 它必须知道如何解释那些 bits/bytes 以便它可以正确呈现(阅读我的answer's 3rd bullet,相同的位如何在不同的编码方案中表示不同的字符)。
浏览器页面编码
- 每个浏览器都有一个与之关联的默认编码。 Check this on how to see the default encoding of browser.
- 如果您未在 HTML 页面上指定任何编码,则浏览器的默认编码将生效,并将按照这些编码规则呈现页面。所以,如果默认编码是 ASCII,而你使用的是日文或中文或来自 Unicode 补充平面的字符,那么你将看到垃圾值。
- 你可以告诉浏览器不要使用你的默认编码方案,而是使用这个来由网站呈现,使用
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
。
- 并且 这正是您 did/found 并且您很好,因为此
meta
标记基本上覆盖了浏览器的默认编码。
- 实现相同效果的另一种方法 是不使用此元标记,而只需更改浏览器的默认编码,您仍然可以。但不建议这样做,建议在 JSP 中使用
Content-Type
元标记。
尝试使用浏览器默认编码和 meta
标签,使用下面的简单 HTML。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
の, は, でした <br></br>
昨夜, 最高
</body>
</html>
编码的服务器视角
服务器还应该知道如何解释传入的数据流,这基本上意味着要使用哪种编码方案(服务器部分很棘手,因为有几种可能性)。阅读以下来自 here
When data that has been entered into HTML forms is submitted, the form
field names and values are encoded and sent to the server in an HTTP
request message using method GET or POST, or, historically, via email.
The encoding used by default is based on a very early version of the
general URI percent-encoding rules, with a number of modifications
such as newline normalization and replacing spaces with "+" instead of
"%20". The MIME type of data encoded this way is
application/x-www-form-urlencoded, and it is currently defined (still
in a very outdated manner) in the HTML and XForms specifications. In
addition, the CGI specification contains rules for how web servers
decode data of this type and make it available to applications.
这又分为两部分,即服务器应如何解码传入的请求流以及应如何对传出的响应流进行编码。
根据用例,有多种方法可以做到这一点,例如:
- HTTP请求和响应对象中有
setCharacterEncoding
、setContentType
等方法,可以用来设置编码。
- 这正是你在你的案例中所做的你已经告诉服务器使用 UTF-8 编码方案来解码请求数据,因为我期待高级 Unicode 补充平面字符。但这还不是全部,请阅读下面的更多内容。
- 使用
-Dfile.encoding=utf8
等 JVM 属性在服务器或 JVM 级别设置编码。阅读 this article 如何设置服务器编码。
在你的例子中,你从 URL 的查询字符串中获取日文字符,并且查询字符串是 HTTP 请求对象的一部分,因此使用 request.setCharacterEncoding("UTF-8");
你能够获得所需的编码结果.
但同样不适用于 URL 编码,这不同于请求编码(您的情况)。考虑下面的示例,在 sysout
中,即使使用 request.setCharacterEncoding("UTF-8");
后,您也将无法看到所需的编码效果,因为在这里您需要 URL 编码,因为 URL 将是某种东西像 http://localhost:7001/springapp/forms/executorTest/encodingTest/hellothere 昨夜, 最高
并且在这个 URL 中没有查询字符串。
@RequestMapping(value="/encodingTest/{quertStringValue}", method=RequestMethod.GET)
public ModelAndView encodingTest(@PathVariable("quertStringValue") String quertStringValue, ModelMap model, HttpServletRequest request) throws UnsupportedEncodingException {
System.out.println("############### quertStringValue " + quertStringValue);
request.setCharacterEncoding("UTF-8");
System.out.println("############### quertStringValue " + quertStringValue);
return new ModelAndView("ThreadInfo", "ThreadInfo", "@@@@@@@ This is my encoded output " + quertStringValue);
}
根据您使用的框架,您可能需要额外的配置来为请求或 URL 指定字符编码,以便您可以在请求尚未指定编码时应用自己的编码,或者在任何情况下都强制执行编码。这很有用,因为即使在 HTML 页面或表单中指定,当前浏览器通常也不会设置字符编码。
在Spring中,有org.springframework.web.filter.CharacterEncodingFilter
配置请求编码。阅读基于此事实的 this similar interesting question。
坚果shell
每个计算机程序,无论是应用程序服务器、Web 服务器、浏览器,IDE 等都只理解位,因此它需要知道如何解释这些位以从中获得预期的意义,因为这取决于编码使用时,相同的位可以表示不同的字符。 这就是 "Encoding" 发挥作用的地方,它通过提供一个唯一的标识符来表示一个字符,以便所有计算机程序、不同的 OS 等都知道解释它的正确方法。
我收到了对此 Web 服务的 GET 响应
@GET
@Path("/nnnnnn")
public Response pfpfpfpf(@BeanParam NNNNNN n)
classNNNNN
有:
@QueryParam("parameter")
private String parameter;
为此 parameter
有一个获取和设置。
我使用 查询参数 发送了一个获取请求,它被自动绑定到我的选项 NNNNN,一切都很好。
但是,现在我在查询中发送日语字符串 url。我在发送前用 UTF-8 编码参数,我必须使用 UTF-8 解码它们。
但我的问题是 我应该在哪里 调用 URLDecoder?我试图在那个参数的 getter 中调用它,但它没有用,我一直有类似 C3%98%C2%B4%C3%98%C2
的东西而不是日文字符
适合我的解决方案是:
在 servlet 上,我应该这样做:
request.setCharacterEncoding("UTF-8");
然后在 html 页面上我必须添加:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
这是一个很好的问题,它可能会澄清许多关于系统之间如何处理(编码和解码)信息的疑问。
在我继续之前,我必须说对字符集、编码等有一个公平的理解。您可能需要阅读 this answer 以快速提醒。
这必须从 2 个角度来看 - 浏览器和服务器。
编码的浏览器视角
每个浏览器都会呈现 information/text,现在要呈现 information/text 它必须知道如何解释那些 bits/bytes 以便它可以正确呈现(阅读我的answer's 3rd bullet,相同的位如何在不同的编码方案中表示不同的字符)。
浏览器页面编码
- 每个浏览器都有一个与之关联的默认编码。 Check this on how to see the default encoding of browser.
- 如果您未在 HTML 页面上指定任何编码,则浏览器的默认编码将生效,并将按照这些编码规则呈现页面。所以,如果默认编码是 ASCII,而你使用的是日文或中文或来自 Unicode 补充平面的字符,那么你将看到垃圾值。
- 你可以告诉浏览器不要使用你的默认编码方案,而是使用这个来由网站呈现,使用
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
。- 并且 这正是您 did/found 并且您很好,因为此
meta
标记基本上覆盖了浏览器的默认编码。 - 实现相同效果的另一种方法 是不使用此元标记,而只需更改浏览器的默认编码,您仍然可以。但不建议这样做,建议在 JSP 中使用
Content-Type
元标记。
- 并且 这正是您 did/found 并且您很好,因为此
尝试使用浏览器默认编码和 meta
标签,使用下面的简单 HTML。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
の, は, でした <br></br>
昨夜, 最高
</body>
</html>
编码的服务器视角
服务器还应该知道如何解释传入的数据流,这基本上意味着要使用哪种编码方案(服务器部分很棘手,因为有几种可能性)。阅读以下来自 here
When data that has been entered into HTML forms is submitted, the form field names and values are encoded and sent to the server in an HTTP request message using method GET or POST, or, historically, via email. The encoding used by default is based on a very early version of the general URI percent-encoding rules, with a number of modifications such as newline normalization and replacing spaces with "+" instead of "%20". The MIME type of data encoded this way is application/x-www-form-urlencoded, and it is currently defined (still in a very outdated manner) in the HTML and XForms specifications. In addition, the CGI specification contains rules for how web servers decode data of this type and make it available to applications.
这又分为两部分,即服务器应如何解码传入的请求流以及应如何对传出的响应流进行编码。
根据用例,有多种方法可以做到这一点,例如:
- HTTP请求和响应对象中有
setCharacterEncoding
、setContentType
等方法,可以用来设置编码。- 这正是你在你的案例中所做的你已经告诉服务器使用 UTF-8 编码方案来解码请求数据,因为我期待高级 Unicode 补充平面字符。但这还不是全部,请阅读下面的更多内容。
- 使用
-Dfile.encoding=utf8
等 JVM 属性在服务器或 JVM 级别设置编码。阅读 this article 如何设置服务器编码。
在你的例子中,你从 URL 的查询字符串中获取日文字符,并且查询字符串是 HTTP 请求对象的一部分,因此使用 request.setCharacterEncoding("UTF-8");
你能够获得所需的编码结果.
但同样不适用于 URL 编码,这不同于请求编码(您的情况)。考虑下面的示例,在 sysout
中,即使使用 request.setCharacterEncoding("UTF-8");
后,您也将无法看到所需的编码效果,因为在这里您需要 URL 编码,因为 URL 将是某种东西像 http://localhost:7001/springapp/forms/executorTest/encodingTest/hellothere 昨夜, 最高
并且在这个 URL 中没有查询字符串。
@RequestMapping(value="/encodingTest/{quertStringValue}", method=RequestMethod.GET)
public ModelAndView encodingTest(@PathVariable("quertStringValue") String quertStringValue, ModelMap model, HttpServletRequest request) throws UnsupportedEncodingException {
System.out.println("############### quertStringValue " + quertStringValue);
request.setCharacterEncoding("UTF-8");
System.out.println("############### quertStringValue " + quertStringValue);
return new ModelAndView("ThreadInfo", "ThreadInfo", "@@@@@@@ This is my encoded output " + quertStringValue);
}
根据您使用的框架,您可能需要额外的配置来为请求或 URL 指定字符编码,以便您可以在请求尚未指定编码时应用自己的编码,或者在任何情况下都强制执行编码。这很有用,因为即使在 HTML 页面或表单中指定,当前浏览器通常也不会设置字符编码。
在Spring中,有org.springframework.web.filter.CharacterEncodingFilter
配置请求编码。阅读基于此事实的 this similar interesting question。
坚果shell
每个计算机程序,无论是应用程序服务器、Web 服务器、浏览器,IDE 等都只理解位,因此它需要知道如何解释这些位以从中获得预期的意义,因为这取决于编码使用时,相同的位可以表示不同的字符。 这就是 "Encoding" 发挥作用的地方,它通过提供一个唯一的标识符来表示一个字符,以便所有计算机程序、不同的 OS 等都知道解释它的正确方法。