Reputation: 19071
I'm facing confusion with how JSP include tag works. What's happening is that the base page's body tag has id attribute of included JSP.
base.jsp
<%@page contentType="text/html" 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 Page</title>
</head>
/* base jsp has no id for body */
<body>
<h1>Hello World!</h1>
<jsp:include page="include.jsp" />
</body>
</html>
include.jsp
<%@page contentType="text/html" 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 Page</title>
</head>
/* here included jsp has body with ID specified */
<body id="includeBody">
<h1>Hello include!</h1>
</body>
</html>
When I go to base.jsp page, the firebug finds only one body tag which is ok but indicates that the HTML BODY tag has id attribute of "includeBody"
outcome:
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>JSP Page</title>
</head>
/* why this body has id ??? */
<body id="includeBody">
<h1>Hello World!</h1>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>JSP Page</title>
<h1>Hello include!</h1>
</body>
</html>
The base.jsp has body without id specified and include.jsp has body with id. Looks like if I don't specify the id in the body of base.jsp, included jsp body id will be applied. Is this a bug? Or should I remove body from included.jsp (no that can be true). Or I could add id to the base.jsp body so it won't get replaced with included body.
Upvotes: 2
Views: 8202
Reputation: 82986
It's not a bug. It's required behaviour of HTML5 parsers. What happens is that if the parser encounters a second body start tag, then it looks at each attribute of that second body start tag and if the body element (created by the first body start tag) doesn't have an attribute with the same name, then the attribute gets added to the body element.
The same thing happens if the parser encounters any further body start tags, accumulating the attributes from each onto the body element, with the first instance any particular attribute taking precedence.
So, for example if you have
<body id="realBody">
<h1>Hello include!</h1>
<body id="invalidSecondBody" class="error"></body>
<body id="invalidThirdBody" class="errorAgain" style="color:red"></body>
</body>
then, in the DOM, the body element will have id="realBody" and class="error" and style="color:red".
The rule in HTML5 is specified here: http://dev.w3.org/html5/spec/tree-construction.html#parsing-main-inbody , search down for A start tag whose tag name is "body"
and it's the last paragraph in that block.
... could [I] add id to the base.jsp body so it won't get replaced with included body
According to the above rules, that is what would happen, yes. However, you shouldn't be including one entire html document in another like that in the first place. The accumulated markup is horribly invalid and you are at the mercy of all kinds of strange parser rules like the one above.
Upvotes: 2