java how to decode get url parameter received throw BeanParam

后端 未结 2 1391
清歌不尽
清歌不尽 2021-01-11 11:47

I receive a GET response to this web service

@GET
@Path(\"/nnnnnn\")
public Response pfpfpfpf(@BeanParam NNNNNN n)

The cla

相关标签:
2条回答
  • 2021-01-11 11:57

    The solution that works for me is :

    on the servlet, i should do this:

    request.setCharacterEncoding("UTF-8");
    

    and then on the html page i had to add this:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    0 讨论(0)
  • 2021-01-11 12:15

    This is a good question which has potential clear many doubts about how information is processed (encoded and decoded) between systems.

    Before I proceed I must say have a fair understanding on Charset, Encoding etc. You may want to read this answer for a quick heads up.

    This has to looked from 2 perspectives - browser and server.

    Browser perspective of Encoding

    Each browser will render the information/text, now to render the information/text it has to know how to interpret those bits/bytes so that it can render correctly (read my answer's 3rd bullet that how same bits can represent different characters in different encoding scheme).

    Browser page encoding

    • Each browser will have a default encoding associated with it. Check this on how to see the default encoding of browser.
    • If you do not specify any encoding on your HTML page then default encoding of browser will take effect and will render the page as per those encoding rules. so, if default encoding is ASCII and you are using Japanese or Chinese or characters from Unicode supplementary plane then you will see garbage value.
    • You can tell browser that do not use your default encoding scheme but use this one to render by website, using <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">.
      • And this exactly what you did/found and you were fine because this meta tag essentially overrode the default encoding of browser.
      • Another way to achieve same effect is do not use this meta tag but just change the browser's default encoding and still you will be fine. But this is not recommended and using Content-Type meta tag in your JSP is recommended.

    Try playing around with browser default encoding and meta tag using below simple HTML.

    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <body>
            の, は, でした <br></br>
            昨夜, 最高
        </body>        
    </html>
    

    Server perspective of Encoding

    Server should also know how to interpret the incoming stream of data, which basically means that which encoding scheme to use (server part is tricky because there are several possibilities). Read below from 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.

    This again has 2 parts that how server should decode the incoming request stream and how it should encode the outgoing response stream.

    There are several ways to do this depending upon the use case, for example:

    • There are methods like setCharacterEncoding, setContentType etc. in HTTP request and response object, which can be used to set the encoding.
      • This is exactly what you have done in your case that you have told the server that use UTF-8 encoding scheme for decoding the request data because I am expecting advanced Unicode supplementary plane characters. But this is not all, please do read more below.
    • Set the encoding at server or JVM level, using JVM attributes like -Dfile.encoding=utf8. Read this article on how to set the server encoding.

    In your case you were fetching the Japanese characters from query string of the URL and query string is part of HTTP request object, so using request.setCharacterEncoding("UTF-8"); you were able to get the desired encoding result.

    But same will not work for URL encoding, which is different from request encoding (your case). Consider below example, in both sysout you will not be able to see the desired encoding effect even after using request.setCharacterEncoding("UTF-8"); because here you want URL encoding since the URL will be something like http://localhost:7001/springapp/forms/executorTest/encodingTest/hellothere 昨夜, 最高 and in this URL there is no query string.

    @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);
        }
    

    Depending upon the framework you are using you may need additional configuration to specify a character encoding for requests or URLs so that you can either apply own encoding if the request does not already specify an encoding, or enforce the encoding in any case. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form.

    In Spring, there is org.springframework.web.filter.CharacterEncodingFilter for configuring request encoding. Read this similar interesting question which is based on this fact.

    In nut shell

    Every computer program whether an application server, web server, browser, IDE etc. understands only bits, so it need to know how to interpret the bits to make expected sense out of it because depending upon encoding used, same bits can represent different characters. And that's where "Encoding" comes into picture by giving a unique identifier to represent a character so that all computer programs, diverse OS etc. knows exact right way to interpret it.

    0 讨论(0)
提交回复
热议问题