I was trying to create a simple web project and was starting to learn JavaScript.
I was suggested to created all JS functions in a .js file and import in to the JSP file, so as the code will be clean and it would be best practise.
But I was facing some issues when I did the following.
<script type="text/javascript" src="/js/index.js"></script>
This is what I tried. The issue I faced is that my script is not running, which means that the .js file is not being found at runtime.
After some googling, I came to know that I should use like below.
<script type="text/javascript" src="${pageContext.request.contextPath}/js/index.js"></script>
Explanation given on the internet:
Put the JS file in a folder under web content (but not WEB-INF) like [WebContent]/js/jsgl.min.js, and use the following in the JSP:
Explanation
JSP files are compiled by the server, then processed to send data (typically HTML) back to the web browser. A
The src attribute is relative to the URL that the browser asked for, not to the path of the JSP on the server.
So as an example, let's say:
The best solution therefore is indeed to put the JS file in a static folder like /js/jsgl.min.js, but to use the following in the JSP script tag:
The JSP will translate the
I was suggested to created all JS functions in a .js file and import in to the JSP file, so as the code will be clean and it would be best practise.
But I was facing some issues when I did the following.
<script type="text/javascript" src="/js/index.js"></script>
This is what I tried. The issue I faced is that my script is not running, which means that the .js file is not being found at runtime.
After some googling, I came to know that I should use like below.
<script type="text/javascript" src="${pageContext.request.contextPath}/js/index.js"></script>
Explanation given on the internet:
Put the JS file in a folder under web content (but not WEB-INF) like [WebContent]/js/jsgl.min.js, and use the following in the JSP:
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jsgl.min.js"></script>
Explanation
JSP files are compiled by the server, then processed to send data (typically HTML) back to the web browser. A
<script>
tag is a HTML tag that gets interpreted by the browser, not by the
servlet container. So the browser sees that in the HTML then makes a new request to the server for the JavaScript file in the src
attribute.The src attribute is relative to the URL that the browser asked for, not to the path of the JSP on the server.
So as an example, let's say:
- The browser asks for a page at
http://example.com/SomeWebApp/some-resource
- The servlet container internally forwards the request to a JSP at
/WEB-INF/jsp/somepage.jsp
- The response sent to the browser contains the script tag
<script type="text/javascript" src="./jsgl.min.js"></script>
(as in your question) - The browser sees the URL
./jsgl.min.js
and resolves it relative to the URL it has asked the server for (which in this case washttp://example.com/SomeWebApp/some-resource
- note there is no trailing '/') so the browser will request the JS file fromhttp://example.com/SomeWebApp/jsgl.min.js
*. This is because the relative URL in the script tag's src attribute starts with a '.'.
<script type="text/javascript" src="/js/jsgl.min.js"></script>
. Using the same original page URL as in the example above, the browser would translate this src URL to http://example.com/js/jsgl.min.js
. Note that this is missing the "/SomeWebApp" context path.The best solution therefore is indeed to put the JS file in a static folder like /js/jsgl.min.js, but to use the following in the JSP script tag:
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jsgl.min.js"></script>
The JSP will translate the
${pageContext.request.contextPath}
bit into the current context path, making the code portable (if you
redeploy the webapp with a different context path, it will still work).
So the HTML response received by the browser will be (again, sticking
with our example above):<script type="text/javascript" src="/SomeWebApp/js/jsgl.min.js"></script>
The browser will now resolve that relative URL to the correct targe
Ponte Vedra Casino and Spa - Mapyro
ReplyDeleteGet 양주 출장마사지 directions, reviews and information for Ponte 군산 출장안마 Vedra Casino and Spa in Ponte Vedra, Arizona. 계룡 출장안마 Ponte Vedra Casino And Spa: 김포 출장마사지 84510 West Route 115, Phoenix, 삼척 출장샵 AZ 85139.