After some time of playing, i was able to get the result you wanted, but i was able to resolve it only using scriplets. Suppose that:
Your Foo.jsp is: (Page which will be included in myJsp1 and myJsp2)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.HashSet" %>
<%String hasInclude = "itHas";
request.setAttribute("hasInclude", hasInclude); %>
<%HashSet<String> hasIncludes = new HashSet<String>();
request.setAttribute("hasIncludes", hasIncludes);%>
<h2>Inside include</h2>
Your myJsp1:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.util.HashSet" %>
<h1>Inside jsp1</h1>
<%
String hasInclude = (String)request.getAttribute("hasInclude");
HashSet<String> hasIncludes = (HashSet<String>)request.getAttribute("hasIncludes"); %>
<%if (hasIncludes==null||!hasIncludes.contains(hasInclude)) { %>
<jsp:include page="include.jsp" />
<%if (hasIncludes==null) {
hasIncludes = new HashSet<String>();
hasIncludes.add((String)request.getSession().getAttribute("hasInclude"));
} else {
hasIncludes.add(hasInclude);
}
request.setAttribute("hasIncludes", hasIncludes);%>
<%}%>
Your myJsp2: (same code as myJsp1 before include)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.util.HashSet" %>
<h1>Inside jsp2</h1>
<%
String hasInclude = (String)request.getAttribute("hasInclude");
HashSet<String> hasIncludes = (HashSet<String>)request.getAttribute("hasIncludes"); %>
<%if (hasIncludes==null||!hasIncludes.contains(hasInclude)) { %>
<jsp:include page="include.jsp" />
<%if (hasIncludes==null) {
hasIncludes = new HashSet<String>();
hasIncludes.add((String)request.getSession().getAttribute("hasInclude"));
} else {
hasIncludes.add(hasInclude);
}
request.setAttribute("hasIncludes", hasIncludes);%>
<%}%>
And finally the main.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="myJsp1.jsp" />
<jsp:include page="myJsp2.jsp" />
</body>
</html>
The output of main will be:
Inside jsp1
Inside include
Inside jsp2