• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using JSP to generate a JavaScript file

 
Eugene Lubman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a somewhat unusual requirement where I need to dynamically generate a JavaScript (.js) file. I was wondering if I can use a JSP/JSTL page for that, or if there's something in the spec that requires that processing of a JSP page must always result in a markup file (perhaps with text/html MIME type in the response header)? I suspect that it should be OK, but just want to make sure.

Thanks.

GL
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, it won't be an issue. JSP is just a templating mechanism after all. It just outputs text, that text can be anything you like.

You might potentially have some issues with extra spaces being generated by the JSP. HTML doesn't worry about extra white space, but javascript might.
Also you will still have to call your page .jsp for it to be interpreted by the jsp engine.

Also be aware that you will have to download this javascript every time.
Is the dynamic part dynamic on every request, or just for each session?
Potentially might there be caching issues with this approach?

Hope this helps,
evnafets
 
Eugene Lubman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan.

I just tried it and it seems to work fine. I also put the following directive at the top of my JSP:

<%@ page language="java" contentType="text/javascript" %>

GL
 
mahendra survase
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thingk that works as in our project we have not only created the javascript
we have created number of HTML component through jsp based on user selection and we have added even the validation at run time to those component through jsp

mahen
SCJP,SCBCD,ICED
SCEA part-I in progress
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<script type="text/javascript" src="yourPage.jsp"></script>

Just set the content type to text/html and make sure you have nothing in the file other than the JavaScript statements when it renders.

Eric
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!