用Mysql又遇到乱码问题,这次比上次更来的猛,所有的方法都试过了还是不行。Mysql5.0+JSP下,数据库的配置编码都是gbk,在mysql命令行下输入输出中文都正常,但是从JSP输入的中文就是乱码了(输出中文正常),所有的方法都试过了还是不行。如:
数据库编码:gbk,安装的时候就选的这个,后又在命令行下查看编码又进一步证实为gbk
JSP页面设置:contentType="text/html;charset=gbk" pageEncoding="gbk"
request设置:request.setCharacterEncoding("gbk");
数据库连接字符串后加上:useUnicode=true&characterEncoding=gbk需要插入的字段我用System.out.println()打印到Console发现是乱码,种种迹象标明乱码出在JSP上,在插入数据库前就已经是乱码了,可是JSP能改编码的地方我都改了,还是不奏效,就差没用上String的getBytes()方法了,话说我也不晓得他本身是什么编码类型,中文乱码几乎全是由问号组成,求大虾帮忙。

解决方案 »

  1.   

    难道是tomcat的原因?求高手解答
      

  2.   

    获取连接之后,立即执行executeUpdate("set names gbk");
    然后再处理你的JDBC逻辑试试。
      

  3.   

    http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx
    MySQL 中文显示乱码
      

  4.   

    Tomcat一般要使用过滤器,要不然乱码
    web.xml中配置
      <filter>
        <display-name>SetCharacterEncodingFilter</display-name>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <filter-class>SetCharacterEncodingFilter</filter-class>
        <init-param>
          <description>POST编码</description>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
          <description>强制转换编码</description>
          <param-name>ignore</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>server.xml中添加 URIEncoding="UTF-8"
        <Connector port="8080" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443" URIEncoding="UTF-8"  />/**
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */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;/**
     * <p>
     * Example filter that sets the character encoding to be used in parsing the
     * incoming request, either unconditionally or only if the client did not
     * specify a character encoding. Configuration of this filter is based on the
     * following initialization parameters:
     * </p>
     * <ul>
     * <li><strong>encoding</strong> - The character encoding to be configured for
     * this request, either conditionally or unconditionally based on the
     * <code>ignore</code> initialization parameter. This parameter is required, so
     * there is no default.</li>
     * <li><strong>ignore</strong> - If set to "true", any character encoding
     * specified by the client is ignored, and the value returned by the
     * <code>selectEncoding()</code> method is set. If set to "false,
     * <code>selectEncoding()</code> is called <strong>only</strong> if the client
     * has not already specified an encoding. By default, this parameter is set to
     * "true".</li>
     * </ul>
     * 
     * <p>
     * Although this filter can be used unchanged, it is also easy to subclass it
     * and make the <code>selectEncoding()</code> method more intelligent about what
     * encoding to choose, based on characteristics of the incoming request (such as
     * the values of the <code>Accept-Language</code> and <code>User-Agent</code>
     * headers, or a value stashed in the current user's session.
     * </p>
     * 
     * @author Craig McClanahan
     * @version $Revision: 500674 $ $Date: 2007-01-28 00:15:00 +0100 (Sun, 28 Jan
     *          2007) $
     */public class SetCharacterEncodingFilter implements Filter { // ----------------------------------------------------- Instance Variables /**
     * 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); }}
      

  5.   

    因为post过来的东西编码不对,要配置好tomcat
    要不然,你每次都要getBytes很麻烦的
      

  6.   


    我的编码是gbk,把你写的UTF-8都改成gbk能行不,不懂过滤器。
      

  7.   

    数据库方面的编码参照斑竹的帖子就没问题jsp界面的乱码主要有两方面引起:
    1.界面字符集不统一;(表现是在页面上写个中文它就成了乱码)
    2.数据库查询出来的数据与界面的编码不一致,(表现就是查询出来显示的数据是?或其他乱码)如果是一,没什么好说的,google下,把字符集改一致;如果是二给你提供一个解决思路:
    连接数据库的url上加上编码方式;
    读取出来的数据进行encode;
    在jsp界面上显示时在decode。
      

  8.   

    用utf8吧,我曾经因为编码差点崩溃。