Discussion Locked

Javascript files in Servlets -Help!

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Help!
Using TOMCAT 4.0.4.
I'm trying to include a javascript file in htm page output for client side validation.
I can do this in JSP the following string in the head section of the htm output:

everything works fine, but if i do the same in a servlet then when i'm in the browser and do something client side that calls functions in the js file i get the browsers "object not found" error.

If i specify a finite path (i.e. E:/mydocs/JSFieldHandler.js)in the servlet (and put the js file in this directory), all is well -but obviously i need to specify a relative path. So, what do i have to specify when using a js file in the servlet. Do i have to put the js files in any special place (i've tried putting them in the WEB-INF directory). Is there any special setup required for Tomcat?

2 replies

by richardhandel

Hi Roobarb,

I was interested to read your message - I struggled for half the night with exactly the same problem you describe! I'm using tomcat 5.0 and prefer servlets to jsp.

I can't find anything in the docs to help, but I finally worked it out - there is a special place you need to put your javascript for relative addressing to work - you need to create a directory structure (directly in your application directory) which mirrors the structure of your servlet paths ( in in web.xml).

For example, suppose you have a servlet called 'orange' and its url-pattern is:

food/fruit/orange'

then the complete url is:

myServer/myApp/food/fruit/orange

Now, suppose this servlet produces html which uses a javascript source file:

This discussion is closed to new replies.