Home > Java > javaTutorial > Detailed explanation of examples of absolute paths and relative paths

Detailed explanation of examples of absolute paths and relative paths

零下一度
Release: 2017-06-30 11:08:25
Original
4104 people have browsed it

First distinguish between "/" and "\"

The path in the window generally uses "\";

The path in java is generally "/"; if "\" is used, it needs to be escaped to "\\"

1. Absolute path

 With the root directory The path of the file or folder used as a reference point is the real path on the hard disk. Has unique characteristics.

For example: C:\caosiege\python\project\C.py, which represents the absolute path of C.txt

2, relative path

Path relative to a base directory. Not unique.

For example: In servlet, "/" represents the root directory of the web application, corresponding to the physical path.

The above absolute path in C:\caosiege can be written as a relative path in the form of .\python\project\C.py. Where ".\" represents the current path.

"..\" represents the upper-level directory.

3. Extension:

1) Server-side address

The server-side relative address refers to the web address relative to yours The address of the application. This address is resolved on the server side. In other words, the relative addresses of jsp and servlet at this time are relative to your web application. That is relative to "http/192.168.0.1/webapp".

2) Client address

The addresses in all HTML pages are relative to the root directory of the server "http/192.168.0.1", not the directory of the web application.

The action attribute of the form in HTML is relative to the server root directory, so if it is submitted to xxx.jsp, action="/webapp/user/xxx.jsp" or action = "<%=request .getContextPath()%>”+

“/user/xxx.jsp”.

  Under normal circumstances, it is best to add <%=request.getContextPath()%> in front of the CSS, Javascript.Action and other attributes referenced by JSP/HTML pages to ensure that all referenced files All belong to the directory in the web application.

In addition, you should try to avoid using "/", "./", "../" and other similar relative paths relative to the location of the file. This will easily cause problems when the file is moved.

4. Some methods of obtaining the path

Absolute path corresponding to the root directory: request.getRequestURI()
The absolute path of the file: application.getRealPath(request.getRequestURI());
The absolute path of the current web application: application.getRealPath(“/”);
Obtain the request file The upper directory of the file: new File(application.getRealPath(request.getRequestURI())).getParent()

The absolute path of the file: request.getSession().getServletContext().getRealPath(request. getRequestURL())
The absolute path of the current web application: servletConfig.getServletContext().getRealPath(“/”);
(There are several ways to obtain the ServletContext object:
javax.servlet.http. HttpSession.getServletContext()
javax.servlet.jsp.PageContext.getServletContext()
javax.servlet.ServletConfig.getServletContext()

The above is the detailed content of Detailed explanation of examples of absolute paths and relative paths. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template