paul
paul

Reputation: 13471

Check jsp is not include twice

Does anyone know how to check if a jsp already includes another jsp twice?

mainJSP.jsp[
   include myJSP.jsp[

       <jsp:include page="foo.jsp"/>
    ]
    include myJSP1.jsp[

       <jsp:include page="foo.jsp"/>//This should never happend
    ]
 ]

Any ideas?.

Upvotes: 0

Views: 935

Answers (2)

Ruan Mendes
Ruan Mendes

Reputation: 92294

A jsp file generates a regular java method that adds text to the output stream so you can return early. See How to stop processing a JSP early?.

Try the following in any file that you don't want included twice.

foo.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.util.HashSet" %>
<%
// Just be careful that the attribute name is unique to this file
if (!request.getAttribute("foo.jsp") == null) {
   return;
}
request.setAttribute("foo.jsp", true)
%>
<h1>Contents of the file</h1>
...

To minimize the problem of having multiple includes with the same attribute name, you can use something like the following to generate a unique attribute name in every jsp. See Get current filename in JSP

String __jspName = this.getClass().getSimpleName().replaceAll("_", ".")

This technique is kind of similar to the popular C approach to prevent double inclusion

#ifndef FILE_NAME
#define FILE_NAME
... file contents
#endif

Upvotes: 1

drgPP
drgPP

Reputation: 936

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

Upvotes: 2

Related Questions