Fog Creek Software
Discussion Board




Attn. J2EE Gurus - JSP Custom Tag

I'm writing a JSP custom tag that encapsulates some Javascipt functionality. The problem is that the script code appears within the HTML for each instance of the tag, which is obviously redundant. Is it possible to include a .js file within the tag library JAR and somehow reference that?

Thanks.

John Topley (www.johntopley.com)
Tuesday, April 13, 2004

IT management will cotton on to the J2EE scam one of these days.


Tuesday, April 13, 2004

Hmm, at the risk of increasing the complexity, why not create a tag for the header which just includes the JS once in the head section? Or just put it directly in the JSP page if it only occurs in one place?

This may not be elegant (it isn't) but it would at least solve that maybe... I think what you're saying is can I reference an external .js file internal to a .war, and the answer, i'm pretty sure, is no. Not directly...

Andrew Cherry
Tuesday, April 13, 2004

Sure, move your javascript to an external file, then use your custom tag to add the statements that reference the javascript. 


Tuesday, April 13, 2004

"IT management will cotton on to the J2EE scam one of these days."

Thanks for that.

OK. I just wanted to check if there was a way to keep everything self-contained without the user of the tag having to remember to copy the JS file to the right place. It would appear not.

John Topley (www.johntopley.com)
Tuesday, April 13, 2004

If you're having trouble referencing files (it should be simple, but I'd understand if there were problems), another possible hack would be to store a state variable in the request as an attribute at the end of the tag processing:

pageContext.getRequest().setAttribute("WroteTheJS", "true");

Then check if that's set before you write the javascript.

>>IT management will cotton on to the J2EE scam one of these days.

You'd think that seeing amatuer programmers using PHP and running circles around veteran J2EE development teams they'd realize that, but so far it hasn't happened.  My guess is they'll eventually just latch on to a new scam.

Matt
Tuesday, April 13, 2004

ah matt, I'm sure your frustrated by the complexities of J2EE, so you decide to knock it.  soon enough you'll graduate to something beyond php hacks, and you'll realize that scripting just doesn't work on a large scale.  Don't even begin to tell me you've worked on perl or php projects with 6+ developers, all having to edit each others code.  It just doesn't happen.  Look at the bugzilla source.  Its the nastiest stuff i've ever seen.  I like perl and i'm sure i'd like php for some stuff, but to think that it can do everything j2ee does faster is just plain ignorant.

vince
Tuesday, April 13, 2004

By creating a custom tag that inserts JavaScript, you're fighting a battle on behalf of somebody who is (theoretically) proficient at HTML (and by extension JavaScript) but bad at Java. I say let the client side coders do what they were (supposedly) hired for and reserve custom tags for more Java/data-oriented tasks.

At least that's my idealistic take on it. In the real world you're more likely dealing with non-technical graphic designers who are functioning as client-side coders, or you're writing both the custom tags and the JSPs yourself. In my case it's the latter, however I still make an attempt to keep the code separated along well-defined programming roles.

drpeterjones
Tuesday, April 13, 2004

I'm writing both myself. I'd love to have some graphic designers available but fat chance. I'm just trying to achieve a higher level of re-use than at the script level and keep any nasty Javascript encapsulated.

John Topley (www.johntopley.com)
Wednesday, April 14, 2004

Create a WAR file that includes your entire web application.  Stick the javascript in a *.js file, then have the tag insert the link to the js file in the web page.

When you deploy, you provide the web file, the js file, and the JAR files all in the same package.

Will
Wednesday, April 14, 2004

I want to achieve re-use at the JAR level and not at the WAR level. I've gone with Matt's flag. It's not the most elegant solution but I'm a pragmatic guy and it works.

Thanks for all your suggestions.

John Topley (www.johntopley.com)
Wednesday, April 14, 2004

*  Recent Topics

*  Fog Creek Home