Hello. First of all i must say that im a noob with .jsp programming, so pleas be indulgent :) ..

String somepage = "client.jsp";
<%@ include file="menu.jsp" %> // this works
//How to add variable somepage in file=
<%@ include file="somepage" %>

Thanks !

Dani AI

Generated

The include directive in JSP is handled at translation/compile time and therefore requires a literal file name. That is why the runtime expression approach suggested by did not work for : the directive cannot evaluate a variable at request time. For dynamic selection of which JSP to include, use a runtime include instead.

A simple, common approach is the JSP include action with EL (the included page must be available as an attribute or EL variable):

<jsp:include page="${somepage}" flush="true" />

Set that somepage attribute from a servlet or in the JSP (JSTL example shown):

request.setAttribute("somepage", "/client.jsp");
<c:set var="somepage" value="/client.jsp" />

If working inside a servlet or needing programmatic control, the servlet API RequestDispatcher include is an alternative:

RequestDispatcher rd = request.getRequestDispatcher(somepage);
rd.include(request, response);

Troubleshooting notes: EL requires a container/JSP version that supports it (JSP 2.x+). Use absolute paths (beginning with "/") when intending to resolve from the webapp root. The static include directive is still appropriate when you need compile-time merging of fragments; choose the runtime include when the included page is chosen dynamically.

Recommended Answers

All 2 Replies

Try this, although I am not sure since I have never used the "include" tag like this: <%@ include file="<%= somepage%>" %>

No this doesnt works but thank you anyway. Im just a little confused-isnt there anybody who had such problem :) ..

Anybody else ... Thanks!

Nice greatings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.