Forums Register Login

Including HTML 'snipets' in JSP

+Pie Number of slices to send: Send
What's the best way to include pieces of HTML code in a JSP, in a modular way?
For instance, suppose I have several standard page components -- say a header, footer, nav bar, etc. -- that I want to add to every page. I can put each of these in a separate JSP file and include each of those files in my main JSP.
But what if I have lots of smaller standard pieces of HTML -- say 4 or 5 lines -- that I include in the header, etc.? What's the best way to catalog these snipets in a central place (for easy update) and pull them into the appropriate JSP?
With ASP, using VBScript, you can create a function for each snipet and 'call' that function where you want to include the HTML code. What's the equivalent practice for JSP?
Thanks!
+Pie Number of slices to send: Send
Hi,
There are 2 ways to do this:
1. The include directive:
<%@include file="filename" %>
This file, as is, is included with the jsp at translation to servlet time. All variables in the file are available to the JSP so they must have unique names. This is faster but the JSP must be redeployed for changes to take effect.
2. The include action:
<jsp:include page="pagename.jsp" flush="true" />
This includes the processed response from the file in question so the variables it has are not visible to the calling JSP. The response is processed on every request so it's a bit slower and is only reccomended for content that changes very frequently. Any changes in the included file take effect on the next request - no recompiling is necessary.
Hope that helps,

------------------
Terry Doyle
Sun Certified Programmer for Java 2 Platform
+Pie Number of slices to send: Send
Many thanks for your reply Terry.
I use 'include' for larger pieces of HTML, but I was searching for a technique for smaller pieces. I use a lot of these to compose pages for larger sites since it allows true modularity -- I can make one change that affects all the pages.
I suppose custom tags would also work, but many of the pieces are pure HTML. The constant calls to output the HTML from Java code seem tedious.
Any further thoughts?
Dennis
+Pie Number of slices to send: Send
HI,
How about defining a utility class with public strings that you can print out anywhere in the web app?
OR
with public methods that return a string from RandomAccessFiles you have on the server in a special directory:
that way you can change the files anytime you want and adding more html snippets is just a matter of inserting a new method and re-compiling..
Hope those ideas are not too silly,

------------------
Terry Doyle
Sun Certified Programmer for Java 2 Platform
+Pie Number of slices to send: Send
hi,
the most powerfull mechanism I've seen are templates. The article at javaworld doesn't mention it is a struts components..
read this http://www.javaworld.com/javaworld/jw-09-2000/jw-0915-jspweb.html
and get the custom tags & other info at http://jakarta.apache.org/struts/struts-template.html
It would be good to check out struts as a whole though It's a MVC (model-view-controller) framework for web applications.
Dave
+Pie Number of slices to send: Send
Yes, Struts looks nice but I don't think the Template architecture used in Struts is the answer to what Dev is asking. Its the solution to another problem. However, Struts does deal with Internationalization also which gives me another idea.
You could use Java's resource bundles in combination with a custom tag to achieve what you want. Although strictly speaking, resource bundles are meant for Internationalization and Localization it could be put to work for your means and into the bargain you could Internationalize your web application at the same time!
There are two types Resource bundles to my knowledge
1) List Resource Bundle which lets you store the key/values in a Java class and...
2) Property Resource Bundles which let you store the key/values in a text file e.g
...
snippets.welcomemessage=Welcome!
snippets.copyright=copyright mr.moo 2001
...
You can store any text you want and that includes HTML so you could have:
...
snippets.welcomemessage=<font size="30">Welcome!</font>
snippets.copyright=<b>copyright mr.moo 2001</b>
...
in the properties file.
This has the advantage of storing all those "snippets" in one file in a central place. Alternatively, you could have several different files depending on categorization - thats up to you.
Then, in combination with your custom tag (which hides accessing the resource bundle etc.) in your JSP's it would look something like this
<%@ taglib uri='/WEB-INF/tlds/snippets.tld' prefix='snippets' %>
...
<snippets:include key='snippets.welcomemessage'>
...
<snippets:include key='snippets.copyright'>
Internationalization then becomes a SNAP! you just create different properties files for the different languages and make your custom tag access the right resource bundle depending on the Locale of the browser etc. BUT THATS GETTING OFF THE POINT!
On short reflection, Resource bundles might be overkill or not appropriate for what you want - its just an idea! It depends on what kind of "snippets" you are talking about. As said by someone above, you could implement it using a class which has all snippets you want defined in it as String literals and then use a custom tag as above to access them in JSP's
Anyway, I've only read a few books and haven't actually written a JSP or Servlet yet so its best to ignore everything I've just said!
Andre.
+Pie Number of slices to send: Send
 

Originally posted by Dev Cunningham:
With ASP, using VBScript, you can create a function for each snipet and 'call' that function where you want to include the HTML code. What's the equivalent practice for JSP?
Thanks!


Well, Ignore my last post if you want.
What's the equivalent practice for JSP? Well you can create a "method" for each snippet and "invocate" that method where you want to include the HTML code!!! Just as you do with ASP.
+Pie Number of slices to send: Send
Great answers!
After some reflection, I'm going to try a combination approach, including restructuring the "snipet" paradigm into larger chunks. One problem with the code I'm working with is that the overall page structure is hard to follow, so a restructuring is probably in order.
+Pie Number of slices to send: Send
I think the taglib approach is about the cleanest, since tags are practically HTML themselves. However, if you want HTML snippets that can be modified by nontechnical people you'd want to abstract the HTML to be plugged in from the tag's Java code.
One easy way to do this is to place the HTML in a properties file and have the tag code simply retrieve the property and output it. Since you can do properties in a straight text file, snipper mainenance is as easy as using a text editor - like so:
pageheader=<h1>FlybyNightCo - A Consolidated Amalgamated Industry</h1>
pagefooter=<hr><i>Copyright © 2001 Shifty Enterprises, Inc.</i>
It's not actually that hard to create a tag component. It's really just a specialized form of JavaBean.
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2851 times.
Similar Threads
one jsp design question
interpreting javascript with RESTful servlet
Why is the include JSP Standard Action like SSI?
HTML display
Design choice for dispatching request to view
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:24:54.