0

I have a dynamic web app developed in Eclipse using Tomcat. My web application folder is WebContent which has the following structure:

WebContent
   |_____WEB-INF
   |        |__ web.xml
   |___form.html
   |
   |___send.js

The form.html file references the JavaScript file send.js as follows:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="send.js"></script>

They are both in the same root directory of WebContent. However, when I started the servlet in Eclipse and tried opening form.html in the browser. The send.js is not executed. But if I copy everything in send.js directly to form.html and put them in the <script>...</script> tags, it will execute the JavaScript code correctly. I don't want to put all JS code in the HTML file, so I prefer to have send.js file separate from HTML file. So how to make JS file to be executed?

Also I'm using Jersey as RESTful web service and my web.xml servlet mapping is as follows:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Why can't Jersey's Servlet container find the JavaScript file (send.js) in the web root directory (WebContent)?

tonga
  • 11,749
  • 25
  • 75
  • 96
  • You say that `send.js` is not executed. Is it even loaded? Use your browser's developer tools and see what the response is when the browser requests send.js. – dsh Jul 04 '13 at 00:20
  • I debugged it using browser's tool. The JS file `send.js` is not even loaded. – tonga Jul 04 '13 at 01:21
  • That's the problem, then. Does the browser request it? What URL is it requesting? Is the path in the URL correct? What is the response from the server? Is it 404, 500, 302, or something else? – dsh Jul 04 '13 at 01:32
  • I put a test link in `form.html`: ``. When I clicked the link, it responded as 404 resource not found. It seems that the RESTful web service cannot find any html or js files in the directory. The URL for `welcome.html` is `http://localhost:8080/myapp/rest/welcome.html`. The file is in the same WebContent directory. – tonga Jul 04 '13 at 01:39
  • From what URL do you load `form.html`? I don't think Jersey will serve anything other than a RESTful resource (that is, a method in a class which is marked with `@Path`). I would expect the URL for your files to be `http://localhost:8080/myapp/welcome.html` and `http://localhost:8080/myapp/send.js`. You probably need to add another `` and `` entry to your `web.xml`. I think an application I work on uses Faces Servlet for serving the JavaScript files in addition to the JSF/Facelets. – dsh Jul 04 '13 at 01:58

1 Answers1

1

Jersey only handles RESTful methods. Jersey does not handle serving other web resources. That is why http://localhost:8080/myapp/rest/send.js results in a 404 "Not Found".

You will need to define another <servlet> and <servlet-mapping> in your web.xml for handling the other HTML, CSS, JavaScript, image, etc. resources. An application I work on uses the Faces Servlet for serving not only the JSF/Facelet pages but also, I think, it is the one serving the JS, CSS, etc.

Then, the correct URL to load send.js will look like http://localhost:8080/myapp/send.js

dsh
  • 12,037
  • 3
  • 33
  • 51
  • 1
    Thanks dsh. I found another link that is related to my question: http://stackoverflow.com/q/12422660/1649466. I need to change from `` to `` in web.xml and add `WebPageContentRegex` to include non-REST static resources such as `js|css|images` files. – tonga Jul 04 '13 at 02:26
  • 1
    That looks like another approach. Personally I keep the URLs separate (ie `/api/*` for REST and a separate path for other resources). – dsh Jul 04 '13 at 02:32
  • Yes, I think that's a better way of having two separate folders for REST resources and non-REST resources. But it seems that I still need to change `` to `` in web.xml in order to access static non-REST resources. – tonga Jul 04 '13 at 02:35
  • Well, like I said, we're not using `` in the application I work on. We have the other URL patterns mapped to a different servlet. The application uses multiple servlets including Jersey Servlet, Faces Servlet, and some other servlets that are specific to the application. – dsh Jul 04 '13 at 02:38
  • I found another article which explains why we need to change from to in web.xml when we want to use Jersey to access static resouces. http://blog.docuverse.com/2009/08/04/using-jsp-with-jersey-jax-rs-implementation/ – tonga Jul 04 '13 at 02:42