2

Does include in JSP works similarly like in PHP? I mean by this just static copy paste before an execution?

<jsp:include page="header.jsp" /> 
//vs.
<?php include("header.jsp"); ?>


Only thing I'm afraid of is that I have for example Index.java = servlet which redirect or forward something to index.jsp, on index.jsp I call something like:

<c:if test="${sessionScope.user != null }"> //etc.


If I move this part of code to the header.jsp which will be same part of code on every page, if it will just hard copy the few of lines which are in header.jsp to the index.jsp where I include it before execution.

If I write it different way if this two options are same:
Before moving the header out to separate file:
index.jsp:

<!-- some code here -->
<!--header code -->
<!-- rest of code index.jps -->

After moving the header out to separate file:
header.jsp:

<!--header code -->

index.jsp:

<!-- some code here -->
<!-- header isn't here it was moved to header.jsp and I include that file
     where I can easily change for every page at one place -->
<jsp:include page="header.jsp" /> 
<!-- rest of code index.jps -->


To summarize my question I wanna know if my expectation of behavior of include in JSP is ok which mean if these two examples work exactly same. ("Suppose that the files exist and are accessible").

informatik01
  • 16,038
  • 10
  • 74
  • 104
user1097772
  • 3,499
  • 15
  • 59
  • 95
  • 1
    There are some differences in implementation, but in behavior yes. – Elliott Frisch Jan 18 '14 at 19:56
  • @ElliottFrisch This answer is enought for me. If you can just copy it to answer I will mark this question as answered. – user1097772 Jan 18 '14 at 20:31
  • Useful reading: [What's the difference between including files with JSP include directive, JSP include action and using JSP Tag Files?](http://stackoverflow.com/a/14763794/814702) – informatik01 Jan 20 '14 at 04:32

1 Answers1

2

There are some differences in implementation, but in the behavior the answer is yes. You can read more about it here.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249