Web application path problem (relative path, absolute path, dynamic fetch path)

Source: Internet
Author: User

1. Relative path and absolute path

Absolute path: The path that begins with " / " and is the full path.

Relative path: A path that does not start with " / " and is relative to the current Web resource directory.

In an absolute path, the meaning of " / " is interpreted in two ways:

(1) If the server-side resolution, " / " represents the absolute path relative to the current Web application, that is: protocol name : //server name (host address): port number / project name / , for example: http://localhost:8080/MyTest/.

  (2) If the browser-side parsing (the path appearing in the Address bar), " / " represents the absolute path relative to the current server, i.e.: protocol Name://server name (host address): Port number/, for example: http://localhost:8080/.

Examples of two common absolute paths are forwarding and redirection .

Assume that in the current Dynamic Web project MyTest, there are two pages src.html and des.html under the project root directory webcontent.

Src.html, the servlet in the project is accessed via hyperlinks, at which point the address of the a tag has a browser-side resolution, and the first " / " of the path represents http://localhost:8080/, if you write directly "/ Myservlet "will report 404 errors.

<href= "/mytest/myservlet"> home </a>  <!-- -

Do the following processing in Myservlet, forwarding the request to des.html for processing, at which point the address in the forwarder is parsed by the server, the first " / " of the path represents http://localhost:8080/MyTest/.

In the case of redirection, the address in the redirect is parsed by the browser, the first " / " of the path represents http://localhost:8080/, and a 404 error is reported if the "/des.html" is written directly.

protected void throws servletexception, IOException {        request.getrequestdispatcher (//   /http// localhost: 8080/mytest/des.html
Response.sendredirect ("/mytest/des.html"); /mytest/des.html equivalent to http://localhost:8080/MyTest/des.html
    }

If the relative path is simpler, but the relative path is more error-prone than the absolute path, the link is easily invalidated when the page is replaced.

<href= "Myservlet"> home </a>
protected void throws servletexception, IOException {        request.getrequestdispatcher ("des.html"). Forward (Request, Response); // response.sendredirect ("des.html");    }

However, both the relative path and the absolute path are prone to problems.

2. Dynamic Fetch path (least error-prone method)

<%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><Body>    <!--gets the protocol used by the current link; The general application returns HTTP; SSL returns HTTPS;  -request.getscheme () = = =<%=Request.getscheme ()%><!--http -    <BR>    <!--Gets the name of the server on which the current page resides, or localhost if it is local -request.getservername () = = =<%=Request.getservername ()%><!--localhost -    <BR>    <!--get the port number of the server -request.getserverport () = = =<%=Request.getserverport ()%><!--8080 -    <BR>    <!--gets the root directory of the current web App -Request.getcontextpath () = = =<%=Request.getcontextpath ()%><!--/mytest -    <BR>basepath===<%=BasePath%><!--http://localhost:8080/MyTest/ -    <BR></Body>

In the Java background can also get to the path, but more for the foreground access path and JS, CSS, IMG and other static resources access path problem.

Web application path problem (relative path, absolute path, dynamic fetch 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.