Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Include 1

Status
Not open for further replies.

Ricjd

Programmer
Sep 12, 2002
104
GB
Hey

I would like to have just one single JSP page with the menus and banners on, but then the content to be added in through an 'include' statment, or something to that effect.

I would like to know how to do this through passing a parameter of the page i want to include and then using that to get the content.

Does anyone know how this is possible.

Thanking you in advance

Rick
 
Save the generic code which you want as the include in a file such as generic.inc or something, and then add this line in your other JSP pages :


<%@ include file="generic.inc" %>
 
No i want the generic code in one file, and then include the none generic code into that one, depending on ehich parameters have been passed to the page.
 
It might help if you gave an example or pseudo code of what you want to do ...

For example do you want to change the the image in an <img src> tag for example depending on the parameter supplied at runtime ?

<img src="<%=request.getParameter("image")%>">
 
OK here's some pusedo code, but please keep in mind that i have no idea how this is done in JSP so this might lead you more astray


<%
String myFilename = pageNameThatHasBeenPassed;
%>

<HTML>
<HEAD>
<TITLE> Main Page </TITLE>
</HEAD>

<BODY>

/*code for menu */

<%@ include file=myFilename %>

</BODY>
</HTML>

Ok now this obvioly doesn't work, and i've tried the jsp:include tag but that doesn't seem to work. heres the code for that.

<jsp:include page="{<%=myFilename%>}" flush="true" />

So am I doing something wrong or am i the wrong track completly??
 
The only way that I know that this is possible is like so :

Code:
<html>
<body>

This is main page.
<br>
<%
	String incPage = request.getParameter("incPage");
	if (incPage == null) incPage = "";
	
	if (incPage.equals("inc1.inc")) {
%>
		<%@ include file="inc1.inc" %>
<%		
	} else if (incPage.equals("inc2.inc")) { 
%>
		<%@ include file="inc2.inc" %>
<%
	} else {
		// nothing or default maybe
	}
	
%>

</body>
</html>
 
Bummer, I thought that might be the case.

I was wishing for a more compact way...

Ah well I guess it will have to do.

Thanx sedj.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top