0

I have a filter where I'm first fetching the request param and then setting the Character Encoding

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
    final HttpServletRequest request = (HttpServletRequest) servletRequest;
    final HttpServletResponse response = (HttpServletResponse) servletResponse;
    final String name = request.getParameter("name");
    request.setCharacterEncoding("UTF-8");

This is not working. As explained in request.getCharacterEncoding() returns NULL... why? that if we dont set the encoding the default is set.

Can we change the encoding once the default is set ? If not what is the exact reason

Bala
  • 11
  • 2

1 Answers1

0

Try it like this

request.setCharacterEncoding("UTF-8");
final String name = request.getParameter("name");

Can we change the encoding once the default is set ? If not what is the exact reason

Yes we can. But you have to be sure setting encoding is executed BEFORE reading parameters

Aramka
  • 111
  • 3
  • 10
  • Thanks @Aramks, the above snippet would work, my question is why the reverse is not possible. i.e getting the param and then setting the encoding – Bala Apr 26 '18 at 08:27