摘 要:Java Web网站开发中经常出现中文乱码问题,初学者在解决此类问题时非常耗费时间。以eclipse开发环境为例,针对初学者在Java Web开发中经常出现的字符编码问题,分别从常见字符编码种类、设定方式、不同页面类型代码编写差异方面进行总结,并对常见中文乱码产生原因进行分析并给出相应的解决方案,为解决中文乱码问题提供相应的思路。
关键词:Java Web;字符编码;中文乱码;eclipse
中图分类号:TP311 文献标识码:A 文章编号:2096-4706(2020)01-0074-03
Abstract:In the development of Java Web,the problem of Chinese messy code is often occered,which is very time consuming. Taking the eclipse development environment as an example,aiming at the character coding problems that often appear in the Java Web development for beginners,this paper summarizes the common types of character coding,setting methods,and the differences in code writing of different page types,and analyzes the causes of common Chinese character scrambling and gives the corresponding solutions,so as to provide the corresponding ideas for solving the problem of Chinese character scrambling.
Keywords:Java Web;character encoding;Chinese messy code;eclipse
0 引 言
Java语言是一種面向对象的语言,它具有平台无关性、较好的安全性、简单易用等优点,基于Java语言的Web开发越来越受到广大编程爱好者的青睐。在Java Web应用程序中有多种类型的页面构成,如HTML、JSP、Servlet等,而不同的页面类型在字符编码设定方式有所差异,初学者易混淆它们之间的区别,导致开发中出现中文乱码问题。接下来,从常见的字符编码方式、设定方式、不同页面代码编写区别方面分别进行介绍。
1 几种常见的字符编码方式
计算机采用二进制进行信息存储,任何语言在计算机存储时必须进行编码。在Java Web开发中,JSP和Java是基于字节流的,也就是通用字符集Unicode,使其具有良好的跨平台性,但其在编译成class文件或Servlet文件,或是与其他类型文件进行交互时必存在转码,转码期间的编码或解码不正确必产生乱码,以下是常用的字符编码方式:
(1)Unicode编码。Unicode基于通用字符集,它为每种语言中的每个字符都设定了统一并且唯一的二进制编码,能实现跨平台、跨语言的转换,Java语言内部即使用2个字节的Unicode编码。……