JSP obtains absolute path

Source: Internet
Author: User

In Javaweb development, JavaScript and CSS files are often introduced using absolute paths, which avoids the fact that the directory changes lead to the introduction of files that are not found, and are commonly used as follows:

Use ${pagecontext.request.contextpath}

The role of the Code "${pagecontext.request.contextpath}" is to remove the deployed application name so that no matter how it is deployed, the path used is correct!

EX:

<!--using absolute paths to introduce CSS files--><link rel= "stylesheet" href= "${pagecontext.request.contextpath}/demo/default/css /index.css "  type=" text/css "/><!--using absolute paths to introduce JavaScript scripts--><script type=" Text/javascript "src=" ${ Pagecontext.request.contextpath}/demo/js/index.js "></script>

Use <%=request.getcontextpath ()%> and use ${pagecontext.request.contextpath} to achieve the same effect

<script type= "Text/javascript" src= "<%=request.getcontextpath ()%>/demo/js/index.js" >

<script>

JSP obtains absolute path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.