Can we replace servlet context object and try cach block with Server Side includes?

326 Views Asked by At

I have the below code snippet in JSP : Can we replace this with Server Side include ? Specifically, the call to the Servlet Context object and the try - catch block.

Please advise :

Thanks in advance.

<%
                String includeURL = "";
                if (getServletConfig().getServletContext().getServletContextName().startsWith("internal")) {
                    includeURL = "/test/index.inc";
                } else {
                    includeURL = "/testone/index.inc";
                }
                try {
                    %><jsp:include page="<%= includeURL %>" flush="true" /><%
                } catch (Throwable e) {
                    out.println("<!-- Could not include file - ERROR: " + e.toString() + " -->");
                }
            %>
2

There are 2 best solutions below

3
On

Yes, you can do it. You only need to keep in mind that it won't work if the index.inc is not a plain vanilla HTML document, but contains JSP tags and scriptlets and so on. They will not be evaluated by SSI.

0
On

Can we use the below SSI code as the replacement for the initial part in the JSP code fragment in question ?<!--#if expr="getServletConfig().getServletContext().getServletContextName().startsWith("internal")" --> <!--#set var="includeURL" value="/ctx/hub_inside/support/index.inc" --> <!--#else --> <!--#set var="includeURL" value="/ctx//EMEA/main/Intranet/Inside/hub_inside/support/STAGING/index.inc" --> <!--#endif -->