• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using JSP to generate a JavaScript file

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic