braint8  
日历
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
统计
  • 随笔 - 0
  • 文章 - 15
  • 评论 - 16
  • 引用 - 0

导航

留言簿(2)

文章分类(15)

文章档案(15)

搜索

  •  

积分与排名

  • 积分 - 17455
  • 排名 - 1882

最新评论

 
一、传统的方法,修改tomcat/conf/server.xml文件找到如下位置:
    <Connector port="8080"……这个位置,在末尾加上一段 URIEncoding="GBK"
    例:<Connector port="8080"……disableUploadTimeout="true" URIEncoding="GBK"/>

二、修改处理编码的过滤器。
例:CharFilter.java

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws            IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
       
        //通过这里转换后过滤,过滤器的配置在web.xml中不变
        if (req.getMethod().equalsIgnoreCase("get")) {
            this.encoding(req);
        }
       
        request.setCharacterEncoding(encoding);
        response.setCharacterEncoding(encoding);
        chain.doFilter(request, response);
       
    }

    /**
     * 读取参数列表,解决中文传参数问题。
     */
    private void encoding(HttpServletRequest request) {
        Iterator iter = request.getParameterMap().values().iterator();
        while (iter.hasNext()) {
            String[] parames = (String[]) iter.next();
            for (int i = 0; i < parames.length; i++) {
                try {
                    parames[i] = new String(parames[i].getBytes("iso8859-1"),
                            "GBK");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

个人喜欢用第二种方法,因为当你的工程做好后要在别人机器上运行时,就不用去更改这些tomcat的配置了。


posted on 2009-06-01 21:08 情晚风 阅读(1000) 评论(2)  编辑  收藏 所属分类: J2EE
评论:
  • # re: JAVAWEB开发中,解决地址栏传输中文问题!  sunhong Posted @ 2010-01-01 19:41
    按照原始的方法,post提交不会有问题,这个是解决get提交的问题,特别是在分页查询有中文参数的时候  回复  更多评论   

  • # re: JAVAWEB开发中,解决地址栏传输中文问题!  sunhong Posted @ 2010-01-01 19:53
    struts1的话,如果不在过滤器设的话,可以在action对应的actionForm的reset方法设置

    public void reset(ActionMapping mapping, HttpServletRequest request) {
    if (request.getMethod().equalsIgnoreCase("get")) {
    encoding(request,"GBK");
    }
    }  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 情晚风 Powered by: 博客园 模板提供:沪江博客