jsp中文乱码问题整理(部分转载他人)

来源:互联网 发布:黑客论坛源码 编辑:程序博客网 时间:2024/05/16 06:17

所有涉及编码的都转为utf-8
1、添加过滤器:
  1)Java代码:过滤器类是一种特殊的Servlet类,可以在请求发给特定的Servlet类处理之前,先执行过滤器类中的方法,将客户端发送的所有请求中的字符编码转换为utf-8;
 package demo.common;

 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;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 public class CharacterEncodingFilter implements Filter
 {
  private FilterConfig filterConfig;

  // 初始化信息,保存filterConfig对象
  public void init(FilterConfig filterConfig) throws ServletException
  {
   this.filterConfig = filterConfig;
  }

  // Process the request/response pair
  public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain filterChain)
  {
   try
   {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    String encoding = filterConfig.getInitParameter("encoding");
    httpRequest.setCharacterEncoding(encoding);
    httpResponse.setCharacterEncoding(encoding);
    filterChain.doFilter(request, response);
   } catch (ServletException sx)
   {
    filterConfig.getServletContext().log(sx.getMessage());
   } catch (IOException iox)
   {
    filterConfig.getServletContext().log(iox.getMessage());
   }
  }

  // 释放资源
  public void destroy()
  {
   this.filterConfig = null;
  }
 }

  2)过滤器类需要在web.xml中配置使用,在web.xml中增加过滤器的配置:
 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
   <filter-name>encodingFilter</filter-name>
   <filter-class>demo.common.CharacterEncodingFilter</filter-class>
   <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
   </init-param>
  </filter>

  <filter-mapping>
   <filter-name>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>

  <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/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>demo/index.jsp</welcome-file>
  </welcome-file-list>
 </web-app>

2、JSP头部声明:在jsp文件开头部分指定响应数据的编码字符集,控制了输出给客户端的文本字符集为utf-8;
 <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

3、HTML代码声明:在jsp代码中的HTML代码、或纯HTML网页文件中的<head>标记中给出下面定义,设定了HTML使用的字符集为utf-8:
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

4、设定数据库字符集:如果在程序中需要使用数据库读写数据,则应该对数据库的字符集进行设置;

5、其它和外界的交互:当应用需要读写文件或者操作XML,那么应该将这些文本字符集设定为utf-8;

6、服务器字符集设定:对服务器字符集进行设定,Tomcat服务器字符集设定如下:
  在Tomcat服务器中的conf目录下的server.xml文件中找到<Connector>标记,做如下设定(见最后):
 <Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true"  port="8080" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" URIEncoding="utf-8">

7、对于Struts中文资源文件有特殊设置:进行转码,把文本内容转换为utf-8字符集。在Java中可以使用native2asii命令进行转码,native2ascii是jdk提供的命令,如下:
 native2ascii -encoding gbk ApplicationResources_tmp.properties ApplicationResources_zh.properties
  其中,encoding指定被转码的源文件的初始字符集。
或者直接:native2ascii a.txt b.txt也行;
8、<%@ include file="*.jsp" %> 的中文乱码问题
在 Tomcat 5.0.x 中,Tomcat 支持了 JSP 2.0 的规格,同时也支持了部分 J2EE 1.4 的规格,在 J2EE 1.4 的规格中,有关 JSP 的部份,有一个 <jsp-config> 的 XML Tag,这个 XML 区块用来定义与 JSP 相关的特殊属性,包含采用的 taglib 与 以下说明的 <jsp-property-group> ,而解决 include 档中文问题的方法就定义在 <jsp-roperty-group> 中。

在当前应用系统的web.xml里加入jsp-config代码:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<jsp-config>
    <jsp-property-group>  
        <description>     
            Special property group for JSP Configuration JSP example.  
        </description>  
        <display-name>JSPConfiguration</display-name>  
        <url-pattern>*.jsp</url-pattern>  
        <el-ignored>true</el-ignored>  
        <page-encoding>GB2312</page-encoding>  
        <scripting-invalid>false</scripting-invalid>  
        <include-prelude></include-prelude>  
        <include-coda></include-coda>  
       
  <description>     
            Special property group for JSP Configuration JSP example.  
        </description>  
        <display-name>JSPConfiguration</display-name>  
        <url-pattern>*.html</url-pattern>  
        <el-ignored>true</el-ignored>  
        <page-encoding>GB2312</page-encoding>  
        <scripting-invalid>false</scripting-invalid>  
        <include-prelude></include-prelude>  
        <include-coda></include-coda>
    </jsp-property-group>
</jsp-config>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>  

... ...

</webapp>

9、更为便捷的做法 
在一个编码为utf-8的页面中,使用<jsp:include>包含另一个.jsp/.html文件时,被包含的页面单独浏览正常,但被包含后就会遇到乱码问题。解决的办法是,在每个被包含的页面开始加上下面一行<% page contentType="text/html;charset=utf-8" %>这个方法可以解决jsp include jsp的中文乱码问题。也就是说,被包含的页面必须改成.jsp,哪怕它的内容只有静态html,否则的话还是会出现乱码,如何解决include .html文件中文乱码的问题,这个我也没查到,网上讲的试了一下不行,希望解决了的同学给我回复一下。
10、
11、

原创粉丝点击