这里是一个具体的例子:

现在因为浏览器对UTF-8的支持,我们可以通过在源文件、请求、响应中都使用unicode编码方式,来轻松达到处理国际化和字符编码问题的目标。

以我们使用的tomcat4.1.2为例,过程如下:

1、编写JSP页面时:在每个JSP页面在页首都要增加一行:

在编辑JSP页面时,一定要确保JSP文件以unicode的方式保存,目前几乎所有的编辑器都有以unicode编码保存或将文件内容转换成unicode的功能。

2、增加一个用来声明request的CharacterEncoding的类SetCharacterEncodingFilter.java;

SetCharacterEncodingFilter的这个类主要的作用就是:把request在从页面刚提交到server端的时候的encoding声明为我们想要的encoding,通过调用request的方法setCharacterEncoding (String encoding) 来改变,这样可以使request的从客户端传过来的时候,按我们在web.xml (在第二点可以讲到) 中配置的encoding来对提交的数据编码。

3、修改web.xml文件,配置一个filter来过滤全部url请求,通过第二步中的类,声明所有url请求的编码类型未UTF-8。

在web.xml文件中加上以下这段:

Set Character Encoding

org.kyle.web.sample.SetCharacterEncodingFilter

encoding

UTF-8

Set Character Encoding

/*

在上面这段文字中“org.kyle.web.sample.SetCharacterEncodingFilter”指定步骤2中的类的位置,“ UTF-8”指定我们希望声明的request的编码类型,“/*”指定这个filter的适用范围(这里指的是全部url请求)。

同时注意二个问题:

1:servlet的版本必需是支持request.setCharacterEncoding(String encoding)这个方法才行,也就是在serlvert2.3以上。

2:控制面板区域设置的当前代码页属性必需设定为"936 (GBK)",如果是"437(OEM-United States)"它处理文字的时候是8-bit,而中文和日文等是16-bit。所以在显示和处理时它把中文的前8位给截掉,这样就会出现乱码问题。

附:SetCharacterEncodingFilter源文件

package org.kyle.web.sample;

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;

import javax.servlet.UnavailableException;

public class SetCharacterEncodingFilter implements Filter

{

/**

* The default character encoding to set for requests that pass through

* this filter.

*/

protected String encoding = null;

/**

* The filter configuration object we are associated with.  If this value

* is null, this filter instance is not currently configured.

*/

protected FilterConfig filterConfig = null;/**

* Should a character encoding specified by the client be ignored?

*/

protected boolean ignore = true;

/**

* Take this filter out of service.

*/

public void destroy()

{

this.encoding = null;

this.filterConfig = null;

}

/**

* Select and set (if specified) the character encoding to be used to

* interpret request parameters for this request.

*

* @param request The servlet request we are processing

* @param result The servlet response we are creating

* @param chain The filter chain we are processing

*

* @exception IOException if an input/output error occurs

* @exception ServletException if a servlet error occurs

*/

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain)

throws IOException, ServletException

{

// Conditionally select and set the character encoding to be used

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

{

String encoding = selectEncoding(request);

if (encoding != null)

request.setCharacterEncoding(encoding);

}

// Pass control on to the next filter

chain.doFilter(request, response);

}

/**

* Place this filter into service.

*

* @param filterConfig The filter configuration object

**encoding

* UTF-8

*

*/

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 if (value.equalsIgnoreCase("yes"))

this.ignore = true;

else

this.ignore = false;

}

/**

* Select an appropriate character encoding to be used, based on the

* characteristics of the current request and/or filter initialization

* parameters.  If no character encoding should be set, return

* null.

*

* The default implementation unconditionally returns the value configured

* by the encoding initialization parameter for this

* filter.

*

* @param request The servlet request we are processing

*/

protected String selectEncoding(ServletRequest request)

{

return (this.encoding);

}

}

