Kidburla February 2016

What encoding should I use for an HTTP servlet input stream if none was specified?

While reading a ServletInputStream my team was doing something like this:

br = new BufferedReader(new InputStreamReader(servletInputStream));

This unsurprisingly gave a red flag on my code analyzer as the encoding is not specified and so it will rely on whatever the default system encoding is.

The first step would be to try to get the encoding from the request:

String encoding = request.getCharacterEncoding();
if (encoding != null) {
  br = new BufferedReader(new InputStreamReader(servletInputStream), encoding);
}

However, as this related answer told me, most browsers don't send the encoding, which will cause the encoding to be null above. In that case, how on earth am I supposed to know what the encoding is?

Do browsers not send the encoding because:

  1. There is a universally-agreed default encoding for HTTP requests which is used if none was specified? (if so what is it and where is the standard that defines it should always be used), or,
  2. There is some other way to determine what the encoding is? (if so what is it? Surely not just trying different encodings and seeing whether you get garbage or something parseable?)

Answers


Ravindra HV February 2016

Since its a dynamic web application, you'd be expected to have some control over what is the encoding with clients post the request. Usually its UTF-8.

Post Status

Asked in February 2016
Viewed 2,008 times
Voted 6
Answered 1 times

Search




Leave an answer