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

13
  • What errors are you facing? Commented Jan 3, 2016 at 22:05
  • I can see the html on the page but all javascript functions don't work, i think the jsp file doesn't find the javascript file. Commented Jan 3, 2016 at 22:08
  • Share your html page Commented Jan 3, 2016 at 22:09
  • when i try the jsp file by double clicking on the file it works but when i start the server to try the jsp file it doesn't work and the only difference between both is that in the second way the page is called by the method doGet in the servlet Commented Jan 3, 2016 at 22:11
  • 1
    You've a second major problem which is answered here: stackoverflow.com/questions/4140448/… Commented Jan 4, 2016 at 11:48

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

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
@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.
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.

6 Comments

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 ?
Is there any red lines or errors in the js code?
Try closing the server deleting it and reopening, as the server doesnt loas with changes that arr made to the js.
Also although it sounds very trivial dont forget to reload the cache. Ctrl+F5
Thanks for all your answers but i think i found the issue, in my web.xml file i changed the url-pattern (<url-pattern>/test</url-pattern>) 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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.