wood burning stoves 2.0*
The moose likes JSP and the fly likes calling js file from jsp. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "calling js file from jsp." Watch "calling js file from jsp." New topic

calling js file from jsp.

Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
hi all,
im using tomcat 4.0
i have a 'js' file which i want to include in a jsp file..how shld i give the path for the js file ??
i have kept the jsp files in the direcory named jsp and js files in a directory named js
the directory structure is
test // context path
| - Jsp
| - js
it works properly on html files..
i have tried everything like
<script language="JavaScript" src="../js/test.js">
<script language="JavaScript" src="/test/js/test.js" >
in the jsp file to include the js but all in vain
can any1 help me ?? im not sure where to post to doubt..is it anything related to tomcat setting ??

SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12758
When you do a view source on the html page that is written to your browser what do you see where the included script is supposed to be?
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
hi bill,
thanks for the reply..i wll tell u something.
I copied the source of the jsp page which is shown on the browser (right click,view source select all) and saved it as a html file..Later I executed this file from outside the server environment, i.e by giving the file path on the browser like d:\tomcat\...\jsp\copied.html
and found i working..i think there is some prob with my server setting but i donno wat to do !!! ..im using tomcat 4.0 on Win NT..
and in my first posting, wat i meant by 'html working properly' is html file working outside the server environment..
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
hi bill,
I solved it..The culprit was the name of the 'js' directory..The name of my js directory was "Js" not "js" so i shld have given the src of js file as

<script .... src = "../JS/test.js">
i donno why ???
I have one more situation..im using two js files in the same "Js" directory..by giving the above <script src ..> I was able to access the one js file but the second one is not getting inlcuded..huh !! im tired ...
thanks for ur reply
Piter john

Joined: Jan 06, 2003
Posts: 19
You can add Two or more .js files Like..
<script language='javascript' src='../JS/validations.js'></script>
<script language='JavaScript' src='../JS/datevalidate1.js'></script>
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
yep, i tried that !! i guess, my tomcat doesnt like 2 js files in a jsp file...i copied the content of the second js file into the jsp and is working properly but i dont like this approach.. hope my tomcat will accept the 2 js file soon..
Ivan Matmati
Ranch Hand

Joined: Feb 26, 2003
Posts: 41
as I've had to notice in a relative url in html the relative part is evaluated like that:
In a web application it's a little different :
But both have their root at context directory.So for html it's normal to write Js/test.js, no mention to tbe context needed.
Good bye.
[ March 17, 2003: Message edited by: Ivan Matmati ]
[ March 17, 2003: Message edited by: Ivan Matmati ]

No Gates!<p>SCPJ 1.4<br />SCWCD 1.4
I agree. Here's the link: http://aspose.com/file-tools
subject: calling js file from jsp.
Similar Threads
problem with accessing a JS file
javascript include src path not right
relative path confusion
calling js file..