使用MyEclipse遇到的乱码问题解决方法概括
本文以UTF-8编码类型为例
1.首先说一下MyEclipse里要配置哪些地方。
Preferences->General->Content Types 选中Text 编辑下面的Default encoding 为UTF-8然后Update。
Preferences->General->Content Types->Editors->Text Editors->Spelling 下Encoding 选 Default(UTF-8)
Preferences->General->Content Types->Workspace下Text File Encoding 选 Other: UTF-8
2.检查jsp页面中的编码类型是否为UTF-8如,<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 或者<%page language="java" pageEncoding="UTF-8"%>
3.如果项目中用了Struts2则要在struts.xml中加入<constant name="struts.i18n.encoding" value="UTF-8"/>或者在struts.properties中加入struts.i18n.encoding=UTF-8
4.在工程的property->resource的text file encoding里也可以设置下
5.如果在java类里设置了request.setCharacterEncoding("UTF-8");或者response.setContentType("text/html; charset=utf-8"),还是出现乱码可以用
System.out.print(request.getCharacterEncoding());
System.out.print(response.getCharacterEncoding());
察看request 或 response 编码是否不是utf-8
若不是,利用web.xml中的 filter 解决. (注:该filter配置最好写在web.xml的最前面,否则有可能会失效)
如
<filter> <filter-name>characterFilter</filter-name> <filter-class>com.manman.smallBBS.filter.CharacterFilter </filter-class> //注该filter类要自己去写<init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter><filter-mapping> <filter-name>characterFilter</filter-name> <url-pattern>com.manman.smallBBS.filter.CharacterFilter </url-pattern> </filter-mapping>