How is using “<%=request.getContextPath()%>” better than “../”

后端 未结 1 1721
走了就别回头了
走了就别回头了 2020-12-13 02:37

I have worked on number of J2EE projects where the view layer is JSP. In most projects, I have seen that we reference external resources i.e. images, javascript, jsp\'s, css

相关标签:
1条回答
  • 2020-12-13 03:01

    request.getContextPath()- returns root path of your application, while ../ - returns parent directory of a file.

    You use request.getContextPath(), as it will always points to root of your application. If you were to move your jsp file from one directory to another, nothing needs to be changed. Now, consider the second approach. If you were to move your jsp files from one folder to another, you'd have to make changes at every location where you are referring your files.

    Also, better approach of using request.getContextPath() will be to set 'request.getContextPath()' in a variable and use that variable for referring your path.

    <c:set var="context" value="${pageContext.request.contextPath}" />
    <script src="${context}/themes/js/jquery.js"></script>
    

    PS- This is the one reason I can figure out. Don't know if there is any more significance to it.

    0 讨论(0)
提交回复
热议问题