selective include statements
Hi,
I'm working on a website that requires different include statements depending on the user's selection (selection box)
this is what i have:
<%if (selected == 1)
{%><%@include file="page1.jsp"%>
<% }
else if (selected == 2)
{%><%@include file="page2.jsp"%>
<%}
else if (selected == 3)
{%><%@include file="page3.jsp"%>
<%}
this works....but when I include common files in page1.jsp, page2.jsp and page3.jsp, Java throws an error "Error: Duplicate declaration of field".
for example, if page1.jsp and page2.jsp both includes page4.jsp.
Is there a way to get around this without having to include page4.jsp in the selection page? I don't want to include unneccessary files, especially if there are a lot of selections and only two of them need the page4.jsp file.
I'm new at Java and JSP so I'm not sure if my code above is correct.
Thanks a lot for your help!!
[1015 byte] By [
blue8173] at [2007-9-26 4:06:06]

That is because <%@ include> causes the contents of the included file to be put in place, in the includer JSP, by the page transalator/compiler.
Try using <jsp:include> instead. This tag performs a request-time include rather than a translation-time include.
Thanks for your answer....
<%if (selected == 1)
{%><jsp:include page="page1.jsp"><%
<% }
else if (selected == 2)
{%<jsp:include page="page2.jsp"><%
<%}
else if (selected == 3)
{%<jsp:include page="page3.jsp">
<%}
I get this error:
javax.servlet.ServletException: Exception thrown running XT: Expected "" to terminate element starting on line 351.
org.xml.sax.SAXParseException: Expected "" to terminate element starting on line 351.
at com.sun.xml.parser.Parser.fatal(Parser.java:2817)
at com.sun.xml.parser.Parser.fatal(Parser.java:2811)
at com.sun.xml.parser.Parser.maybeElement(Parser.java:1406)
at com.sun.xml.parser.Parser.content(Parser.java:1499)
..........//error goes on...
can you please tell me what this error means?
thanks
i see my mistake before was that i forgot to put the backslash in. However, using the <jsp:include...> tag, the included file cannot read the variable declared outside of the file.
for example:
int var = 1;
<%if (selected == 1)
{%><jsp:include page="page1.jsp"/><%
<% }
else if (selected == 2)
{%<jsp:include page="page2.jsp"/><%
<%}
else if (selected == 3)
{%<jsp:include page="page3.jsp"/>
<%}
and page1.jsp, page2.jsp needs "var"....
is there anyway to get around this? thanks
You can nest a <jsp:param name="someName" value="someValue"> tag within the body of <jsp:include> tag. Then in the included JSP, use request.getParameter("someName") to retreive the value of that parameter.
You may complain that with the above scheme one can only pass parameters that have string values. Well, you can also do request.setAttribute("someName", someObject) in the includer JSP and do request.getAttribute("someName") in the included JSP.