欢迎来到010在线作文网!

总结解决JSP开发Web程序中文显示三种方法

总结 时间:2021-08-31 手机版

总结解决JSP开发Web程序中文显示三种方法

  方法一:最简单也是用的最多的方法 <%@ page language="java" pageEncoding="GBK" %>

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

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

  方法二:使用过滤器

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

  编写一个SetCharacterEncodingFilter类。

  importjava.io.IOException;

  importjavax.servlet.Filter;

  importjavax.servlet.FilterChain;

  importjavax.servlet.FilterConfig;

  importjavax.servlet.ServletException;

  importjavax.servlet.ServletRequest;

  importjavax.servlet.ServletResponse;

  publicclassSetCharacterEncodingFilterimplementsFilter{

  protectedStringencoding=null;

  protectedFilterConfigfilterConfig=null;

  protectedbooleanignore=true;

  publicvoidinit(FilterConfigfilterConfig)throwsServletException{

  this.filterConfig=filterConfig;

  this.encoding=filterConfig.getInitParameter("encoding");

  Stringvalue=filterConfig.getInitParameter("ignore");

  if(value==null)

  this.ignore=true;

  elseif(value.equalsIgnoreCase("true"))

  this.ignore=true;

  else

  this.ignore=false;

  }

  publicvoiddoFilter(

  ServletRequestrequest,ServletResponseresponse,FilterChainchain)

  throwsIOException,ServletException{

  //TODO自动生成方法存根

  if(ignore (request.getCharacterEncoding()==null)){

  Stringencoding=selectEncoding(request);

  if(encoding!=null)

  request.setCharacterEncoding(encoding);

  }

  chain.doFilter(request,response);

  }

  publicvoiddestroy(){

  //TODO自动生成方法存根

  this.encoding=null;

  this.filterConfig=null;

  }

  protectedStringselectEncoding(ServletRequestrequest){

  return(this.encoding);

  }

  }

  然后再web.xml加上

  <!--SetCharacterEncoding-->

  <filter>

  <filter-name>SetCharacterEncoding</filter-name>

  <filter-class>mon.SetCharacterEncodingFilter</filter-class>

  <init-param>

  <param-name>encoding</param-name>

  <param-value>UTF-8</param-value>

  </init-param>

  </filter>

  <filter-mapping>

  <filter-name>SetCharacterEncoding</filter-name>

  <url-pattern>/*</url-pattern>

  </filter-mapping>

  <!--SetCharacterEncoding-->

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

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

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

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

  <Connectordebug="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方法在处理编码时是一样的。

【总结解决JSP开发Web程序中文显示三种方法】相关文章:

1.web前端开发年终总结

2.web前端开发年终总结范文

3.web开发简历范文

4.总结jsp实训的内容

5.jsp实训心得总结

6.win10 14393.105微信无法显示图片解决方法

7.web开发简历模板

8.WEB开发员个人简历范文


本文来源http://www.010zaixian.com/shiyongwen/zongjie/4168163.htm
以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请给我们来信(zaixianzuowenhezi@gmail.com),我们会及时处理和回复,谢谢.