Solve the problem of introducing JS, CSS and pictures under the Webroot of JSP page in/web-inf directory

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/qq_18875541/article/details/69390871

JSP pages are usually placed under Webroot/web-inf to prevent visitors from entering the page directly.

The pages under WebRoot/web-inf are protected and users cannot access them through the form of http://localhost:8080/test/webRoot/WEB-INF/content/xx.jsp, and their visits must be handled through struts.

However, when the page is placed under the/web-inf, the JSP page can not avoid the "external" js,css, the picture "interaction", because the need to introduce some webroot/css, Webroot/js, Webroot/images, and at this time it is easy to introduce failure, In general, the relative path does not work at this time, so absolute paths must be used! The following one by one summarizes.

First, the introduction of WEBROOT/CSS

<link href="<%=request.getcontextpath ()%>/css/default.css" rel="stylesheet" type ="Text/css"/>

This <%=request.getcontextpath ()%> is/hh_test (project name), and <%=request.getcontextpath ()%>/ CSS is equivalent to using an absolute path to directly locate the WEBROOT/CSS.

Ii. introduction of Webroot/js

<script type="Text/javascript" language= "javascript" src="<%= Request.getcontextpath ()%>/js/jquery-1.4.4.min.js "></script>

Iii. introduction of Webroot/images

<img src="<%=request.getcontextpath ()%>/images/4.gif" alt="Loading external pictures" />

Summary: It is best to put the JSP page under/web-inf, and the JSP page into the "external" js,css,images, you must get the root path of the project, plus/CSS (or/js or/images) is the method of using absolute path, rather than the method of relative path, To access the resources placed under the Webroot!

Solve the problem of introducing JS, CSS and pictures under the Webroot of JSP page in/web-inf directory

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.