| 以下是引用片段: <%@page contentType="text/html; charset=UTF-8"%>而不是%@page contentType="text/html; charset=UTF-8"% |
| 以下是引用片段: <web-app> ... <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>com.redv.projects.eduadmin.util.filters.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ... </web-app> |
| package com.redv.projects.eduadmin.util.filters; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.UnavailableException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SetCharacterEncodingFilter 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 { // Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) { request.setCharacterEncoding(encoding); // Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). } } // Pass control on to the next filter 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")) { this.ignore = true; } else if (value.equalsIgnoreCase("yes")) { this.ignore = true; } else { this.ignore = false; } } protected String selectEncoding(ServletRequest request) { return (this.encoding); } } |
这样,我们的request请求就是以UTT-8编码的,在JSP程序中就可以使用:request.getParameter("myKey")来直接得到UTF-8编码的字符串了,而不需要像这样:new String(request.getParameter("myKey").getBytes("ISO-8859-1"), "GBK")来解决那些乱码了。
上一篇: 高性能、高弹性JSP和Servlet性能优化-JSP教程-网络编程-iT粉丝网 专注网页设计制作教程:HTTP://WWW.WEBJX.COM 旧版 Photoshop教程 网络营销 Flash教程 最近更新 会员注册 网页素材 文章投稿 iT粉丝网
下一篇:JSP环境配置:TOMCAT的内存和连接数配置详解-JSP教程-网络编程-iT粉丝网 专注网页设计制作教程:HTTP://WWW.WEBJX.COM 旧版 Photoshop教程 网络营销 Flash教程 最近更新 会员注册 网页素材 文章投稿 iT粉丝网