setCharacterEncoding problems

时光总嘲笑我的痴心妄想 提交于 2021-02-08 04:52:34

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/10710668/setcharacterencoding-problems

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!