java乱码 解决大全

乱码问题:

1. jsp页面中出现乱码:再jsp的头<%@ %>中加一个<%@ …… pageEncoding="utf-8"%>

  修改myeclipse使其默认新建的jsp页面为utf-8
 
      设置:windows-->jsp-->Encoding    (第一个就是)

2. servlet直接输出页面中的乱码:

    设置:   response.setContentType("text/html;charset=utf-8");

3. post表单传递参数乱码:

      设置1:  request.setCharacterEncoding("utf-8");

      设置2:更好的方法:使用过滤器技术,在接收参数之前,设定传递的参数内容的编码是UTF

-8,
  
   CharacterEncodingFilter
   在spring.jar包的org.springframework.web.filter包下有个CharacterEncodingFilter.java

   然后在web.xml文件中<servlet>元素前加以下代码:
<filter>
         <filter-name>CharacterEncodingFilter</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>
<filter-mapping>
         <filter-name>CharacterEncodingFilter</filter-name>
         <url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
         <filter-name>CharacterEncodingFilter</filter-name>
         <url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
         <filter-name>CharacterEncodingFilter</filter-name>
          <url-pattern>*.do</url-pattern>
</filter-mapping>

4.uri方式传递参数乱码

  方法:修改tomcat下的server.xml文件

  把<Connector port="8088" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

   设置为<Connector port="8088" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="utf-8"/>
        修改完需要重启tomcat

5.Properties文件乱码


^^^^^^希望大家补充一下哈
           
 

你可能感兴趣的:(java,spring,tomcat,jsp,MyEclipse)