This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes External javascript and servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "External javascript and servlets" Watch "External javascript and servlets" New topic
Author

External javascript and servlets

Jim Bo
Greenhorn

Joined: Dec 09, 2002
Posts: 5
Can anyone help me with an example of how to use an external javascript file in a servlet?
I am trying to use the same javascript (a navigation menu) on multiple pages that are generated from servlets.
I can get the javascript to work properly if I embed it directly in the servlet code, but then I need to modify every servlet every time there is a change. When I extract the javascript from the servlet and place it in an external file, I get errors on my page.
I've tried everything I can think of including to make this work and I've also tried to make it work as a JSP file instead of a servlet.
My environment is:
OS Win2000
JDK 1.3.1
Servlet Engine Tomcat 4.0.1
Web Server Apache 1.3.26
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Jim
What code are you trying? Something like this HTML should work:
<script language="JavaScript" type="text/javascript" src="../includes/validation.js"></script>
Just write it out in the servlet or embed it as HTML in the JSP.
What sort of errors are you getting? Is it a path problem. In the example above the javaScript file is in a folder called 'includes' which is in the root of the webapp. The JSP pages that use it are all in the root too.
Hope that helps


Dave
Jim Bo
Greenhorn

Joined: Dec 09, 2002
Posts: 5
Dave,
Thanks for your reply. This problem is particularly frustrating for me, since I can't really figure out where my problem lies.
I have used the construct
<script language="JavaScript" type="text/javascript" src="../includes/validation.js"></script>
as you suggested, and this works just fine if I have a plain HTML page. However, when I include the same code in my servlet (enclosed within a println statement) it doesn't work.
Looking thru the Apache access.log it appears that the server is finding the javascript file OK but the javascript doesn't work on the resulting page.
I have also tried to use the include() method from a RequestDispatcher, but wasn't successful with this either.
I believe my problem might be one of using the correct syntax in the javascript file. Once again, I have tried several different approaches, but still haven't been able to make this work.
I'm afraid that I've managed to confus myself at this stage so I certainly appreciate any help.
Thanks
Jim
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Jim
Are you sure it is finding it correctly? Do you use packages in your project? If so then you might have to change the path to the javaScript file. Try an absolute path name or a virtual one but from the root of the web app.
Also, check out the soource of the html page to make sure it is being written as you think it is. Lastly try a simple script inside the include file:
function myTest(){ alert("Testing");}
Make that the only function in the included file then change the body tag of your HTML page to add this:
on_load="myTest()" (I had to add an underscore to the event handler, just take out the underscore to use it). If you get the alert box then you know it is something else in you javascript that is broken, if not then it is most likely a path issue.
Let me know how that works...
Jim Bo
Greenhorn

Joined: Dec 09, 2002
Posts: 5
Dave,
I believe the file is being found, but at this point, I am not certain of anything. A look thru the Apache access.log shows that http code 200 is returned the first time the servlet and the javascript file is accessed and a code of 304 is returned upon subsequent accesses. As I understand these codes, 200 means that everything was OK and 304 means that nothing in the file being served has changed since from the previous time it was served.
Other different files served from the same site seem to only return code 200 no matter how many times they are accessed. So, am I correct in believing that the return of code 304 for the javascript file is due to the fact that it is part of the servlet code that was compiled? I'm basing this on the servlet engine having to keep track of which servlets have been modified.

A look thru the Apache error log show no errors when the servlet and javascript file are accessed.
While I won't rule out the possibility of a package problem, I think that I would see error messages since the file wouldn't be found in the correct location. Since I don't see those, I am hoping the package naming and path info are correct. However, I could be vulnerable to having files located in the wrong directories. If javascript files need to be placed in specific directories I am not aware of which ones they need to go in. My servlet files are in the correct directories.
I have also looked at the servlet generated HTML as you suggested. A snippet is listed below.

html
head
SCRIPT LANGUAGE='JAVASCRIPT' TYPE='TEXT/JAVASCRIPT' SRC='/examples/GCImages/S1.js'
/SCRIPT
/head
body on_Load='rotate()'
...other html....
...other html....
/body
/html
(Sorry, but I have left out the carets in order to be able to respond to you)
My javascript file is S1.js and the function rotate() is defined in the file.
Should the javascript actually be printed out in the HTML source? I'm getting blank lines where I would expect the javascript to appear.
Do the blank lines help to indicate anything about the problem?
The number of blank lines doesn't equal the number of statements in the javascript file or anything else I can recognize, so I don't know where they come from.
I have literally copied the javascript from a test servlet where the javascript was included as part of the servlet source. Once I got this to work properly, I cut the javascript lines out, put them in the SI.js file and changed the servlet to add the SRC='/examples/GCImages/S1.js' reference. After recompiling the servlet and placing both files in their respective directories, I tried to run the servlet again.
So, this raises the question of whether my process was correct. Could I have introduced inadvertant errors by following this process? Do I need to change any of the statements? Do I need to use the less than inequality symbol rather than the characters that represent the symbol?

I can't try your example until later, as the system is being used for a demo, but I'll have access later.
Again, thanks for all your help, it is very much appreciated.
Jim
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
Hi Jim,
On having a casual look was just curious if the "_" is a typo in onLoad, if not, that is what you are looking for.
Bhushan
Jim Bo
Greenhorn

Joined: Dec 09, 2002
Posts: 5
Bhushan,
Thanks for your input. The underscore is not part of the code, but was the only way I could figure out how to put the code segment in my reply. This BB tried to read my message as HTML code. So I put the underscore in as a workaround. You are right, that would have caused a problem had it been in my code.
Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: External javascript and servlets