Summary of relative path/absolute path in Java Web development, plug-in development, ECLIPSERCP __java

Source: Internet
Author: User
Tags code tag parent directory java web

1. Understanding of basic concepts

Absolute path: Absolute path is the true path of the file or directory on your home page on your hard disk, (URL and physical path) For example: C:/xyz/est.txt represents the absolute path of the Test.txt file. Http://www.sun.com/index.htm also represents a URL absolute path.

Relative path: The path relative to a base directory. Contains the relative path of the web (relative directories in HTML), for example: In a Servlet, "/" represents the directory for Web applications, and the relative representation of the physical path. For example: "./" represents the current directory, "... /"represents the parent directory. This similar representation is also a relative path.

2. about relative and absolute paths in Jsp/servlet
2.1, server-side address

The relative address on the server side refers to the address of your Web application, which is resolved on the server side (unlike the relative addresses in HTML and JavaScript, which are parsed by the client browser) that means that at this point in the JSP and servlet The relative address in should be relative to your Web application, that is, relative to http://192.168.0.1/webapp/.

The places it uses are:
Forward:servlet in the Request.getrequestdispatcher (address); This address is resolved on the server side, so you have to forward to a.jsp should write this: Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application WebApp, its The absolute address is: http://192.168.0.1/webapp/user/a.jsp. Sendredirect: <%response.sendredirect in JSP ("/rtccp/user/a.jsp");%>
2.2, the client's address

The relative addresses in all HTML pages are relative to the server root (HTTP://192.168.0.1/) rather than to the directory of the Web application (in the directory) http://192.168.0.1/webapp/. The address of the form's action attribute in Html should be relative to the server root (HTTP://192.168.0.1/), so if the commit to a.jsp is: action = "/webapp/user/a.jsp" or action = "<%=request.getcontextpath ()% >"/user/a.jsp;
Submitting to the servlet for actiom = "/webapp/handleservlet" Javascript is also resolved on the client side, so its relative path is the same as the form form. Therefore, in general, it is best to add <%=request.getcontextpath ()%> before attributes such as jsp/html pages, such as Css,javascript.action, to ensure that the referenced files belong to the Web The directory in the application. In addition, you should try to avoid using a similar ".", "./", ".. /.. /"A relative path relative to the location of the file, so that when the file is moved, it can easily go wrong.

3.jsp/servlet gets the relative path and absolute path of the current application

3.1, in the JSP to obtain the current application of the relative path and absolute path:

Absolute path for root directory: Request.getrequesturi ()

Absolute path of the file: Application.getrealpath (Request.getrequesturi ());

Absolute path to the current Web application: Application.getrealpath ("/");

Gets the upper directory of the request file:

New File (Application.getrealpath (Request.getrequesturi ())). GetParent ()
3.2, the Servlet to obtain the current application of the relative path and absolute path

The absolute path of the root directory: Request.getservletpath ();

Absolute path of File:

Request.getsession (). Getservletcontext (). Getrealpath (Request.getrequesturi ())

Absolute path of the current Web application: Servletconfig.getservletcontext (). Getrealpath ("/");
(ServletContext objects get several ways:
Javax.servlet.http.HttpSession.getServletContext ()
Javax.servlet.jsp.PageContext.getServletContext ()
Javax.servlet.ServletConfig.getServletContext ()
)

4. Use relative paths in the page

1 For example, now there is a page "index.htm", in which the link has a picture "photo.jpg". Their absolute path is as follows:

①c:/website/index.htm
C:/website/img/photo.jpg

If you use the absolute path "c:/website/img/photo.jpg", then everything works on your own computer because you can actually find the "photo.jpg" file in the specified location, "c:/website/img/photo.jpg." But when you upload the page to the site, it's probably going to go wrong. Because your site may be in the server "C" disk, may be in the "D" disk, may also be in the "AA" directory, more likely in the "BB" directory, in short, there is no reason for "c:/website/img/photo.jpg" Such a path. So, what path do you want to use in the "index.htm" file to locate the "photo.jpg" file? Yes, it should be a relative path, the so-called relative path, as the name suggests is their relative and target position. The "Photo.jpg" joined in "index.htm" in the example above can use "img/photo.jpg" to locate files, so no matter where they are placed, there is no error as long as their relative relationship does not change.

In addition we use ".. /"to represent the previous level of the directory,".. /.. /"represents the directory on the ancestor, and so on. (A friend who has studied "DOS" may be easier to understand)

To take a look at a few more examples, note that all of the examples in the "index.htm" file are joined with a picture "photo.jpg".

②c:/website/web/index.htm
C:/website/img/photo.jpg

How should "Photo.jpg" in "index.htm" in this example be represented?

Wrong wording: img/photo.jpg

This writing is not correct, in this case, for "index.htm" file, "Img/photo.jpg" represents the absolute path is "c:/website/web/img/photo.jpg", obviously does not meet the requirements.

Correct wording: use ".. /img/photo.jpg the relative path to locate the file

③c:/website/web/xz/index.htm
C:/website/img/images/photo.jpg

How should "Photo.jpg" in "index.htm" in this example be represented?

Wrong wording:. /img/images/photo.jpg

This type of writing is incorrect, in this case, for the "index.htm" file. The absolute path represented by "/img/images/photo.jpg" is "c:/website/web/img/images/photo.jpg".

The correct wording: You can use the ".. /.. /img/images/photo.jpg the relative path to locate the file

④c:/website/web/xz/index.htm
C:/website/web/img/photo.jpg

How should "Photo.jpg" in "index.htm" in this example be represented?

Wrong wording:. /.. /img/photo.jpg

This type of writing is incorrect, in this case, for the "index.htm" file. /.. The absolute path represented by "/img/photo.jpg" is "c:/website/img/photo.jpg".

The correct wording: You can use the ".. /img/photo.jpg the relative path to locate the file

Summary: Through the above examples can be found, in the absolute path to the relative path, two files absolute path of the same part can be ignored, do not consider. Just think about what they are different.

2 How to modify the path of the style sheet:

Open the "htm" file using a text editor, view the source code, and locate the "<link rel=" stylesheet "href=" in the beginning of the source code, "The

Cases:
C:/website/web/xz/index.htm
C:/website/css/test.css

In this example, the join "test.css" file in "index.htm", you can use the ".. /.. /css/test.css "The relative path to locate the file, the complete code tag is" <link rel= "stylesheet" href= ". /.. /css/test.css "type=" Text/css ">"

Error writing examples:.. /.. /.. /css/test.css

This type of writing is incorrect, in this case, for the "index.htm" file. /.. /.. /css/test.css "is the absolute path represented by" C:/css/test.css "

Finally, to avoid a path error when making a Web page, we can use the Dreamweaver site management feature to manage the site. As long as you use the menu command "site>new site > New site" To create a new site and define the site directory, it will automatically convert the absolute path to a relative path, and when you move files in the site, the connection path associated with those files will automatically change, It's really very convenient.

5.java to obtain a relative path in Class , method of Absolute path
5.1, get absolute path in a separate Java class

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.