devxlogo

Virtual Includes

Virtual Includes

Question:
I’ve been told that you cannot have dynamic virtual includes inJSP. Is this true? For example, is something like this going tocause problems?

<% if (condition.equals("true")) { %><% } else { %><% } %>

Answer:
JSP is still relatively new, so I won’t claim to know all of its insand outs, but it looks to me like you’re mixing Server Side Includes(SSI) syntax with JSP code. It is indeed possible to perform dynamicJSP includes, but you must use the element. Thiselement allows you to include both static and dynamic resources. Theincluded resource is specified with the page attribute. The flushattribute specifies whether or not to flush the output buffer,although its utility is questionable now that the JSP 1.1specification explicitly disallows a value of false. The followingexample demonstrates how to perform the type of include you are askingabout. It generates a random integer every time the page is accessed.If the number is odd, it loads file1.jsp, if the number is even, itloads file2.jsp.

<%! java.util.Random random =	 new java.util.Random(System.currentTimeMillis()); %><% if (random.nextInt() % 2 != 0) { %><% } else { %><% } %>
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist