Question

I am writing JSP code and I have included sling taglibs as

<sling:defineObjects/>

What is the best way to include an external file ?

There are two options:

  1. Include file: <%@ include file="global.jsp" %>
  2. Use replaceSelectors as <sling:include replaceSelectors="global" />

Both are working fine except I am not getting sling request object in global.jsp when I use include file option. I want to know more details.

Was it helpful?

Solution

So this works only under a certain condition where you have a global file in the same location as the jsp that you are writing.

Your first option is including the file global.jsp inline at the time of compilation. This is then fixed and if you change something in global.jsp it won't be pulled in unto you recompile this servlet.

The second option you listed is taking the existing resource request and changes out the selector, if any, with "global." It then runs this through the sling resolution process to determine which file to include.

Since the global.jsp, in the same directory, will match your request, it's included.

See Sling URL Resolution for more details, in particular the Examples section which will show how the selector would indicate the jsp to use.

Note: as I mentioned this works, because the resolver resolves the global in this case. If the global.jsp you were attempting to add was in a different directory structure it would not work.

Now to answer your specific question as to which is better. It depends. If you are developing in sling then the sling include is considered the better option as it responds better when the identified resource is changed.

I should note that the specific usage of sling include is not the normal usage for global.jsp as the global file is usually in a fixed position and you want to use that specific version.

The more common usage of the sling include is to specify the path and/or resource.

The interesting usage of your option 2 is that the actual file that is resolved is relational to the component and is determine through the sling resolution, so you can extend or override this component and include a new global.jsp that would be utilized.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top