struts tomcat 中文乱码解决

 
再也不用

strRet = new String(src.getBytes("ISO_8859_1"),"UTF-8");

配置下边

 web.xml


<? xml version="1.0" encoding="Shift_JIS" ?>

<! 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 > Set Character Encoding </ filter-name >
< filter-class > htcommoninfo.SetCharacterEncodingFilter </ filter-class >
< init-param >
< param-name > encoding </ param-name >
< param-value > UTF-8 </ param-value >
</ init-param >
< init-param >
< param-name > enable </ param-name >
< param-value > true </ param-value >
</ init-param >
</ filter >
< filter-mapping >
< filter-name > Set Character Encoding </ filter-name >
< servlet-name > Action Servlet </ servlet-name >
</ filter-mapping >
< filter-mapping >
< filter-name > Set Character Encoding </ filter-name >
< servlet-name > Faces Servlet </ servlet-name >
</ filter-mapping >
< filter-mapping >
< filter-name > Set Character Encoding </ 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 > 2 </ param-value >
    
</ init-param >
    
< init-param >
      
< param-name > detail </ param-name >
      
< param-value > 2 </ param-value >
    
</ init-param >
      
< init-param >   
   
< param-name > javaEncoding </ param-name >   
   
< param-value > GBK </ param-value >   
   
</ init-param >  
< load-on-startup > 2 </ load-on-startup >
     
</ servlet >

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

  
< taglib >
    
< taglib-uri > /tags/struts-bean </ taglib-uri >
    
< taglib-location > /WEB-INF/struts-bean.tld </ taglib-location >
  
</ taglib >

  
< taglib >
    
< taglib-uri > /tags/struts-html </ taglib-uri >
    
< taglib-location > /WEB-INF/struts-html.tld </ taglib-location >
  
</ taglib >

  
< taglib >
    
< taglib-uri > /tags/struts-logic </ taglib-uri >
    
< taglib-location > /WEB-INF/struts-logic.tld </ taglib-location >
  
</ taglib >

  
< taglib >
    
< taglib-uri > /tags/struts-nested </ taglib-uri >
    
< taglib-location > /WEB-INF/struts-nested.tld </ taglib-location >
  
</ taglib >

  
< taglib >
    
< taglib-uri > /tags/struts-tiles </ taglib-uri >
    
< taglib-location > /WEB-INF/struts-tiles.tld </ taglib-location >
  
</ taglib >

  
<!--
  <taglib>
    <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
    <taglib-location>/WEB-INF/c.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
    <taglib-location>/WEB-INF/fmt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
    <taglib-location>/WEB-INF/sql.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
    <taglib-location>/WEB-INF/x.tld</taglib-location>
  </taglib>
  
-->

</ web-app >

 
package  htcommoninfo;

import  javax.servlet.Filter;
import  javax.servlet.FilterConfig;
import  javax.servlet.ServletException;
import  javax.servlet.ServletRequest;

import  javax.servlet.ServletResponse;
import  javax.servlet.FilterChain;
import  java.io.IOException; 


public   class  SetCharacterEncodingFilter  implements  Filter  {

    
protected FilterConfig filterConfig;
    
protected String encodingName;
    
protected boolean enable;

    
public SetCharacterEncodingFilter() {
    
this.encodingName = "UTF-8";
    
this.enable = false;
    }


    
public void init(FilterConfig filterConfig) throws ServletException {
    
this.filterConfig = filterConfig;
    loadConfigParams();
    }


    
private void loadConfigParams() {
//    encoding
    this.encodingName = this.filterConfig.getInitParameter("encoding");
//    filter enable flag
    String strIgnoreFlag = this.filterConfig.getInitParameter("enable");
    
if (strIgnoreFlag.equalsIgnoreCase("true")) {
    
this.enable = true;
    }
 else {
    
this.enable = false;
    }

    }


    
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) 
throws IOException, ServletException {
    
if(this.enable) {
    request.setCharacterEncoding(
this.encodingName);
    }

    chain.doFilter(request, response);
    }


    
public void destroy() {
    }


    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值