Hello,

The reader that HttpServletRequest#getReader returns
seems to decode characters not using the character encoding
set by ServletContext#setRequestCharacterEncoding(since Servlet 4.0).

My questions are:
1. Is this behavior intentional(e.g. for backward compatibility)?
2. If this behavior is intentional, is there any specification
describing such ServletContext#setRequestCharacterEncoding
and HttpServletRequest#getReader behaviors?


I have created a simple war application and tested
ServletContext#setRequestCharacterEncoding as follows.

[Environment]
Tomcat9.0.19 (I don't change any default configuration)
JDK11
Windows8.1

[index.html]
#####################################################################
    <button type="button" id="the_button">post</button>
    <script>
        document.getElementById('the_button').addEventListener('click',
function() {
            var xhttp = new XMLHttpRequest();
            xhttp.open('POST', '/SimpleWarApp/app/simple');
            xhttp.setRequestHeader('Content-Type', 'text/plain');
            <!-- The body content is Japanese character '\u3042' -->
            xhttp.send('あ');
        });
    </script>
#####################################################################

[InitServletContextListener.java]
#####################################################################
@WebListener
public class InitServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        sce.getServletContext().setRequestCharacterEncoding("UTF-8");
    }
}
#####################################################################

[SimpleServlet.java]
#####################################################################
@WebServlet("/app/simple")
@SuppressWarnings("serial")
public class SimpleServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

        System.out.println("requestCharacterEncoding : " +
req.getServletContext().getRequestCharacterEncoding());
        System.out.println("req.getCharacterEncoding() : " +
req.getCharacterEncoding());

        String hello = req.getParameter("hello");
        if (hello != null) {
            System.out.println("hello : " + req.getParameter("hello"));
        } else {
            System.out.println("body : " + req.getReader().readLine());
        }
    }
}
#####################################################################

I don't have any servlet filters.
The above three are all the components of this war application.


Here are the war application's console logs.

Case 1:
When I submit the form with a parameter 'hello',
the value of 'hello' is successfully decoded as follows.
#####################################################################
requestCharacterEncoding : UTF-8
req.getCharacterEncoding() : UTF-8
hello : あ
#####################################################################

Case 2:
When I click 'post' and send text content,
the request body cannot be successfully decoded as follows.
#####################################################################
requestCharacterEncoding : UTF-8
req.getCharacterEncoding() : UTF-8
body : ???
#####################################################################

Case 1 is OK,
but at Case 2, I expect for the request body to be successfully decoded
by the character encoding set by ServletContext#setRequestCharacterEncoding.


I originally posted the issue on StackOverflow.
There are a bit of more information there:
https://stackoverflow.com/questions/56087155/why-servletcontextsetrequestcharacterencoding-does-not-have-an-effect-on-htt


Thanks,

Tomoki

Reply via email to