JSP 解决illegal to have multiple occurrences of contentType with different values错误

在一个项目jsp文件中包含了另一个jsp文件(Common.jsp),执行出现如下错误:

2015-5-11 16:37:04 org.apache.catalina.core.ApplicationDispatcher invoke
严重: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /include/Common.jsp(1,2) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html; charset=UTF-8, new: text/html; charset=utf-8)
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.Java:40)

Common,jsp的页面时没有错误的,原来是包含页面与被包含页面的@page指令里面的contentType不一致,仔细检查两个文件第一行的 @page,

包含页面的是: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

被包含页面: <%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>

果然contentType="text/html;和charset=utf-8"之间多了一个空格,把两个文件第一行的@page内容改为一致,再执行程序,运行通过。

总结:被包含页面和包含页面必须都加上@page 头内容这个asp正好相反的,而且值要一致

如果你是 <%@ include file="/WEB-INF/jsp/common/head.jsp"%>
那么改为<jsp:include page="/WEB-INF/jsp/common/head.jsp" >

如果你是<jsp:include page="/WEB-INF/jsp/common/head.jsp" >
那么改为<%@ include file="/WEB-INF/jsp/common/head.jsp"%>

原文地址:https://www.cnblogs.com/latter/p/5482131.html