File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Reference java script in servlet code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reference java script in servlet code" Watch "Reference java script in servlet code" New topic

Reference java script in servlet code

David Perry

Joined: Dec 12, 2003
Posts: 19
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.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63839

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"

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Reference java script in servlet code
It's not a secret anymore!