jsp页面中使用include标签进行包含静态HTML页码乱码有关问题
jsp页面中使用include标签进行包含静态HTML页码乱码问题
jsp页面中使用include标签进行包含其他页面:
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>哈哈</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 哈哈 <jsp:include page="header.html" flush="true"></jsp:include> </body> </html>
header.jsp:
<!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> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> </head> <body> * </body> </html>
直接访问header.jsp的时候页面显示正常,但是访问index.jsp的时候 index中的文字显示正常,而被包含的header中的文字却都是中文乱码。
而我们已经把编码都设置统一的UTF-8了,还是会出现乱码。
解决办法:
1、把所有的页面的编码全都改成中文的GBK编码,可以正常。
2、所有的页面,不论是静态的还是动态的,全都用jsp文件写,即把.html改成.jsp即可。
3、
在项目的web.xml里面放入这串代码就可以了:
<!-- 控制jsp:include的编码 --> <jsp-config> <jsp-property-group> <description> Special property group for JSP Configuration HTML example. </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>