How Include An External JS File In A JSP Page
Solution 1:
but when I running the application, the system behavior seems like no script is running.
This is because the browser is unable to load the javascript 'index.js' from the specified location. If you open the browser console and go to the 'networks' tab, you will see 404 (resource not found) against it. You cannot specify a relative URL to a JavaScript in a JSP file that way.
You need to provide your folder structure (where index.js is located in your project) and how you have configured web.xml. But if you try the following, it will surely work:
<script type="text/javascript" src="${pageContext.request.contextPath}/js/index.js"></script>
And then keep the 'js' folder containing 'index.js' at the same level as 'WEB-INF'.
Solution 2:
Check the path of the index file and then make sure that the code is correct.I assume that js is a folder.Post the index.js code.
Solution 3:
In hopes of saving others the half hour it cost me to resolve this, VHS's answer needs an update for anyone who has upgraded to Jakarta. The reference needs to be: <script type="text/javascript" src="${jakarta.servlet.jsp.PageContext}/js/index.js"></script>
Post a Comment for "How Include An External JS File In A JSP Page"