request.getQueryString() seems to need some encoding

前端 未结 5 1245
盖世英雄少女心
盖世英雄少女心 2020-11-28 05:35

I have some problem with UTF-8. My client (realized in GWT) make a request to my servlet, with some parametres in the URL, as follow:

http://localhost:8080/s         


        
5条回答
  •  难免孤独
    2020-11-28 06:05

    From the HttpServletRequest#getQueryString() javadoc:

    Returns: a String containing the query string or null if the URL contains no query string. The value is not decoded by the container.

    Note the last statement. So you need to URL-decode it youself using java.net.URLDecoder.

    String queryString = URLDecoder.decode(request.getQueryString(), "UTF-8");
    

    However, the normal way to gather parameters is just using HttpServletRequest#getParameter().

    String param = request.getParameter("param"); // così
    

    The servletcontainer has already URL-decoded it for you then if you have configured it to use the correct encoding. The request.setCharacterEncoding() has only effect on the request body (POST) not on the request URI (GET). Also see Mirage's answer.

提交回复
热议问题