Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference java script in servlet code

 
David Perry
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet that generates html code to display a page back to the user. In this html code, I'm trying to place a reference to a java script file.
"<script language='javascript' src='../myscript.js'></script>"
The page gets generated, however when the user does something that calls a function in the java script, the page bombs out. The only way I can get this to work is to actually embed the java script in the html page that is sent back.
"<script language='javascript'>function submitForm(inParam){this.document.resultsform.dml.value=inParam;this.document.resultsform.submit();}</script>"
My thought was that maybe I can't embed a reference to a java script because there's no actual html page that exists and therefore no context for the java script file to be referenced to. Anyone have any experience feedback on this?
Thanks in advance.
David
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your relative reference of "../" will be relative to the current URL, which in the case of a servlet does not represent a physical folder. You need to modify the reference to be server relative.
For example, something along the lines of src="/mywebapp/scripts/myscript.js"
bear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic