« Babelfish fun | Main | Life is strange »

Javascript, JSP and common code base

I'm not exactly a JSP veteran so this may sound like a very stupid question.. but how do other, more experienced developers deal with common JavaScript methods?

Now with JavaScript being object oriented it makes it relatively simple to write common functions.. and so far my solution has been to simply put them in includes files that get included in the JSPs themselves.. There has to be a better solution..

I searched and searched and searched and nothing jumped out at me that would solve this in a more elegant manner.. so I pose this question to the Java folks in the blog land out there.. how do you do it?

And uh.. thanks for reading this completely useless blog post :)

Comments

Generally I have used meta links to Javascript files. That way the Javascript content only needs to be downloaded once no matter how many times they use the same screens.

<script src="/scripts/common.js" type="text/javascript" ></script>

You will want to place as much code into these external files instead of each JSP to reduce the download size of each screen. Some applications can get even more complex, so you may even have a taglib manage what Javascript you include on a screen. The logic all depends on what you are doing.

There is nothing about JSP that should require you to treat Javascript in a different manner than if you were writing plain HTML. Unless, of course, you are trying to dynamically generate javascript based on certain server-side conditions; though, again, that's pretty straightforward.

Lance, I know that.. I'm trying to minimize code duplication by creating common JavaScript libraries.. just asking what's the best solution.

So far it sounds like my include files are it.

We did play around with JSP Custom tags that spew out Javascript. For example, we had a tag called which would encapsulate the Javascript to open a new browser window. We then backed off this approach and settled for merely centralizing .js files in a javascripts directory.

rtb