0

my jsp file doesn't find my javascript file. here you can see my jsp file :

    <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="ressources/js/addMatrix.js"></script>
</head>
<body>
    <div ng-app="App" ng-controller="MainCtrl">
        <p>Add a new element : </p>
        <table>
            <tr>
                <td data-ng-repeat="data in texts">
                    <button>{{data.text}}</button>
                </td>
                <td data-ng-repeat="field in fields">
                    <form ng-submit="submit(text)">
                        <input type="text" ng-model="text" name="text"
                            placeholder="New Category" />
                    </form>
                </td>
                <td>
                    <button ng-click="addNewChoice()">+</button>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>

My javascript file is in the folder /WebContent/ressources/js and the jsp file is in the folder /WebContent.

My servlet only contains this call in the get method:

this.getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);

and my web.xml file looks like this :

 <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <servlet>
    <servlet-name>Home</servlet-name>
    <servlet-class>com.pi.servlets.Index</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Home</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

waiting for your help.Thanks

Kevin Vincent
  • 587
  • 13
  • 28

2 Answers2

5

I am not JavaEE dev so this may not be best solution but I think it may be good start

my jsp file doesn't find my javascript file

<script src="ressources/js/addMatrix.js"></script> is executed on clients side. Since it is not absolute path like

https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js

but relative one it will try to find ressources directory based on current location (URL available for browser). So if your URL is something like

http://server/project/someTask/yourServlet

it will try to find it in

http://server/project/someTask/ 

instead of resources inside

http://server/project/

Simple way around would be making your path relative to root of your server (so start it with /) and include project name:

<script src="/project/ressources/js/addMatrix.js"></script>

Or to make it more dynamic you can read name of your project in form /project with EL, so your code can look like

<script src="${pageContext.request.contextPath}/ressources/js/addMatrix.js"></script>
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • I try this and i think it's a very good start but i encountered an another issue when i look my chrome console developer i have this message Uncaught SyntaxError: Unexpected token < and when i click on my javascript file i see my html file content and not my javascript file content – Kevin Vincent Jan 03 '16 at 23:03
  • @KevinVincent Can I assume your JS script was found by client but there is some problem inside of that script? If yes then you should probably ask separate question about it since problem about finding/describing location of script != problem with script content. – Pshemo Jan 04 '16 at 01:22
0

You can open it in chrome, and hit the f12 button, then under the tab sources you can see if it is connected or not.

Rabbi Shuki Gur
  • 1,656
  • 19
  • 36
  • in my chrome console developer i have this issue Uncaught SyntaxError: Unexpected token < and i look into my javascript file i see the jsp file content. have you ever encounter this issue ? – Kevin Vincent Jan 03 '16 at 23:07
  • Is there any red lines or errors in the js code? – Rabbi Shuki Gur Jan 04 '16 at 05:12
  • Try closing the server deleting it and reopening, as the server doesnt loas with changes that arr made to the js. – Rabbi Shuki Gur Jan 04 '16 at 05:14
  • Also although it sounds very trivial dont forget to reload the cache. Ctrl+F5 – Rabbi Shuki Gur Jan 04 '16 at 05:23
  • Thanks for all your answers but i think i found the issue, in my web.xml file i changed the url-pattern (/test) and it finally works. I think it's because the name of my jsp file is "index". There is probably a conflict. I don't know why it works so fi anyone has an idea please share it. – Kevin Vincent Jan 04 '16 at 08:43
  • Maybe it saved an old copy of the xml. – Rabbi Shuki Gur Jan 04 '16 at 17:37