<p>I have a .tag file that requires a JavaScript library (as in a .js file).</p>

<p>Currently I am just remembering to import the .js file in every JSP that uses the tag but this is a bit cumbersome and prone to error.</p>

<p>Is there a way to do the importing of the .js inside the JSP tag?</p>

<p><em>(for caching reasons I would want the .js to be a script import)</em></p>

[{'answer_id': 49185, 'author': 'Doug Miller', 'author_id': 3431280, 'author_profile': 'https://Stackoverflow.com/users/3431280', 'pm_score': 2, 'selected': False, 'text': "<p>Short of just including the js in every page automatically, I do not think so. It really would not be something that tags are designed to to.</p>\n\n<p>Without knowing what your tag is actually doing (presumably its its outputting something in the body section) then there is no way that it will be able to get at the head to put the declaration there.</p>\n\n<p>One solution that might (in my head) work would be to have an include that copies verbatim what you have in the head after the place in the head to import tags right up to where you want to use the tag. This is really not something that you would want to do. You would have to have multiple 'header' files to import depending on the content and where you want to use the tag. Maintenance nightmare. Just a bad idea all round. Any solution I can think of would require more work than manually just adding in the declaration.</p>\n\n<p>I think you are out of luck and stuck with manually putting it in.</p>\n\n<p><em>edit: Just import it in every page. It will be cached and then this problem goes away.</em></p>\n"}, {'answer_id': 49205, 'author': 'Tony BenBrahim', 'author_id': 80075, 'author_profile': 'https://Stackoverflow.com/users/80075', 'pm_score': 4, 'selected': True, 'text': "<p>There is no reason you cannot have  a script tag in the body, even though it is preferable for it to be in the head. Just emit the script tag before you emit your tag's markup. The only thing to consider is that you do not want to include the script more than once if you use the jsp tag on the page more than once. The way to solve that is to remember that you have already included the script, by addng an attribute to the request object.</p>\n"}]
['https://Stackoverflow.com/questions/49156', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1666/']