注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

love3400wind的blog

like

 
 
 

日志

 
 
 
 

tomcat 乱码  

2011-01-05 14:25:53|  分类: IT |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1.用<%@ include file="a.jsp" %>引入文件时,a.jsp中的中文全成乱码了,而单独打开时,却不是乱码。

解决方法:在web.xml中添加如下代码:

<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>GBK</page-encoding>
   <scripting-invalid>false</scripting-invalid>
   <include-prelude></include-prelude>
   <include-coda></include-coda>

   <description>Special property group for HTML Configuration HTML example.</description>
   <display-name>JSPConfiguration</display-name>
   <url-pattern>*.html</url-pattern>
   <el-ignored>true</el-ignored>
   <page-encoding>GBK</page-encoding>
   <scripting-invalid>false</scripting-invalid>
   <include-prelude></include-prelude>
   <include-coda></include-coda>
  </jsp-property-group>
 </jsp-config>

 

2.当form提交时,不管以get还是以post方式提交,所以的request.getParameter("Name"),中文全是乱码了

解决方法:

get方式提交时:修改%TOMCAT_HOME%/conf/server.xml ,添加URIEncoding参数

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               URIEncoding="GBK" />

红色为解决乱码而添加的参数

post方式提交时:添加一个过滤器类(%TOMCAT_HOME%\webapps\examples\WEB-INF\classes\filters 目录下的三个java文件就是例子,web.xml中有配置):

package com.filter;

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 EncodingFilter 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);  
   }
   
  }
  
  chain.doFilter(request, response);  
 }

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


}

当然,完成后,还要在web.xml中配置,添加如下内容:

<!-- post 方式提交乱码特殊处理 -->
 <filter>
  <filter-name>Encoding</filter-name>
  <filter-class>com.idfounder.heyx.filter.EncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>GBK</param-value>
   <!--gbk或者gb2312或者utf-8-->
  </init-param>
  <init-param>
   <param-name>ignore</param-name>
   <param-value>false</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>Encoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
注意红色部分为<url-pattern>标签,不是<servlet-name>,配错了就不管用了

  评论这张
 
阅读(503)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017