初学JAVA不久,眼下就遇到了几个比较棘手的问题,,,,
1。中文乱码问题,从网上找了好多方法也不行。具体原因如下:
我存在数据库里面的是中文,而且显示出来的也是中文,我给他它一个链接,点到另外一个页对它时行修改,但是我点到另一个页面时就成了乱码了。为什么,?那位遇到过各种问题??
2。关于Map和HashMap,List和ArrayList:
由于这几天用到了这两对,所以就有点疑问,我是新手,高手看了不要见笑呀。。
比如说有一个方法,返回的是Map类型的。。但是为什么用时我要这样:
Map map =null; 
map = new HashMap();
map = method返回值?
为什么不直接这样用:
Map map =null; 
map = new HashMap();
还简单,,,。
那位高手知道,,,,帮忙解决一下。谢了,,,解决了一定结贴

解决方案 »

  1.   

    1.是不是修改页面的编码不对?
    2.map = new HashMap();可以省略(最好省略)Map map =null; 
    map = method返回值
    这样就可以了
      

  2.   

    1.编码问题:
      (1)首先确定JSP页面头部是否有:<%@ page contentType="text/html; charset=GBK" %>
      (2)用这个转码:
         String param= new String(request.getParameter("param").getBytes("ISO-8859-1"), "GBK");
      (3)添加filter字符过滤器
      (4)如果是通过"a.jsp?param=中文"传递参数,则需要:
         a.在传参数之前先把参数进行转码:java.net.URLEncoder.encode(param);
           取值用java.net.URLDncoder.dncode(param);再转回中文
         b.在你的Tomcat目录-->conf目录-->server.xml里找出这段:
           <Connector 
    port="8080"               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                   enableLookups="false" redirectPort="8443" acceptCount="100"
                   debug="0" connectionTimeout="20000" 
                   disableUploadTimeout="true" <!--在里边加上这个参数-->URIEncoding="gb2312"
     />
      

  3.   

    2.Map map =null; 
    map = new HashMap();
    map = method返回值?
    ===============
    map=new HashMap();//新创建一个空HashMap对象,可以省略掉
    map=method返回值?//method方法返回一个有内容的HashMap对象,不能省略
      

  4.   

    拜托大家尽量使用UTF-8,国际化趋势哦,
      

  5.   

    用的一个过滤器。代码如下:
    package com.lztx.util;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;        
        }
        
        /**
         * 选择这次过滤所使用的语言编码
         * @param request 本次servlet请求的用户request实例
         * @return 选择出的语言编码
         */
        protected String selectEncoding(ServletRequest request) 
        {
            return (this.encoding);
        }
    }
    我用的是struts
    在web.xml下的配置如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app>
        <filter>
          <filter-name>SetCharacterEncoding</filter-name>
          <filter-class>com.lztx.util.SetCharacterEncodingFilter</filter-class>
          <init-param>
            <param-name>encoding</param-name>
            <param-value>GB2312</param-value>
          </init-param>
        </filter>
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/config/struts-config.xml</param-value>
        </init-param>
        <init-param>
          <param-name>debug</param-name>
          <param-value>3</param-value>
        </init-param>
        <init-param>
          <param-name>detail</param-name>
          <param-value>3</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
         <welcome-file>login.jsp</welcome-file>
      </welcome-file-list>
      <taglib>
            <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
        </taglib>
    </web-app>
    为什么还是不行,还需要什么东西吗?
      

  6.   

    TO lip009(深蓝忧郁) ?是什么原因
      

  7.   

    web.xml里没有加这个
      <filter-mapping>
        <filter-name>SetCharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      

  8.   

    你是怎么传参数的?
    是a.jsp?param=中文
    还是通过表单提交
      

  9.   

    lip009(深蓝忧郁) 加了还是不行呀。郁闷了。。
      

  10.   

    我增加时用struts的mvc做的,,,
    修改时用的是a.jsp?param=中文现在我加了RUIEncoding = "gb2312"之后好了。但是增加的还是不行。。为什么?lip009(深蓝忧郁)
      

  11.   

    UTF-8 怎么解决呢??/?xlyyc(宇)
      

  12.   

    <html:form  action="/collKindNew.do?method=collKindNew" method = "post">用的这种方法。。怎么回事呢。
      

  13.   

    怎么回事呀?用UTF-8怎么解决郁闷中!
      

  14.   

    在你的filter里打印消息,看看是否执行了filter过滤器
      

  15.   

    这个问题我刚好最近总结过,不过,我用的是webwork,仅供参考***  ***
    根本java中文问题的解决之道是统一编码为UTF-8     1)页面中文硬编码输出:
     使用JSTL标签(也可用到structs中,),所有输出都是通过message.perporyt文件定义,基本不再出现本地化字符在客户端的页面上2)中文数据存储:
     统一在hibernate的jdbc字符串中注释为utf-8, 
     如jdbc:mysql://localhost:3306/hgoa?useUnicode=true&amp;characterEncoding=utf-83)页面输入中文的角本编译:
        (a)用页面首语句统一编码      
          <%@ page language="java"  pageEncoding="utf-8"  %>    
            (b)在web.xml中加拦截器(这里是用的spring的一个拦截器)
          <filter>       
           <filter-name>Set CharacterEncoding</filter-name>
                <filter-class>
                      org.springframework.web.filter.CharacterEncodingFilter
                  </filter-class>    
                  <init-param>          
                    <param-name>encoding</param-name>        
                <param-value>UTF-8</param-value>       
                  </init-param>     
                  <init-param>  
                   <param-name>forceEncoding</param-name>        
                   <param-value>true</param-value>        
                  </init-param>    
                </filter>     3)服务器端的设置:
      tomcat的server.xml,设置8080端口的容器中属性为
               (其中,useBodyEncodingForURI="true" 是关键句):     
         <Connector
     useBodyEncodingForURI="true" 
            port="8080"
            redirectPort="8443"
            minSpareThreads="25"
            connectionTimeout="20000"
            maxSpareThreads="75"
            maxThreads="150"
            maxHttpHeaderSize="8192">
        </Connector> 
           
    4)页面间的中文参数传递:
     将URL里的中文参数转成ASC码字符串,例如引用urlencode.js中的方法UrlEncode(str) 
           
    <script src="/query/jslib/urlencode.js"></script> 
    <script>  
           function sub(){
            var productname=new String();
            productname=getValue('fsearch','productname');
           top.down.location='searchProductsbyClassTwo.do?productname='+UrlEncode(productname)+'&classid='+<ww:property value="#request.classid"/>; 
      } 
    </script>urlencode.js中:
    function UrlEncode(str){
    var i,c,ret="",strSpecial="!\"#$%&'()*+,/:;<=>?@[\]^`{|}~%";
    for(i=0;i<str.length;i++){
    if(str.charCodeAt(i)>=0x4e00){
    c=qswhU2GB[str.charCodeAt(i)-0x4e00];
    ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
    }
    else{
    c=str.charAt(i);
    if(c==" ")
    ret+="+";
    else if (c=="\r")
    ret+="%0d";
    else if (c=="\n")
    ret+="%0a";
    else if(strSpecial.indexOf(c)!=-1)
    ret+="%"+str.charCodeAt(i).toString(16);
    else
    ret+=c;
    }
    }
    return ret;
    }***  ***
    以上是我们试验了N多种中文解决之道后,总结出来的最有效最简洁也最彻底的办法(自吹一下)
      

  16.   

    非常感谢各位了,但是我想用过滤器。。配置如下: 
    <filter>
          <filter-name>Set Character Encoding</filter-name>
          <filter-class>com.lztx.util.SetCharacterEncodingFilter</filter-class>
          <init-param>
            <param-name>encoding</param-name>
            <param-value>GB2312</param-value>
          </init-param>
        </filter>
        <filter-mapping>
            <filter-name>Set Character Encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    只要设置成这个就行了吗?/我设了不起作用呀
      

  17.   

    楼主,若你坚持用GBK,那么1、把JSP页面头部的<%@ page contentType="text/html; charset=GBK" %>换成
    <%@ page language="java" pageEncoding="GBK" %>  2、改tomcat的server.xml,设置8080端口的容器中的属性URIEncoding="GBK"换成
    useBodyEncodingForURI="true" 如下所示:(其中,useBodyEncodingForURI="true" 是关键句):    
    <Connector
    useBodyEncodingForURI="true"
    port="8080"
    redirectPort="8443"
    minSpareThreads="25"
    connectionTimeout="20000"
    maxSpareThreads="75"
    maxThreads="150"
    maxHttpHeaderSize="8192">
    </Connector>
      

  18.   

    哦,
    3、你的过滤器那里也要统一成GBK,不然会有少量的字符乱码:
    <filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>com.lztx.util.SetCharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>GBK</param-value>
    </init-param>
    </filter>***  ***
    或者页面的encoding和过滤器的encoding都统一成GB2312也行至于中文参数的传递问题,我因为用的是webwork框架,用转码、get set的toCN,试过很多种方法,最后确定的是转成ASC码是彻底的解决之道
      

  19.   

    最方便的方法
    建个 FilterServlet
    先声明个类变量 private String encoding="GBK";
    在doFilter方法中
    写入:
    HttpServletRequest hreq=(HttpServletRequest)request;
    hreq.setCharacterEncoding(encoding);//对于每个请求都会重新设置编码;
    filterChain.doFilter(request, response);//以下都是系统自己生成的用这个方法我认为最简单的。(其实Tomcat自己也可以设置编码转换,不过实在是麻烦)
      

  20.   

    第一个问题相信已经解决,那是编码问题!
    第二个问题我要说一下。
    map是接口而不是类,所以你无法写成
    Map map = new Map();
    你必须以接口定义,而以实现该接口的类来赋值
    Map map = new [实现Map接口的类]();
      

  21.   

    1.中文乱码问题
    在jsp页面加上如下代码基本上可以消除乱码问题:
    <%@ page contentType="text/html; charset=GBK" %>
    <%
    request.setCharacterEncoding("GBK");
    %>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
      

  22.   


    你必须以接口定义,而以实现该接口的类来赋值
    Map map = new [实现Map接口的类]();这个就是JAVA中称为动态调用机制
      

  23.   

    最近遇到这个问题,搞了一两个小时才搞出来。
    <%@ page contentType="text/html; charset=GBK" %>
    <%
    request.setCharacterEncoding("GBK");
    %>加上这两句代码之后,得到的参数也有可能是乱码,解决方法是,一定要把之前的页面的提交方法设为POST。设为GET的话就全是乱码。
    我也不知道为什么是这样,希望有人能回答。
      

  24.   

    非常感谢大家都这么热心,但是问题还没没有解决撒!!
    <%@ page contentType="text/html; charset=GBK" %>
    <%
    request.setCharacterEncoding("GBK");
    %>
    这种方法试过了,肯定不行。。
    还有就是我现在要用过滤器的话,具体有那些步骤,还有设置和过滤器的代码等,有那位用过,发出来我的看看,是不是我那里写错?
      

  25.   

    <%@ page language="java" pageEncoding="GBK" %>  
    <%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic" %>
    <%
    request.setCharacterEncoding("GBK");
    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
    response.setHeader("Pragma","no-cache"); //HTTP 1.0
    response.setDateHeader ("Expires", -1);
    response.setDateHeader("max-age", 0);
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html:html>
    <head>
    <title>文章类别(新增类别)</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <link href="css/style.css" rel="stylesheet" type="text/css">
    <script language="JavaScript" type="text/JavaScript" src="js/common.js"></script>
     </head><body>
    <table width="96%"  border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td height="10">&nbsp;</td>
      </tr>
    </table>
    <table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr align="center">
        <td width="14" height="38" align="left" background="images/div_topBg.gif"><img src="images/add_topLeft.gif" width="12" height="38"></td>
        <td height="38" align="center" background="images/div_topBg.gif">
          <table width="100%"  border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="2%" align="left"><img src="images/div_topPic.gif" width="11" height="38"></td>
              <td width="84%" class="TFont2" style="padding-top:2px">新增文章类别</td>
              <td width="14%" align="right"></td>
            </tr>
           </table></td>
        <td background="images/div_topBg.gif" width="12" height="38" align="right"><img src="images/add_topRight.gif" width="14" height="38"></td>
       </tr>
        <tr>
         <td height="50" bgcolor="#FFFFFF" class="divBg1">&nbsp;</td>
     <td align="center" bgcolor="#FFFFFF">&nbsp;</td>
     <td bgcolor="#FFFFFF" class="divBg2">&nbsp;</td>
       </tr>
       <tr>
         <td height="351" bgcolor="#FFFFFF" class="divBg1">&nbsp;</td>
         <td align="center" bgcolor="#FFFFFF">
         <html:form  action="/collKindNew.do?method=collKindNew" method = "post">
           <table width="80%"  border="0" cellspacing="0" cellpadding="0">
            <tr>
             <td height="30" align="right" width="15%">类别名称:</td>
             <td width="85%">
               <html:text property="fl_name" style="width:90%" styleClass="input" onmouseout="this.style.backgroundColor = ''" onmouseover="this.style.backgroundColor = '#ffffff'"/>
       <br>
     </td>        <tr>
    <td width="15%">&nbsp;</td>
             <td height="40" align="left" width="85%"><a href="#" onclick="document.forms[0].submit();return false"><img src="images/B_submit_1.gif" name="Image32" width="58" height="20" border="0" onMouseOver="this.src='images/B_submit_2.gif'"  onMouseOut="this.src='images/B_submit_1.gif'"></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="addindex.jsp"><img src="images/B_cancel_1.gif" name="Image32" width="58" height="20" border="0" onMouseOver="this.src='images/B_cancel_2.gif'"  onMouseOut="this.src='images/B_cancel_1.gif'"></a></td>
            </tr>
           </table>
         </html:form>
     </td>
         <td bgcolor="#FFFFFF" class="divBg2">&nbsp;</td>
       </tr>
       <tr>
         <td height="10" bgcolor="#FFFFFF" class="divBg1">&nbsp;</td>
         <td align="center" bgcolor="#FFFFFF">&nbsp;</td>
         <td bgcolor="#FFFFFF" class="divBg2">&nbsp;</td>
       </tr>
       <tr>
         <td background="images/div_bottomBg.gif" align="left"><img src="images/add_bottomLeft.gif" width="14" height="13"></td>
         <td background="images/div_bottomBg.gif">&nbsp;</td>
         <td background="images/div_bottomBg.gif" align="right"><img src="images/add_bottomRight.gif" width="13" height="13"></td>
       </tr>
    </table>
    </body>
    </html:html>
    这是用来增加的页面,增加之后显示的是乱码。。看看。。
      

  26.   

    楼主,如果是传参数过去的时候就变成了乱码,并且用了
    <%@ page contentType="text/html; charset=GBK" %>
    <%
    request.setCharacterEncoding("GBK");
    %>
    这些语句的话,那就是你的提交中文的页面的提交方法没有设置成POST.
    提醒:默认的方法是GET。