Lucy
Lucy

Reputation: 491

setCharacterEncoding problems

I have this line of code:

response.setCharacterEncoding("UTF-8");

I'm getting this error:

The method setCharacterEncoding(String) is undefined for the type HttpServletResponse

Eclipse suggests to cast the response to request which is some thing I don't want to. Can any one help me fix this, please?

Upvotes: 2

Views: 23453

Answers (3)

Paul Vargas
Paul Vargas

Reputation: 42030

Another way would be to set the content type.

response.setContentType("text/html;charset=UTF-8");

You can read in the docs on method setContentType:

Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691765

See the javadoc. This method exists since servlet 2.4. Either your server supports this version (or later) of the servlet spec, and the jar in your buildpath is too old, or it doesn't support it, and you should not use this method.

In the latter case, read the javadoc to know by what you should replace it.

Upvotes: 3

Jeshurun
Jeshurun

Reputation: 23186

The method setCharacterEncoding(String charset) is defined in HttpServletResponse's parent class, ServletResponse. If eclipse complains that it is undefined for the type, you probably have an incorrect import statement. Try deleting all your import statements, and then pressing Ctrl + O.

Upvotes: 2

Related Questions