您现在的位置: 圆点视线 >> 教程 >> 网页设计 >> javascript技巧 >> 文章正文
[图文]解决JSP开发Web程序中文显示三种方法           ★★★
解决JSP开发Web程序中文显示三种方法
作者:Arting36…  文章来源:Arting365.com  点击数:  更新时间:2007-8-10 16:22:15 
 页面功能:【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口 【字体:
本文介绍解决JSP开发Web程序中文显示的三种方法……
   方法一:最简单也是用的最多的方法
  <%@ 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); 
  } 
  }

[1] [2] 下一页

 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 文章录入:Rainsion    责任编辑:Rainsion  【字体:
 
  • 上一篇文章:

  •  
  • 下一篇文章:
  • 最新文章
    相关文章
    几种常用禁止修改输入框的方法
    网页脚本语言:深入学习JavaScript中的函数
    JS中将字符串转为XML并读取对象的值
    JavaScript在Avant浏览器中的妙用
    详细讲解JavaScript脚本语言的 document 对
    Javascript获得当前网页页面详细地址的实现
    用JS得到字符串中出现次数最多的字母
    网页制作Javascript经典小技巧总结
    javascript数组
    javascript对象
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    发表评论:
     姓  名: 评 分: 1分 2分 3分 4分 5分
     评论内容: ·严禁发表危害国家安全、政治、黄色淫秽等内容的评论。
    ·用户需对自己在使用本网服务过程中的行为承担法律责任
    ·本站管理员有权保留或删除评论内容。
    ·评论内容只代表机友个人观点,与本网站立场无关。
     
    最 新 推 荐
     
    百度主题推广
    最 新 热 门