JSP三种中文解决方案及区别
来源:互联网 发布:淘宝代销保证金 编辑:程序博客网 时间:2024/05/21 17:47
方法一:只是针对某一个字符串起作用
String name=request.getParameter("name");
name=new String(name.getBytes("ISO-8859-1"),"utf-8");
方法二:针对当前页面有效
request.setCharacterEncoding("gbk");
String name=request.getParameter("name");
方法三:针对真个web站点有效
采用过滤器原理
- public class SetEncodingFilter implements Filter {
- protected String encoding = null;
- protected FilterConfig filterConfig = null;
- protected boolean ignore = true;
- public void destroy() {
- this.encoding = null;
- this.filterConfig = null;
- }
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- if (ignore || (request.getCharacterEncoding() == null)) {
- request.setCharacterEncoding(selectEncoding(request));
- }
- chain.doFilter(request, response);
- }
- public void init(FilterConfig filterConfig) throws ServletException {
- this.filterConfig = filterConfig;
- this.encoding = filterConfig.getInitParameter("encoding");
- String value = filterConfig.getInitParameter("ignore");
- if (value == null)
- this.ignore = true;
- else if (value.equalsIgnoreCase("true")
- || value.equalsIgnoreCase("yes"))
- this.ignore = true;
- else
- this.ignore = false;
- }
- protected String selectEncoding(ServletRequest request) {
- return (this.encoding);
- }
- public FilterConfig getFilterConfig() {
- return filterConfig;
- }
- public void setFilterConfig(FilterConfig filterConfig) {
- this.filterConfig = filterConfig;
- }
- }
web.xml
- <filter>
- <filter-name>SetCharsetEncodingFilter</filter-name>
- <filter-class>filter.SetEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>gbk</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>SetCharsetEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- JSP三种中文解决方案及区别
- JSP中文乱码问题分类及解决方案
- JSP中pageEncoding和charset区别,中文乱码解决方案
- JSP中pageEncoding和charset区别,中文乱码解决方案
- JSP中pageEncoding和charset区别,中文乱码解决方案
- JSP中pageEncoding和charset区别,中文乱码解决方案
- JSP中pageEncoding和charset区别,中文乱码解决方案
- JSP/Servlet中pageEncoding和charset区别,中文乱码解决方案
- JSP与MySQL交互的中文乱码解决方案及总结
- JSP实现文件下载及中文乱码解决方案
- JSP中文乱码的产生原因及解决方案
- JSP开发过程遇到的中文乱码问题及解决方案
- JSP开发过程遇到的中文乱码问题及解决方案
- JSP开发过程遇到的中文乱码问题及解决方案
- JSP开发过程遇到的中文乱码问题及解决方案
- JSP开发过程遇到的中文乱码问题及解决方案
- JSP 文件中文乱码的类型分析及解决方案
- JSP的几种中文解决方案
- 微软的软件测试方法(一)
- 获得某月有几天
- 兼容ie和火狐的js无缝八向滚动特效代码
- 一切是从构思开始
- 快乐的菜鸟
- JSP三种中文解决方案及区别
- 看完fight club
- C/C++中的内存管理
- 初恋女友与现任女友
- 如何用PS切图和输出网页?
- 微软的软件测试方法(二)
- Oracle日期操作
- 微软的软件测试方法(三)
- Liskov替换原则--LSP