Search code examples
jspincludeserver-sideinclude-pathserver-side-includes

Is there a way to load a variable page with a java <%@ include?


I would like to do something like:

<%@ include file="links/"+linkFile %>

Is there any way to do something like this? It's for a jsp page, but I don't want to use <jsp:include> because I want access to all the information on the parent page.

Thanks!


Solution

  • TL;DR: No.

    See this page for a comparison between them. This thread has more details regarding why what you want to do isn't possible (and a potential reason why you're trying to do it like that ;)

    It sounds like the pages were architected "PHP-style". In a clean JSP site most of the dynamic work is done in Java code, generally a servlet or framework-specific class (action, controller, whatever).

    When I say a "scoped" variable, I mean an attribute in the page, request, session, or application context. In this case you'd probably just put things in the request scope so they'd be available everywhere that has access to the current request.

    Once a variable is in scope in can be retrieved using JSP EL syntax, like ${myVariable}.

    Without more details it's difficult to know what the best way to go about cleaning things up would be.