Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to include a part of html code in to a jsp page?

 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a peculiar problem.In my web app there are around 4to 5 jsp pages and i want all these pages to share a common border on the top and left, while the other contents of the page will vary.how can i achive this.i am aware abt the include directive of jsp, but i think it can be used to include only complete html file and not part of the code.to be more precise, i want to include half of the final code from some other page while i want to generate the rest of the code dynamically.
how can i do this?
Subbu
 
ken chou
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I believe you can include some other jsp pages(not only html pages) to your jsp pages.
 
Robert Brunner
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you do some code refactoring and then include the refactored code snippets wherever you need them. A more elegant technique might be to turn each snippet into a JavaBean, and then *use* them where you need them.
 
Bart Tubalinal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Subbu Aswathanarayan:
Hi,
I have a peculiar problem.In my web app there are around 4to 5 jsp pages and i want all these pages to share a common border on the top and left, while the other contents of the page will vary.how can i achive this.i am aware abt the include directive of jsp, but i think it can be used to include only complete html file and not part of the code.to be more precise, i want to include half of the final code from some other page while i want to generate the rest of the code dynamically.
how can i do this?
Subbu

I did something similar to this. If the first file is called "top.jsp" and the second file is called "left.jsp" you can code it like this (or something similar):
<!-- index.jsp file -->
<html>
<head>
<title></title>
</head>
<body>
<table width="100%">
<tr>
<td colspan="2">
<jsp:include page="top.jsp" flush="true" />
</td>
</tr>
<tr>
<td>
<jsp:include page="left.jsp" flush="true" />
</td>
<td>
<% String filename = request.getParameter("file"); %>
<jsp:include page="<%= filename %>" flush="true" />
</td>
</tr>
</table>
</html>
Of course, this can be modified in any way. In fact, in your "top.jsp" file, you can put everything contained in this file (index.jsp) up until the include action for the top.jsp.
so top.jsp can be:
... some jsp/html code ..
<html>
<head>
<title></title>
</head>
<body>
<table width="100%">
<tr>
<td colspan="2">
------------------
and index.jsp can just be:
<jsp:include page="top.jsp" flush="true">
</td>
</tr>
</table>
... and the rest ...
I hope this helped.
 
Rodrigo de la Parra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently developing an e-commerce site for a big client that requires to handle multiple audiences and each one has a different header and navigation but they share the same functionality so what we do is to use the include JSP tag and we load the audience specific HTML pieces using the information provided in the session. The files are just plain text with .inc extensions and HTML code inside and it works pretty nice given that we have like 40 audiences and 100 include files for each one.
 
Tom Ebert
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Subbu,
the <%@ include file="somePieceOfCode.html" %> directive does not care for the somePieceOfCode.html-file to be some complete html-file. Both files will be merged at compilation time into one servlet.
------------------
 
Robin Kimzey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subbu,
No, you do NOT have to include a complete HTML file when using the #INCLUDE functionality in .JSP pages. It is a direct copy of the included file contents into your JSP page.
Enjoy,
Robin
 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanks a lot for ur help.
It works.
Subbu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic