欢迎来到天天文库
浏览记录
ID:35434150
大小:55.72 KB
页数:5页
时间:2019-03-24
《解决jsp中使用request乱码问题》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、核心提示:经常在讨论区看到冇人问我的JSP显示中文冇乱码怎么办,我用request得到的用户输入的中文怎么是乱码经常在讨论区看到有人问我的JSP显示中文有乱码怎么办,我用request得到的用户输入的中文怎么是乱码,我把汉字写到数据库怎么是乱码,等等一些关于汉字乱码的问题。其实这个问题很简单,管它汉字不汉字,还是日文,还是其他的什么双字节的语言,我们一律把它当作UTF-8看待。(一)request中的双字节文字好下面我们就来实现在整个应用程序中使用UTF-8编码工作,之所以选择UTF-8不仅仅之于上述原因,我们知道java的就是基于在UTF-8之上的,所以我们选
2、择UTF-8应该没错"我们首先把我们的.java,,jsp文件都用UTF-8编码来保存,如果以前的没有用UTF-8保存也无所谓,但是建议以后写的都用UTF-8来保存。并在.jsp里面写:<%@pagecontentType二"text/html;charset二UTF-8"%>而不是〈%@pagecontentType二"text/html;charset二UTF-8〃%>然后在web.xml添加下面一段:•••SetCharacterEncoding〈/filter-name〉3、s>com.redv.projects,eduadmin.Ml.filters-SetCharacterEncodingFilterencodingUTF-8SetCharacterEncoding〈/filter-name〉/*4、ping>其「Acom.redv.projects,eduadmin.uti1・filters・SetCharacterEncodingFiltcr的代码如下:packagecom.redvprojects,eduadmin.ut订.filters;importjava.io.IOException;importjavax5、eption;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.UnavailableException;importjavax.servlet,http.HttpServletRequest;importjavax.servlet,http.HttpServletResponse;publicclassSetCharacterEncodingF订terimplementsFilter{protectedStringencoding6、=null;protectedFilterConfigfilterConfig=null;protectedbooleanignore=true;publicvoiddestroy(){this7、haracterencodingtobeusedif(ignore8、9、(request.getCharacterEncoding()二二null)){Stringencoding二selectEncoding(request);if(encoding!=null){request.setCharacterEncoding(encoding);//就是这句话在工作的啦,哈哈,它:0verridesthenameofthecharacterencodingusedinthebodyofthisrequest.Thismethodmustbecalledpriorto10、readingreque
3、s>com.redv.projects,eduadmin.Ml.filters-SetCharacterEncodingFilterencodingUTF-8
4、ping>其「Acom.redv.projects,eduadmin.uti1・filters・SetCharacterEncodingFiltcr的代码如下:packagecom.redvprojects,eduadmin.ut订.filters;importjava.io.IOException;importjavax5、eption;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.UnavailableException;importjavax.servlet,http.HttpServletRequest;importjavax.servlet,http.HttpServletResponse;publicclassSetCharacterEncodingF订terimplementsFilter{protectedStringencoding6、=null;protectedFilterConfigfilterConfig=null;protectedbooleanignore=true;publicvoiddestroy(){this7、haracterencodingtobeusedif(ignore8、9、(request.getCharacterEncoding()二二null)){Stringencoding二selectEncoding(request);if(encoding!=null){request.setCharacterEncoding(encoding);//就是这句话在工作的啦,哈哈,它:0verridesthenameofthecharacterencodingusedinthebodyofthisrequest.Thismethodmustbecalledpriorto10、readingreque
5、eption;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.UnavailableException;importjavax.servlet,http.HttpServletRequest;importjavax.servlet,http.HttpServletResponse;publicclassSetCharacterEncodingF订terimplementsFilter{protectedStringencoding
6、=null;protectedFilterConfigfilterConfig=null;protectedbooleanignore=true;publicvoiddestroy(){this7、haracterencodingtobeusedif(ignore8、9、(request.getCharacterEncoding()二二null)){Stringencoding二selectEncoding(request);if(encoding!=null){request.setCharacterEncoding(encoding);//就是这句话在工作的啦,哈哈,它:0verridesthenameofthecharacterencodingusedinthebodyofthisrequest.Thismethodmustbecalledpriorto10、readingreque
7、haracterencodingtobeusedif(ignore
8、
9、(request.getCharacterEncoding()二二null)){Stringencoding二selectEncoding(request);if(encoding!=null){request.setCharacterEncoding(encoding);//就是这句话在工作的啦,哈哈,它:0verridesthenameofthecharacterencodingusedinthebodyofthisrequest.Thismethodmustbecalledpriorto
10、readingreque
此文档下载收益归作者所有