serlvert jsp mysql_JAVA基础:Java多语言编码问题解析(2)相关推荐

  1. Java基础—— Java的语言基础

    第二章 Java的语言基础 第一节 字符 1.字符就是各种文字和符号的总称,包括各国的文字.标点符号.图形符号.数字等. 1.1二进制码和字符的对应关系,编码. 1.2字符集市多个字符的集合,不同的字 ...

  2. java设置语言编码_Java多语言编码问题解析

    1.Java编译器在对源文件编译前,会先把源文件转换为unicode编码,因为这个原因,我们在编译时一定要把源文件用的是什么编码方式正确无误的"告诉"编译器. 例如:我们的源文件是 ...

  3. java exif 语言编码_Java读取图片EXIF信息的代码

    Java读取图片EXIF信息的代码 本文实例讲述了Java读取图片EXIF信息的方法.分享给大家供大家参考.具体分析如下: 首先介绍一下什么是EXIF,EXIF是Exchangeable Image ...

  4. jsp需要多少java基础_Java基础——JSP(一)

    注意:访问JSP的过程 如果是第一次访问服务器,则翻译成一个对应的java文件(Servlet).然后,再被编成 .class 文件并加载到内存中. 如果是以后访问,则直接调用内存中的jsp实例,所以 ...

  5. Java基础-Java概述-Java语言概述

    Java工程师知识树 / Java基础 文章目录 Java语言概述 概述: 发展历程 1.发展概述 2.JDK 版本更新历史及技术点 编程开发 编程环境 编程工具 语言特点 1.简单性 2.面向对象 ...

  6. 1,Java语言基础-Java语言概述和必要计算机知识

    Java语言基础 Java语言概述 1,Java语言发展史 Java语言本质 Java是面向对象的高级语言,它是由c和c++发展而来. Java发展语言历史概述 Java 是由 James Gosli ...

  7. java设置中文语言编码_-GWA2 Java版本的i18n/中文编码/乱码问题

    本篇问题域被定义为 -GWA2 (-吉娃兔)的 -Java 版本的多语言的编码/乱码问题,也包括中文的编码和乱码问题.当然,也具有普遍意义,包括所有Java/JSP应用的中文编码.乱码问题. 这次距离 ...

  8. 2,Java语言基础-Java语言基本程序设计知识

    Java语言基础 Java语言基本程序设计知识 1,JavaAPI概述 1.1,什么是API API是指应用程序接口( Application Program Interface, API),故名思意 ...

  9. c语言中大写英文字母所占字节,Java中字符编码和字符串所占字节数 .

    首 先,java中的一个char是2个字节.java采用unicode,2个字节来表示一个字符,这点与C语言中不同,C语言中采用ASCII,在大多数 系统中,一个char通常占1个字节,但是在0~12 ...

最新文章

  1. AI一分钟 | 阿里与南洋理工成立AI联合研究院;传蔚来汽车拟赴美IPO,融资20亿美元
  2. C++ 虚函数在基类与派生类对象间的表现及其分析
  3. GAN生成对抗网络-GAN原理与基本实现-去噪与卷积自编码器01
  4. stl中map函数_map :: empty()函数以及C ++ STL中的Example
  5. php 变量字节大小,PHP 变量
  6. Android开发汇总帖子
  7. Hibernate二级缓存适用场景
  8. RoboWare 下载地址
  9. xp关闭计算机共享,关闭Windows XP系统默认共享四种方法
  10. 《财务报表分析从入门到精通》——读书笔记
  11. VC++2010解决上位机dll报错(load library error)
  12. OpenCV 调用手机摄像头
  13. 从“受精卵”到“独角兽”,有多大概率?
  14. oracle备份提示12560,做standby 数据库时,出现ORA-12560 错误:
  15. 互联网日报 | 2月26日 星期五 | B站月均活跃用户突破2亿;返利网借壳上市获批;理想汽车首次实现季度盈利...
  16. 关于U盘的镜像经营模式
  17. bates chock_2019资生堂中国杯世界花样滑冰大奖赛-冰舞前瞻
  18. 文科生参加计算机竞赛,文科生有保送的吗??
  19. 要想走向比目前更为理想的境况
  20. jQuery 的运行机制(How jQuery Works)

热门文章

  1. Arxiv 论文提交流程——看这篇就够了
  2. php传递数据给javascript
  3. php全局变量GLOBAL
  4. 3D视觉创新应用(三维重建)竞赛作品系列——人体三维精准量测与动作捕捉
  5. 其他算法-高斯混合模型
  6. Mdnice 简洁主题
  7. 远程挂载 NFS 共享目录引发死机问题
  8. R语言绘制Vonoroi图
  9. 零基础入门学习Python(36) 类和对象:继承
  10. MPB:林科院袁志林组-​内生镰刀菌基因组染色体级别组装和注释