查询字符串中的字符编码,希伯来语 [英] Character encoding in query string, hebrew

查看:138
本文介绍了查询字符串中的字符编码,希伯来语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在希伯来语中发送一个带有查询字符串参数的GET请求。
当控制器获取请求时,参数是乱码。
我添加了org.springframework.web.filter.CharacterEncodingFilter但它没有改变一个事情。

I am trying to send a GET request with query string parameter in hebrew. When the controller gets the request, the parameter is in gibberish. i've added "org.springframework.web.filter.CharacterEncodingFilter" but it didn't change a thing.

请指教如何解决它。

更新:这是请求。

GET /myapp/specialties?query=%D7%92%D7%99%D7%A0%D7%A0%D7%A0%D7%A0 HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: *
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like           Gecko) Chrome/33.0.1750.117 Safari/537.36
Content-Type: application/json;charsert=utf-8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,he;q=0.6
Query String Parametersview sourceview URL encoded
query:גיננננ
Response Headersview source
Content-Type:application/json;charset=UTF-8
Date:Mon, 03 Mar 2014 20:45:17 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>med.rec</display-name>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-config.xml</param-value>
    </context-param>

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

-Roy

推荐答案

结果是 HttpServletRequest#setCharacterEncoding(String) CharacterEncodingFilter 使用


覆盖此
请求正文中使用的字符编码的名称
。在读取请求
参数或使用getReader()读取输入之前,必须调用此方法。否则,它没有
的效果。

Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). Otherwise, it has no effect.

这对你没有好处,因为你没有从正文,而是来自查询字符串。

Which is no good for you since you aren't getting the parameters from the body, but rather from the query string.

但是,如果你使用的是Tomcat,你就是运气。 Tomcat有一个特殊的 Connector 属性,当设置时(默认情况下未设置),将为查询字符串使用相同的字符编码。

If you are using Tomcat, however, you are in luck. Tomcat has a special Connector attribute which, when set (it's unset by default), will use that same character encoding for the query string.

该属性为 useBodyEncodingForURI 。如果你打开你的Tomcat servet.xml 文件,你会发现一个元素(没有属性)

That attribute is useBodyEncodingForURI. If you open up your Tomcat servet.xml file, you will find an element like (without the attribute)

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
    redirectPort="8443" useBodyEncodingForURI="true">
</Connector>

添加属性,它将按预期工作。请确保您在相应的连接器(在这种情况下为HTTP)中设置。

Add the attribute and it will work as intended. Make sure you are setting it for the appropriate Connector, HTTP in this case.

其他Servlet容器可能有一些类似配置。

Other Servlet containers probably have some similar configuration.

这篇关于查询字符串中的字符编码,希伯来语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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