0

There is good discussion on Stack Overflow about their difference found here. It roughly says that <jsp:include> is dynamic, while <@ include> is static. Yet, I have some questions.

1. st question

Say I use <jsp include> to include header.jsp file. It would run like this: Container compiles my JSP file and during runtime it calls header.jsp, which would also get compiled. So now it uses its content as a response to my JSP file (and combines it). But how is it dynamic? What happens if I change header.jsp? As it had already been compiled, it won't see any changes. So does <jsp: include page="header.jsp"/> simply compiles header.jsp every single time to maintain its dynamic behavior? Or there is different approach that "tells" Container to recompile it?

2. nd question

Whatever the answer on first question, I understand that it is dynamic, no matter how does it do it. Well, until I came across this picture from Head First JSP and Servlets.

enter image description here

Does this now mean that even <@ include> can be dynamic, as well? If I am making an app in Tomcat5 (friendly container), why would I care if some other Container (older versions) aren't friendly? I mean, I would use one Container and stick to it, not move my app around other Containers, right? So why I don't simply always use <@ include>, let Container "deal" with any changes in included file, plus I don't get performance hit of using RequestDispatcher every single request (like <jsp:include has).

Stefan
  • 969
  • 6
  • 9

1 Answers1

1

The <%@ include %> directive is static because it takes the content of the included file and simply "dumps it" in the place of the directive. This is done at translation time, so the content becomes part of the parent JSP as if you wrote it there yourself.

The <jsp:include> action is dynamic because you "call it" by sending it the request and it can generate a response that gets included into the output of the parent JSP. What makes it dynamic is that you can send it parameters with <jsp:param> sub-elements. So basically, instead of dumping some content in the page, it acts like a function that you can invoke with parameters. For this reason, the things you include with it are not just content but it's dynamic content resulting from using a template that you can invoke with various parameters at various moments of time.

As for your second question, this isn't related to the <%@ include %> directive or <jsp:include> action themselves, this is how the server works (and I find it silly that they would call this "a friendly container", but well, the books tries to be friendly, I guess). Tomcat implementers decided that they can make it detect when a page included at compile time has changed and then recompile the JSP that included it, but other servlet container may have not. The specification doesn't direct the implementers to handle this, so the behavior is not portable to other servlet containers.

If you want to take advantage of this fact, that's your choice, assuming you run the same server in all your environments and you can guarantee this fact won't change, a statement which can be valid in a development environment, or if you want to play with things, but most likely will not be valid in a professional setting running user facing production code, where using proprietary things that limit portability is a frowned upon practice.

Bogdan
  • 23,890
  • 3
  • 69
  • 61
  • Thanks Bogdan! So let me clear this up. Bottom line is that both `` and `<@ include>` can detect that included file has changed. In that manner, Container will recompile included file, so next time user requests something, it will get latest updated file. I should always choose `<@ include>` because it calls it only once, while `` calls it every single time user requests something. Unless I want to use params to create something different (dynamic) each time - then I choose ``. Did I get it right? – Stefan Jan 10 '21 at 22:11
  • And one more thing, if you know it. Looking on a picture I sent, it says that only newer Containers can detect if included file from `<@ include>` has changed. But what about ``? Does Container notice changes in `` in every Container, or just newer ones? – Stefan Jan 10 '21 at 22:18
  • 1
    *[...] both `` and `<@include>` can detect that included file has changed*. No! The action and the directive cannot do that. What that image above says is that the server can notice an included file has changed and it can choose to rebuild and recompile the code that uses it. The JSP spec does not say that servers need to do this. Some server implementations chose to do it because it might be a feature people would want, for convenience in development, for ex. But you should not expect all servers to do that. It's not behavior portable between different servers. – Bogdan Jan 11 '21 at 15:52