JSP Path Problems

Source: Internet
Author: User

Get project path: $ {pagecontext. Request. contextpath}

<SCRIPT type = "text/JavaScript" src = "$ {pagecontext. Request. contextpath}/WH/JS/system/rainutil. js"> </SCRIPT>

I. Problems

Whether to use absolute paths or relative paths in JSP is more and more complex with the technology adopted, and this problem becomes increasingly difficult to solve.

1) problems with using relative paths

L The relative path is flexible, but it is difficult to copy the code in the page because different pages have different relative paths. After copying, you must modify the path of each connection.
L if a page is contained by more than one page, the relative path in the contained page is incorrect.
L if the struts action is used to return the page, the browser cannot correctly interpret the path in the page because the page path is different from the action path, for example, the page is/pages/Cust. JSP. The image directory is/images/title.gif. the path used in JSP is ".. /.. /images/title.gif ", but if the forward of an action points to this JSP file, the action path is/Cust/manage. do, then the page content is ".. /.. "/images/title.gif" will no longer point to the correct path.
It seems that only the absolute path is used to solve the above problem.

2) problems with absolute paths

L the absolute path value varies with different web application publishing methods. If the web application is published as MyApp, the path "/MyApp/images/title.gif" is correct, but the path is incorrect when the web application is published as myapp2, this may be a small case, but the absolute path above is also different when the web application is released in the default mode: "/images/title.gif ".



II. solution 1) use an absolute path. To solve the differences between different deployment methods, add $ {pagecontext before all non-Struts labels. request. contextpath}, for example, the original path is:

"/Images/title.gif", changed
"$ {Pagecontext. Request. contextpath}/images/title.gif ".
The Code "$ {pagecontext. Request. contextpath}" is used to retrieve the name of the deployed application. No matter how deployed, the path is correct.
Disadvantages:
The operation is inconvenient. Other tools cannot correctly explain $ {pagecontext. Request. contextpath}

2) use a relative path to add a base tag to each JSP file, such:

<Base href = "http: // $ {header ['host'] }$ {pagecontext. Request. contextpath}/pages/Cust/relation. jsp"/>
In this way, all paths can use relative paths.
Disadvantages:
The file to be included is still invalid.
For real use, flexible application is required. 1) and 2) write more robust code.

You can use $ {pagecontext. request. contextpath}. You can also use <% = request. getcontextpath () %> achieves the same effect. You can also set $ {pagecontext. request. contextpath}, put it into a JSP file, put it in a variable using C: set, and then use the El expression when using it.

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