Search code examples
javascripthtmljspexternal

JavaScript - Error 404 resource not found


I had a java script in my JSP, which worked fine. Now I want to move it to it's own file (abc.js). I created js-Folder and placed it there. I tried to include it with <script language="JavaScript" type="text/javascript" src="./js/abc.js"></script> which links to http://localhost:8080/ProjectName/js/abc.js (from link in HTML-Code of the page). I think this should be correct, isn't it?

Nevertheless I get errors in the browser console (Failed to load resource: the server responded with a status of 404 () abc.js)

Do I have to register the .js somewhere to be recognized?

Edit: My project structure is like this:

ProjectName
   |---WEB-INF
          |---page.jsp (that wants to include abc.js)
          |---js
               |---abc.js

Solution

  • The WEB-INF directory is not publicly accessible. Your HTML file accesses the JS file over an public URL. Details see here: Include javascript file from inside WEB-INF

    So the solution for your problem is to place the JS file on the same level as the WEB-INF directory, so that it is served publicly and adjust the src link accordingly:

    <script type="text/javascript" src="${pageContext.request.contextPath}/js/abc.js"></script>
    

    (How include an external JS file in a JSP page)