3

From the Java EE tutorial:

If the resource is static, the include method enables programmatic server-side includes.

If the resource is a web component, the effect of the method is to send the request to the included web component, execute the web component, and then include the result of the execution in the response from the containing servlet.

I'm not quite sure what they mean by "programmatic server-side includes" and how those differ from the web component case.

I mean, regardless of the resource I include, I pass a request/response object tuple to it and get some side-effects that I may or may not communicate to the client, right?

Could somebody please elaborate on this?

User1291
  • 6,231
  • 5
  • 34
  • 75
  • Perhaps a servlet (as an example)? `request.getRequestDispatcher("/anotherservlet").include(request, response); ` – ernest_k Jan 09 '19 at 14:09
  • @ernest_k I was under the impression a servlet was a web component? – User1291 Jan 09 '19 at 14:10
  • Check this https://www.codejava.net/java-ee/jsp/jsp-include-directive-examples – Ivan Jan 09 '19 at 14:15
  • Most servlets are web components, but that is not necessarily the case (eg `java.servlet.Servlet` is a generic protocol-independent solution, while `javax.servlet.http.HttpServlet` is specifically for handling http requests. – Mark Rotteveel Jan 09 '19 at 15:54

1 Answers1

2

You've omitted the heading and text preceding your quotation. These provide important context for the comments you're asking about:

Including Other Resources in the Response

It is often useful to include another web resource, such as banner content or copyright information, in the response returned from a web component. To include another resource, invoke the include method of a RequestDispatcher object:

include(request, response);

Thus, when the comments continue with

If the resource is static, the include method enables programmatic server-side includes.

they are characterizing the effect of the preceding code snippet, not introducing some new concept. A "programmatic server-side include" in this context is invoking the include() method of a RequestDispatcher() associated with a static resource. It has the effect of including the resource associated with the dispatcher, inline in the response being prepared. As such, this is "server-side" because it is all done by the server, transparently to the client, as opposed to the client having to make a separate request for the included resource.*

The distinction between the static and web component cases is about the resource associated with the RequestDispatcher on which that include() method is invoked -- i.e. what resource is to be included -- not about the component whose code contains the method invocation. A static resource is exactly a resource that can be identified by a URL that is not associated with a web component. Normally this means it corresponds to a file. The file content could be anything, but a common use is for it to comprise an HTML fragment, such as a header or footer shared by many web pages.

I mean, regardless of the resource I include, I pass a request/response object tuple to it and get some side-effects that I may or may not communicate to the client, right?

It's more accurate to view it as passing a request and response object to the RequestDispatcher. In the event that the dispatcher is associated with a static resource, no, the request and response objects are not presented to that resource (itself) because it has no mechanism for receiving or manipulating them. Instead, the servlet engine in which the code runs manipulates the response object as it decides is appropriate.

In the event that the target resource is a web component, yes, it will be able to read data from the provided request, manipulate the request and its context, and manipulate the provided response, all at its discretion. Generally, it cannot distinguish this case from the one in which it is accessed directly. But no, the component invoking include() has at best limited control over what is communicated to the client via that mechanism.


*For more information on the history of and inspiration for the "server-side includes" part of the term, consult Wikipedia.

John Bollinger
  • 121,924
  • 8
  • 64
  • 118
  • I still don't get it, I'm afraid. Let's start with the question I originally asked: What IS a "programmatic server-side include"? Then, do you have an example of a sensible inclusion of a static resource? I suppose a database would be a static resource, but I wouldn't "include" a database, I would include a servlet that communicates with the database. – User1291 Jan 09 '19 at 15:14
  • Thank you. Can I convince you to include (no pun intended) these comments in your answer? Happy to accept it, then. :) – User1291 Jan 09 '19 at 15:31