request.getCharacterEncoding() returns NULL… why?

前端 未结 2 1288
灰色年华
灰色年华 2020-12-31 18:33

A coworker of mine created a basic contact-us type form, which is mangling accented characters (è, é, à, etc). We\'re using KonaKart a Java e-commerce platform on Struts 1.

相关标签:
2条回答
  • 2020-12-31 19:03

    The request.getCharacterEncoding() relies on the Content-Type request attribute, not Accept-Charset

    So application/x-www-form-urlencoded;charset=IS08859_1 should work for the POST action. The <%@page tag doesn't affect the POST data.

    0 讨论(0)
  • 2020-12-31 19:05

    Modern browsers usually don't supply the character encoding in the HTTP request Content-Type header. It's in case of HTML form based applications however the same character encoding as specified in the Content-Type header of the initial HTTP response serving the page with the form. You need to explicitly set the request character encoding to the same encoding yourself, which is in your case thus UTF-8.

    request.setCharacterEncoding("UTF-8");
    

    Do this before any request parameter is been retrieved from the request (otherwise it's too late; the server platform default encoding would then be used to parse the parameters, which is indeed often ISO-8859-1). A servlet filter which is mapped on /* is a perfect place for this.

    See also:

    • Unicode - How to get the characters right?
    0 讨论(0)
提交回复
热议问题