征集jsp/servlet乱码解决方案,最好能够有个终极解决办法
本人分别用过 asp, asp.net 和 java 开发web应用程序,但发现java别的地方都好,就是乱码问题实在让人头大:
1、在一台机器上运行得好好的程序,拷贝工程到另一台机器上运行时就变成了乱码
2、本来一个好好的html或jsp页面,如果是从一个 servlet 中 forward 过来,就变成了乱码
3、硬编码的一条插入语句,存到数据库中去却变成了乱码
......
不同的问题要用不同的办法解决
实在想不明白 tomcat 中为什么会有那么多种编码,很多配置文件是用 UTF-8,但有些配置文件却用 ISO-8859-1。Unicode 被应用已经不止10年了,为什么象 ISO-8859-1 这类编码还在tomcat中被一直使用,害得中国的java程序员一直被编码问题蹂躏。人家 asp.net 的乱码问题就很少,有时出现一点,差不多在一个配置文件设置一次就可以全部搞掂,为什么 tomcat 不可以呢。
[解决办法]
一般分为两种情况 1:提交表单接收中文一般需要在接收的servlet里设置 request.setCharacterEncoding("GBK");
2:<a href="aaa?userName='张三'"> 这种形式传的需要在接收的servlet里做如下操作
String userName=request.getParameter("userName");
byte[] temp=userName.getBytes("ISO-8859-1");
userName=new String(temp,"GBK");
[解决办法]
像楼主的问题不难,一般通用的解决方案是加一个过滤器的话就OK了..
过滤器代码如下:
package com.lanzhengwu.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;/** * @author Administrator * * 更改所生成类型注释的模板为 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释 */public class SetCharacterEncodingFilter implements Filter { /** * The default character encoding to set for requests that pass through * this filter. */ protected String encoding = null; /** * The filter configuration object we are associated with. If this value * is null, this filter instance is not currently configured. */ protected FilterConfig filterConfig = null; /** * Should a character encoding specified by the client be ignored? */ protected boolean ignore = true; // --------------------- Public Methods /** * Take this filter out of service. */ public void destroy() { this.encoding = null; this.filterConfig = null; } /** * Select and set (if specified) the character encoding to be used to * interpret request parameters for this request. * * @param request The servlet request we are processing * @param result The servlet response we are creating * @param chain The filter chain we are processing * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet error occurs */ 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); } // Pass control on to the next filter chain.doFilter(request, response); } /** * Place this filter into service. * * @param filterConfig The filter configuration object */ 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 Methods /** * Select an appropriate character encoding to be used, based on the * characteristics of the current request and/or filter initialization * parameters. If no character encoding should be set, return * <code>null</code>. * <p> * The default implementation unconditionally returns the value configured * by the <strong>encoding</strong> initialization parameter for this * filter. * * @param request The servlet request we are processing */ protected String selectEncoding(ServletRequest request) { return (this.encoding); }}
[解决办法]
JSP页面
<%@ page contentType="text/html;charset=UTF-8"%>
JDBC连接数据库时
String url="jdbc:mysql://hostname/database?useUnicode=true&characterEncoding=utf8";
数据库的字符集也是uft8
以上三个条件都满足了,那么任何字符(即使是阿拉伯文)都不会出乱码了!
如果只是中文可以考虑都设成GBK.