How to access CSS and JS files in Webroot in the JSP page of the Java EE Project under the Web-inf directory

Source: Internet
Author: User
Tags tomcat server

It took so long to find the information, and finally solved it.

Thanks Blog Park:

Thank you Baidu: Http:// Vz4tlygvnmyyvj105bcuzkusjf0g5rm6ophvezhccajk5s1gfuz3pbgawcnsfy1rmtpf4zeo8ev_gd7sykv4s_

In the Java EE environment with struts deployment, we generally write the JSP page in the Webroot\web-inf\content directory, so that the JSP page must be controlled and forwarded by struts to access, improve the security of the page.

However, it is very difficult to apply the Css,js file found in the Webroot directory in the JSP page today, first look at my file structure:

The goal is to access Default.css in index.jsp.

DEFAUTL.CSS is the parent directory of the parent directory of the index.jsp directory under the CSS directory. So I wrote this:

PS: The space is intentionally added, I do not know why it will automatically become the site of the blog Park

<link href = "../ ../css/default.css" rel = "stylesheet" type = "text / css" />
        <link href = "../ ../css/uploadify.css" rel = "stylesheet" type = "text / css" />
A hyperlink prompt also appears in Eclipes, indicating that the directory is correct, but the result fails.

After querying the information, I saw in Baidu Encyclopedia:

The WEB-INF folder under the TomCat server is a very secure file. The files in it cannot be accessed directly on the page. You must map the files to be accessed through the web.xml file to access them.

It can be seen from the above experiment that not only direct access, but also indirect access such as "../ ../" cannot succeed.


After querying the information from multiple parties, try to access it using the following methods:

         <!-Output to the project root directory, namely WebRoot->
         <% String path = request.getContextPath ();%>
         <link href = "$ {path} /css/default.css" rel = "stylesheet" type = "text / css" />
         <link href = "$ {path} /css/uploadify.css" rel = "stylesheet" type = "text / css" />

As expected, the page successfully read out the style in the css file to achieve the goal.


However, there are java scripts in this page, which are not standardized. After querying the information, the following pure EL expressions are used to implement:

 <C: set value = "$ {pageContext.request.contextPath}" var = "path" scope = "page" />
         <link href = "$ {path} /css/default.css" rel = "stylesheet" type = "text / css" />
         <link href = "$ {path} /css/uploadify.css" rel = "stylesheet" type = "text / css" />
Put the ContextPath in the page first, and then use the el expression to get the problem out.


However, there are also a lot of programmers who are using the Struts_2 label, to achieve the above effect, there is a difference

Struts_2 tags need to access pageContext.request.contextPath, which is different from jstl in writing

<S: set value = "# request.get (‘ javax.servlet.forward.context_path ‘)" var = "cssPath" scope = "page" />
<Link rel = "stylesheet" href = "$ {cssPath} /css/index.css" />

Struts_2 tag set value attribute to access the object, the syntax is value = "# Object" or value = "% {Object}" two ways of writing




-------------------------------------------------- --------------------

Over (over)


About JAVA EE project's jsp page under the WEB-INF directory, how to access the CSS and JS files in WebRoot

Related Article

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: 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.