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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating HTML tags (like CGI.pm does)

Status
Not open for further replies.

fishiface

IS-IT--Management
Feb 24, 2003
687
GB
I'm a novice java programmer writing scripts to generate HTML pages. All the examples I've found use code like
Code:
strHTML += "<a href=\"" + strURL + "\">";
strHTML += strLinkText + "</a>\n";

I've been writing in perl for many years and the standard module, CGI.pm, allows me to use syntax like
Code:
$HTML .= a( {-href=>$strURL}, $strLinkText );

This is much easier to read, much easier to code (you don't have to keep track of closing tags) and lets you virtually forget about encoding, escaping or quoting problems, to which the first example above would be particularly prone.

Is there a java class (I hope that's the correct terminology) that I can use to perform the same function - i.e. generate well-formed HTML tags with their attributes and arguments properly escaped and/or encoded? I've checked the FAQ and done several searches on this forum without success. Several search engines were similarly unforthcoming.

If there is no such standard class, would it be possible to abuse an XML generator class or can anyone point me towards a third-party solution?

I'm still struggling to find a java community equivalent to CPAN so please forgive me if I'm missing something obvious.

Yours,


fish

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
I'm so very much "in at the deep end" that I'm not entirely sure, but I believe so. I'm working in an IBM IDE called WebSphere Studio and I'm working with objects that look like this:
Code:
<%@ taglib uri="[URL unfurl="true"]http://www.oo.it/oo/oo-taglib"[/URL] prefix="oo" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<oo:page title='Tabs Dashboard'>
	<oo:pane>
		<oo:part type='execframe' hasBorder='no' hasTitle='no' contentURL='Tabs_part.jsp'>
			<oo:parameter name='comp' source='user' value='comp'/>
			<oo:parameter name='user' source='url' value='u'/>
		</oo:part>
	</oo:pane>
</oo:page>
and have the file extension .jsp. The more complex ones contain java code between <% and %> tags and some of ths code generates quite complex html. These are the areas that I wish to simplify.

Yours,

fish

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Ok - this is JSP

It looks like that perl code is building a hyperlink. Java the language itself doesn't have a function that will do what you want but you can write one.


There's a number of different ways to do what you want to do..


JSP can be used a number of different ways as well. You can do coding with it where you mix HTML and Java code. In this scenario you would put html and then put <%= myLinkMakingFunction("link") %>


You can do CGI like stuff in servlets where you have something like the above example. so it would be like

html += myFunkyLinkMakingFunction("link");



And then you can do it with TagLibs. The pages you see are using Taglibs. Each of those <oo:something> tags you see actually calls out to some Java code that's located wherever
the uri=" is located. There might be an <oo:link param="ref"> tag or <oo:href param="ref"> but I'm not sure what is available in those libraries. You'll have to look.

There are some JSP standard tag libraries that you can use and they have <jsp:link param="ref">.

To use these you will need to declare the taglib location in the web.xml file, declare the uri name at the top of the page, and then use the jsp tags in your html page.

It's really much to explain in one post - grab a book on JSP sometime and that can help you out.


Also keep asking questions about this topic on here - we're happy to answer them.
 
Thanks for a really helpful and detailed reply. It's making a lot more sense now.

I've tried mixing "raw" HTML and bits of HTML-generating script in <% %> tags and that all seems to work. I've found the tag libraries and understand what they do, although I can't get at the source.

I think that what I need to do is write a tag-generator class with methods(s) in a .jar file and use syntax like
Code:
<%@ page import='myclass.*' %>
to make it available to my JSPs.

The tag-generating methods will need to take variable numbers of arguments but I've found links at java2s.com which describe the varargs implementation so I can probably get that working.

I'd rather not write a method for each HTML tag as there are a quite a few and most of them would be practically identical. In perl I'd use AUTOLOAD which is a way of trapping unimplemented method calls in a catch-all method. This gets passed the desired method name so that it can work out what to do.

I can't find anything similar in java. I then thought of generating the method bodies programatically and using "eval" to incarnate them but java doesn't seem to have an eval either.

I could always pass the tag name as the first argument but that seems like giving up - alhtough I may well head that route initially through pressure of time.

The only other way of doing it that I've thought of is to implement the method as an exception catcher. The tag name is used as a method of a class which doesn't implement it, an exception is thrown; the code for which, I hope, can retrieve the tag name from the exception itself and generate the required text.

I'd be intrested in your opinion on the plausibility of this approach and on any possible performance implications. Of course: if I'm on a wild goose chase I'd be very grateful if you pointed me in a more productive direction.

Yours,


fish

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Look at Jakarta Tag Lib stuff

Download the Tag Lib (and associated libraries it requires) and place it in your /lib files directory for your project. Add a line in your web.xml that looks like the lines pointing to the OO taglibs but make it point to the new JSTL lib you just downloaded. Then you can use the functions in that tag lib such as encodeUrl and that will do what you want. You also get some powerful perl like string manipulation as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top