选择显示字体大小

解决jsp开发web程序中的中文问题


  这段时间经常看到有人问到web开发中怎么中文总是?号。原因其实很简单,因为大家大多用的是tomcat服务器,而tomcat服务器的默认编码为 iso-8859-1(西欧字符)。就是因为iso-8859-1(西欧字符)编码造成了我们经常看到?号。

  方法一:最简单也是用的最多的方法。

  <%@ page language="java" pageencoding="gbk" %>

  或者<%@ page contenttype="text/html;charset=gbk";>这里可以用gb2312或者gbk,只是gbk比gb2312支持跟多的字符。

  这个方法用于jsp页面中的中文显示。

  方法二:使用过滤器。

  过滤器使用主要针对表单提交,插入数据库的数据都是?号。这也是应为tomcat不按request所指定的编码进行编码,还是自作主张的采用默认编码方式iso-8859-1编码。

  编写一个setcharacterencodingfilter类。

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 init(filterconfig filterconfig) throws servletexception {
  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
   this.ignore=false;
 }

 public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {
 // todo 自动生成方法存根
 if (ignore (request.getcharacterencoding() == null)) {
  string encoding = selectencoding(request);
  if (encoding != null)
   request.setcharacterencoding(encoding);
 }
 chain.dofilter(request, response);
}

public void destroy() {
 // todo 自动生成方法存根
 this.encoding = null;
 this.filterconfig = null;
}

protected string selectencoding(servletrequest request) {
 return (this.encoding);
}
}

  然后再web.xml加上

<!-- set character encoding-->
<filter>
 <filter-name>set character encoding</filter-name>
 <filter-class>com.struts.common.setcharacterencodingfilter</filter-class>
 <init-param>
  <param-name>encoding</param-name>
  <param-value>utf-8</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>set character encoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
<!-- set character encoding-->

  使用过滤器的好处很多,特别是项目之中。

  而且在使用国际化时就更有用了,只要在页面指定 <%@ page language="java" pageencoding="utf-8" %>,服务器就会根据本地locale来显示正确的字符集。

  所以我特别推荐使用过滤器。

  方法三:修改tomcat的server.xml文件中uriencoding。

<connector debug="0" acceptcount="100" connectiontimeout="20000" disableuploadtimeout="true"
port="80" redirectport="8443" enablelookups="false" minsparethreads="25" maxsparethreads="75"
maxthreads="150" maxpostsize="0" uriencoding="gbk" >
</connector>

  这个方法主要针对从url中获取字符串的问题。

  在tomcat5.0及以上版本,post和get方法在处理编码时有所不同。如果你在url中获取中文就会出现?号。但在tomcat4.1版本没有问题,因为tomcat4.1的post和get方法在处理编码时是一样的。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons