java how to decode get url parameter received throw BeanParam

守給你的承諾、 提交于 2019-12-04 00:00:42

问题


I receive a GET response to this web service

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

The class NNNNN has:

@QueryParam("parameter")
private String parameter;

And for that parameter there is a get and set.

I send a request on a get with a query parameter and it is being bind automatically to my option NNNNN, everything is great.

but, now i am sending Japanese strings in the query url. I encode the paramter by UTF-8 before sending, and I have to decode them using UTF-8.

but my question is where should I call the URLDecoder? i tried to call it in the getter of that parameter, but it didn't work, i kept having something like C3%98%C2%B4%C3%98%C2 instead of the Japanese characters


回答1:


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">



回答2:


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.



来源:https://stackoverflow.com/questions/33439705/java-how-to-decode-get-url-parameter-received-throw-beanparam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!