在jsp中使用<%@include file=”in.html” %>导入html页面时,如果html页面里有中文,就会产生乱码。检查jsp文件和html文件的编码,编码一致,都是统一使用的utf-8,检查生成的Servlet类文件,发现里面直接就乱码了。
jsp页面内容:
- <%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>
- <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
- <html>
- <head>
- <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
- <title>测试JSP的include指令</title>
- </head>
- <body>
- <%@include file=“in.html” %><br/>
- <%@include file=“in1.jsp”%><br/>
- <%@include file=“in2.html” %>
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试JSP的include指令</title>
</head>
<body>
<%@include file="in.html" %><br/>
<%@include file="in1.jsp"%><br/>
<%@include file="in2.html" %>
</body>
</html>
in.html文件内容:
- <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
- <html>
- <head>
- <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
- <title></title>
- </head>
- <body>
- 我是in.html文件的内容
- </body>
- </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
我是in.html文件的内容
</body>
</html>
生成的Servlet内容:
- out.write(“<!DOCTYPE html PUBLIC \”-//W3C//DTD HTML 4.01 Transitional//EN\” \”http://www.w3.org/TR/html4/loose.dtd\”>\r\n”);
- out.write(”<html>\r\n”);
- out.write(”<head>\r\n”);
- out.write(”<meta http-equiv=\”Content-Type\” content=\”text/html; charset=UTF-8\”>\r\n”);
- out.write(”<title></title>\r\n”);
- out.write(”</head>\r\n”);
- out.write(”<body>\r\n”);
- out.write(”ææ¯in.htmlæ件çå容\r\n”);
- out.write(”</body>\r\n”);
- out.write(”</html>\r\n”);
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("<title></title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("ææ¯in.htmlæ件çå容\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");
通过上面生成的Servlet内容可以看出,在将jsp文件编译成java类这一过程就出现了乱码,问题肯定是编码一致的,而设置编码的有两个:pageEncoding和contentType,这两个属性的区别如下:
pageEncoding是jsp文件本身的编码,是指定web容器将jsp编译成java文件时采用什么编码读取jsp文件。
contentType的charset设置的编码是指服务器发送给客户端时的内容编码。
而客户端访问一个jsp文件要经过如下三个阶段:
1、(第一次访问时)web容器将jsp编译成java文件,这个阶段编译器会根据pageEncoding设置的编码读取jsp文件,翻译成统一的utf-8的Servlet类,如果pageEncoding设置错误或未设置,编译出来的java文件就会出现中文乱码。
2、由javac将java源码编译成class字节码,javac用utf-8编码读取java源码,编译成utf-8编码的二进制文件。3、web容器载入class字节码文件,将内容输出结果到客户端,这一过程内容的编码为contentType设置的编码。
由此可见,是由于pageEncoding设置问题导致翻译jsp时乱码。有两种方式处理:
方法一:在每个引入的html文件设置pageEncoding编码,即在html添加<%@page pageEncoding=”UTF-8”%>,尽管html不能识别该指令,但通过include指令引入时该指令就能起作用了,如下:
- <%@page pageEncoding=“UTF-8”%>
- <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
- <html>
- <head>
- <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
- <title></title>
- </head>
- <body>
- 我是in.html文件的内容
- </body>
- </html>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
我是in.html文件的内容
</body>
</html>
方法二:在web.xml里统一配置pageEncoding的编码,在web-app标签里添加如下配置:
- <jsp-config>
- <jsp-property-group>
- <description>html encoding</description>
- <display-name>JSPConfiguration</display-name>
- <url-pattern>*.html</url-pattern>
- <el-ignored>true</el-ignored>
- <page-encoding>UTF-8</page-encoding>
- <scripting-invalid>false</scripting-invalid>
- <include-prelude></include-prelude>
- <include-coda></include-coda>
- </jsp-property-group>
- </jsp-config>
<jsp-config>
<jsp-property-group>
<description>html encoding</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.html</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
</jsp-property-group>
</jsp-config>
方法一和方法二原理是一样的,都是通过设置pageEncoding编码来指定jsp将html文件include时使用的编码。方法一和方法二任选一种即可,如果同时使用需要注意两个地方设置的pageEncoding编码必须一致,否则将会报如下编码不一致的错误:
- org.apache.jasper.JasperException: /in.html (line: 1, column: 2) Page-encoding specified in jsp-property-group (UTF-8) is different from that specified in page directive (GBK)
org.apache.jasper.JasperException: /in.html (line: 1, column: 2) Page-encoding specified in jsp-property-group (UTF-8) is different from that specified in page directive (GBK)
</div>
转载链接:https://blog.youkuaiyun.com/gnail_oug/article/details/51707061