我使用的是struts
我在tomcat的server.xml中加了URIEncoding="UTF-8"
在web.xml中写了过滤器,过滤器中加了
request.setCharacterEncoding("UTF-8");
都没能解决。在FormAction.java中的set方法中直接出来乱码
public void setGo_timehm(String go_timehm) {
this.go_timehm = go_timehm
}
也就是说,从页面过来的值已经是乱码了。
于是我添加了这个方法来转码
private String toGBK(String str) throws java.io.UnsupportedEncodingException
{
return new String(str.getBytes("ISO-8859-1"),"GBK");
}但是在set方法中引用的时候却报错
public void setGo_timehm(String go_timehm) {
this.go_timehm = new String("123".getBytes("ISO-8859-1"),"GBK");
}
unhandled exception type unsupportedEncodingException这个错误
小弟百思不得其解,现在我快疯了,大哥们救救我把

解决方案 »

  1.   

    是不是编码格式不对?打印一下字串的HEX值看看是何种编码
      

  2.   

    打印一下字串的HEX值看看是何种编码?
    怎么打,不明白
      

  3.   

    set方法贴错了

    public void setGo_timehm(String go_timehm) {
    this.go_timehm = this.toGBK(go_timehm);
    }
    报unhandled exception type unsupportedEncodingException这个错误
    编译不过去,有红X
      

  4.   

    大哥,方法throws异常了,下面得catch处理下把,错误报的很清楚
    public void setGo_timehm(String go_timehm) throws UnsupportedEncodingException{
    this.go_timehm = this.toGBK(go_timehm);
    }
      

  5.   

    感谢
    vagrant1984
    的回答,使我疏忽了,不报错了,可以正常运行
    但仍然是乱码。
    setGo_timehm(String go_timehm)的参数go_timehm本身就是乱码,转完了还是乱码
    朋友们帮忙亚
      

  6.   

    你是用什么方式提交的,用POST方式就不会有文题了,在有也不用改tomcat的配置文件
      

  7.   

    标签
    <html:button value="" property="leave"  onclick="submit1('leave');"></html:button>提交javascript
    function submit1(submitType)
    {
    MainForm1.hd_action.value=submitType;
    document.MainForm1.submit();}
      

  8.   

    用的是post
    <html:form method="POST" action="/Main.do">
      

  9.   

    sturts.cfg.xml下添加 
    <controller processorClass="common.Trans" />
    添加类
    package common;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.RequestProcessor;public class Trans extends RequestProcessor{
    public Trans(){}

    protected boolean processPreprocess( HttpServletRequest request,
                                         HttpServletResponse response ){
     
     try{
       request.setCharacterEncoding("UTF-8");

     }
     catch(Exception ex){
    ex.printStackTrace();
     
     }
     return true;
    }
    }
    试下
      

  10.   

    to:KingNE() 
    试了,无效。还是一样的乱码顺便说一句,我这个项目做的对日外包,转码我用的“JIS”
    怕有的人说闲话,提问题的时候就写成了“GBK”
      

  11.   

    我调试了一下,在页面上是
    2007/05/31(木) 13:17 
    到了
    public void setGo_timehm(String go_timehm) throws java.io.UnsupportedEncodingException{
    this.go_timehm = this.toGBK(go_timehm);
    }
    里面就成了2007/05/31(&#65533;&#65533;) 13:17 了,怎么转都没用了,郁闷亚
      

  12.   

    启动NetBeans
    打开原来的SimpleWebSite_web项目,将所依赖的SimpleWebSite_dao与SimpleWebSite_service两个项目同时打开
    右键点SimpleWebSite_web项目的Source Packages
    New
    File/Folder...
    选“Web”,File Type为Filter
    NextClass Name:EncodingFilter
    Package:org.liupopo.simplewebsite.web.filter
    Next
    要选上“Add information to deployment descriptor (web)”, 这样NetBeans会自动在web.xml中配置好相应的Filter设置
    Next
    在这里我们可以设置Filter要用到参数,因为我想可以通过修改web.xml中的参数改变网站的编码,所以“New”,name为“Encode”value为“UTF-8”
    Finish看看NetBeans为我们做了什么
    1.在web.xml增加了以下Filter的内容:
        <filter>
            <filter-name>EncodingFilter</filter-name>
            <filter-class>org.liupopo.simplewebsite.web.filter.EncodingFilter</filter-class>
            <init-param>
                <param-name>Encode</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>EncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    省了我们自己手动配置web.xml了。而且NetBeans对web.xml的编辑提供了可视化的操作界面,对web.xml中的各项设置进行了分类,非常方便我们的修改。
    2.为我们生成了EncodingFilter这个类,刚打开的时候吓我一跳,一个Filter怎么这么多代码?原来是NetBeans为我们把一些常用的方法已经写好,我们只要在相应的位置加上我们的做具体事情的代码就可以了。NetBeans为我们加的doBeforeProcessing()和doAfterProcessing()两个方法是想我们在Filter之前和之后做些什么,生成的代码里已经做了日志处理,并且还有一大段被注释起来的示例代码,但我平时都是在init()里做一些初始化的处理,这两个方法显得有些罗嗦了,并且对于log,我回头要统一处理,所以全部去掉。
    最后的EncodingFilter内容为:
    package org.liupopo.simplewebsite.web.filter;import java.io.*;
    import javax.servlet.*;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  liupopo
     * @version 1.0
     */
    public class EncodingFilter implements Filter {
        
        // The filter configuration object we are associated with.  If
        // this value is null, this filter instance is not currently
        // configured.
        private FilterConfig filterConfig = null;
        private String encode = null;
        
        public EncodingFilter() {
        }
        
        /**
         *
         * @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 {
            
            request.setCharacterEncoding(encode);
            try {
                chain.doFilter(request, response);
            } catch(Throwable t) {
                t.printStackTrace();
            }
        }
        
        
        /**
         * Return the filter configuration object for this filter.
         */
        public FilterConfig getFilterConfig() {
            return (this.filterConfig);
        }
        
        
        /**
         * Set the filter configuration object for this filter.
         *
         * @param filterConfig The filter configuration object
         */
        public void setFilterConfig(FilterConfig filterConfig) {
            this.filterConfig = filterConfig;
        }
        
        /**
         * Destroy method for this filter
         *
         */
        public void destroy() {
        }
        
        
        /**
         * Init method for this filter
         *
         */
        public void init(FilterConfig filterConfig) {
            this.filterConfig = filterConfig;
            if (filterConfig != null) {
                this.encode = this.filterConfig.getInitParameter("Encode");
            } else {
                this.encode = "UTF-8";
            }
        }
    }好了,Clean and Build Project,
    Run Project输入中文进行注册,可以正常的保存与显示中文啦。
    每次启动这个系统之前都要记得启动数据库Server,有时觉得挺烦的,本打算把HsqlDb的Server模式改为In-Process (Standalone) Mode,但数据库文件的路径又成问题,暂时还是先麻烦点吧。
    Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=638078NetBeans5.0中文版JSP中文问题终于解决 这些天用NetBeans5.0中文版编了一个项目,感觉确实不错,但解决JSP中文问题让我头疼一下午,现终于解决。
    使用默认的NetBeans设置,所有JSP网页默认编码均为UTF-8 ,这样浏览器发送参数也是基于UTF-8的,所以用Servlet处理的时候会出现乱码问题。按以前的方法,编码为“ISO-8859-1",无效,不但这个无效,其它编码如”GBK“,”UTF-8"等,均无效。后来偶然注意到NetBeans是用UTF-8编码JSP文件的(以前用DW)设计JSP,默认为GB2312 ,随即将其换为GB2312一试,但换编码无异于将网页重写一遍(幸好我只是做了个大概,还没有做美工设计,要不就惨了!),
    因为所能的字符全变为乱码,将UTF-8编码的JSP文件复制到DW里,仍然是UTF-8 ,只好在DW里新建一个编码为GB2312的文件,将网页重写一遍。写完后,再用NetBeans打开,这样文件就成为GB2312编码了,运行一试,果然不再乱码!所以以后再用NetBeans编写JSP,一定要先将编码设置为GB2312,然后再写网页,我是忘 不了了,呵呵。
      

  13.   

    问题解决了,被我误打误撞碰见的,将过滤器中的代码
    request.setCharacterEncoding("utf-8");
    改成
    request.setCharacterEncoding("shift_jis");
    就行了。
    感谢KingNE() 给了我启发。
    还有其他同志的热心帮助,等一回没有问题的话,散分
      

  14.   

    前台用的<%@ page contentType="text/html; charset=shift_jis"%>
    数据库编码问题还没有考虑
      

  15.   


    试试看我的吧
    我也刚刚做完基于struts得系统 
    乱码问题的解决  论文哦 呵呵5.8.1 显示及输入为汉字
    使页面显示为汉字,只需把页面的字符集设定为charset=“gb2312”和pageEncoding="gb2312"即可。
    把输入转为汉字,需要在元素所在的form中添加enctype="multipart/form-data",并在将输入更新到数据库前,作如下转换
    exp.getExp_cont().getBytes("gb2312").toString();
    即把输入内容转换为编码为gb2312的字节,然后再重新转换成字符串。这样就做到了显示在页面上及在数据库中都为汉字。
    5.8.2 错误信息的中文显示
    由于异常及错误信息都定义在Application.properties文件中,必须先将其为转换为unicode编码,否则在页面显示为乱码。
    转换时用native2ascii命令,格式为:
    native2ascii[–encodinggbk]application.properties application_cn.properties
      

  16.   

    1、创建个Filter类,在里面extetds ActionServlet
    2、
    protected void process(HttpServletRequest request,HttpServletResponse response)throws java.io.IOException,
    javax.servlet.ServletException{

    request.setCharacterEncoding("gb2312");//过滤编码集
    super.process(request, response);
    }3、XML里把action的Class指向到你这个类的位置。就OK了
      

  17.   

    结论,在过滤器的request.setCharacterEncoding("");方法中不要使用utf-8,而应该使用你需要的语言的编码,比如gb2312,日语是shift_jis
    谢谢各位
    结贴
      

  18.   

    环境:
    WindowsXP中文
    Eclipse3.2.1+Myeclipse5.1.0GA
    Tomcat5.5
    JDK1.5.0
    Hibernate3.1
    Mysql5.0+ mysql-connector-java-5.0.4-bin.jar方案:
    1.集成开发环境Eclipse中设置文本文件存储编码为UTF-8。
    //我想是因为….如果所做工程项目最终要在别的版本操作系统的服务器上跑,这里需要设置(未经证实)
    2.数据库mysql,默认编码使用utf8;
    并且创建数据库时在语句后面追加DEFAULT CHARSET=utf8;set names utf8;
    //如果数据库默认编码是utf8,那这个也不是必需的吧(未经证实)
    3.跟数据库连接的URL:Hibernate URL:
    jdbc:mysql://127.0.0.1:3306/addressbook?useUnicode=true&characterEncoding=utf8
    //如果数据库默认编码是utf8,那这个也不是必需的吧(未经证实)
    4.使用过滤器,过滤器文件代码见后面附1。
    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>org.biti.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>
    //过滤的是post还是get还没弄明白,据说只过滤器中一个,另一个见5。
    5.修改Tomcat配置文件server.xml中Connector部分
    <Connector port="80"               
    maxHttpHeaderSize="8192"
        maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
        enableLookups="false" redirectPort="8443" acceptCount="100"
    connectionTimeout="20000" disableUploadTimeout="true" />
    加入URIEncoding="UTF-8"一项。
    //我现在这个没乱码的就没有设置….附1:SetCharacterEncodingFilter.java(可在Tomcat示例源码中找到)
    package org.biti.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;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 {
    if (ignore || (request.getCharacterEncoding() == null)) {
    String encoding = selectEncoding(request);
    if (encoding != null)
    request.setCharacterEncoding(encoding);
    }
    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);
    }
    }
      

  19.   

    乱码是3中情况照成 请楼主逐一检查
    1.页面编码问题  
    2.页面传递参数编码
    3.业务或数据访问层编码问题 如JAVABEAN编译时的编码设置和数据库内字段编码问题