项目开发中jsp乱码的处理方法

       JSP乱码解决方案(作者:余新龙 email:yxl_allen@163.com QQ:81705066)

在开发J2EE系统是,经常遇到jsp,数据库出现乱码问题,其解决方法分如下四个步骤:
1 首先检查jsp页面最上面的pageEncoding,将其设置为:GBK
<%@ page language="java"  pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK"%>

2 我们开发工具用eclipse,检查ecpilpse的默认字体格式,方法是:
Project->Properties->Info 查看Text file encoding是否为GBK ,如果不是将其设置为GBK

3 给工程新建一个包,包名为filters,编写一个类SetCharacterEncodingFilter,次类在包filters里面
SetCharacterEncodingFilter实现Filter接口 ,该类的代码如下:
package 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() {
  // TODO Auto-generated method stub
         this.encoding = null ;
         this.filterConfig = null ;
 }

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  // TODO Auto-generated method stub
     if (ignore || (request.getCharacterEncoding() == null)) {
               String encoding = selectEncoding(request);
            if (encoding != null)
                  request.setCharacterEncoding(encoding);
          }
  
       // Pass control on to the next filter
        chain.doFilter(request, response);

 }

 public void init(FilterConfig filterConfig) throws ServletException {
  // TODO Auto-generated method stub
     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);
  
      }

}
4 配置web.xml ,具体web.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_2.xsd">
  <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>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>EncodingServlet</servlet-name>
    <servlet-class>EncodingServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>EncodingServlet</servlet-name>
    <url-pattern>/servlet/EncodingServlet</url-pattern>
  </servlet-mapping>

   <taglib>
     <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
     <taglib-location>/WEB-INF/c.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-template.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
   </taglib>
  
     <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GBK</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值