中文乱码问题

作者在JSP页面请求到ACTION中遇到中文问题,经尝试将原系统还原并重新添加修改内容后问题解决。总结原系统解决乱码方案,包括采用国际化、filter编码转换等,还提及在server.xml文件中设置编码。最后提醒开发时要沉着冷静,弄清原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      晕了一上午碰到了从JSP页面请求到ACTION中出现了中文问题。找了很久也不知道到底问题出现在那,郁闷呀。10.gif总觉得很奇怪,因为我是原来的系统(中文传递没有问题)中加入一些页面,也添加了一个Action,可它有中文问题了。想了很久也不能解决,真是noidea.gif呀。最后,我干脆把原来的系统还原,然后重新在系统中添加所要修改的东东,居然没有问题了。14.gif
总结一下原系统解决乱码的方案:
   1)采用国际化。这样能解决页面的中文乱码问题。

    a)把页面中所出现的中文写到属性文件中,例如:messages_zh.properties,
None.gifshipment.system = 出货小系统
None.gifshipment.jsnum 
= 条码
None.gifshipment.stylenum 
= 款式
    
                 b)native2ascii把文字转换成ascii码,
None.gifD:\project\shipment\web\WEB-INF\classes>native2ascii messages_zh.properties mess
None.gifages_zh_CN.properties

    c)页面统一采用utf-8编码
<%@ page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

     d)在web.xml文件中引入JSTL与WEBWORK集成的类
None.gif <!-- Define the basename for a resource bundle for I18N -->
None.gif    
<context-param>
None.gif        
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
None.gif        
<param-value>messages</param-value>
None.gif    
</context-param>
None.gif
       <!-- 采用filter编码转换-->
None.gif    
<filter>
None.gif                
<filter-name>Set Character Encoding</filter-name>
None.gif                
<filter-class>org.appfuse.web.SetCharacterEncodingFilter</filter-class>
None.gif        
</filter>
None.gif
None.gif        
<filter-mapping>
None.gif                
<filter-name>Set Character Encoding</filter-name>
None.gif                
<url-pattern>/*</url-pattern>
None.gif        
</filter-mapping>
None.gif

      
              e)filter类把请求的统一转换成utf-8,(在2.1.7版本以后自己处理好了)如下:
None.gifpackage org.appfuse.web;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//**
InBlock.gif * Created by IntelliJ IDEA.
InBlock.gif * User: Administrator
InBlock.gif * Date: 2005-8-11
InBlock.gif * Time: 13:17:00
InBlock.gif * To change this template use File | Settings | File Templates.
ExpandedBlockEnd.gif 
*/

None.gifimport java.io.IOException;
None.gif
None.gifimport javax.servlet.Filter;
None.gifimport javax.servlet.FilterChain;
None.gifimport javax.servlet.FilterConfig;
None.gifimport javax.servlet.ServletException;
None.gifimport javax.servlet.ServletRequest;
None.gifimport javax.servlet.ServletResponse;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class SetCharacterEncodingFilter implements Filterdot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void init(FilterConfig arg0) throws ServletException dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * Servlet编码
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        request.setCharacterEncoding(
"utf-8"); 
InBlock.gif        chain.doFilter(request,response);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void destroy() dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
    
                ee)在web-inf\classes中的webwork.properties文件(可能这个就能取代上面的,有待测试)中加入
None.gifwebwork.custom.i18n.resources=messages
None.gifwebwork.locale
=zh_CN
None.gifwebwork.i18n.encoding
=GBK
 
                f)在页面中还需添加JSTL标签,当然在classpath下要加入相关的类包
None.gif<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
   
   g)现在在页面中就可以采用jstl的EL来引入了
None.gif<fmt:message key="shipment.shipment"/>
        
        2)把tomcate下面的conf文件夹下的server.xml文件中加入 URIEncoding="GBK",有了上面的可能这一个也是多余的,如:
None.gif <Connector 
None.gif
port="8080"               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
None.gif               enableLookups
="false" redirectPort="8443" acceptCount="100"
None.gif               debug
="0" connectionTimeout="20000" 
None.gif               disableUploadTimeout
="true" URIEncoding="GBK" />
   
 
  现在想想,当时可能我太性急,有点慌,盲目,所以没有注意到原来我在系统中加入了filter来转换编码(utf-8),而在我新添加的页面中没有注意到这一点,所以怎么处理还是有乱码。所以在以后的开发当中一定要沉着冷静专注,要弄清其所以然,才能顺利的解决好问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值