Absolute paths and relative paths in Java

Source: Internet
Author: User
Tags rfc

1. Understanding of basic concepts

Absolute Path: The absolute path is the real path on the hard disk of the file or directory on your home page, (url and physical path) such as:
C:/xyz/test.txt represents the absolute path to the Test.txt File. Http://www.sun.com/index.htm also represents a
The URL absolute path.

Relative path: the path relative to a base directory. Contains the relative path to the Web (relative to the directory in html), for example:
In a servlet, "/" represents a directory of Web Apps. and the relative representation of the physical path. For Example: "./" represents the current directory,
".. /"represents the parent Directory. This similar representation also belongs to the relative path.

For additional information about uri,url,urn, please refer to RFC documentation STANDARDS.

RFC 2396:uniform Resource Identifiers (URI): Generic Syntax,
(http://www.ietf.org/rfc/rfc2396.txt)


2. about the relative path and absolute path in Jsp/servlet.

2.1 Server-side Address

The Server-side relative address refers to the address that is relative to your web application, which is resolved on the Server-side
(unlike the relative addresses in HTML and javascript, they are parsed by the client Browser)
The relative addresses in the JSP and servlet should be relative to your web application, that is, relative to http://192.168.0.1/webapp/.

Where they are used are:
Forward:servlet in Request.getrequestdispatcher (address); this address is
In the Server-side parsing, so you want to forward to a.jsp should be written like this:
Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application webapp,
The absolute address is: http://192.168.0.1/webapp/user/a.jsp.
Sendredirect: <%response.sendredirect ("/rtccp/user/a.jsp") in the jsp;%>

2.22, the address of the client

Relative addresses in all HTML pages are relative to the server root directory (http://192.168.0.1/).
Instead of (with the directory of the Web app under the Directory) http://192.168.0.1/webapp/.
The address of the Action property of the form form in HTML should be relative to the server root directory (http://192.168.0.1/).
so, if submitted to a.jsp as: action= "/webapp/user/a.jsp" or action= "<%=request.getcontextpath ()%>"/user/a.jsp;
Submit to servlet for actiom= "/webapp/handleservlet"
JavaScript is also parsed on the client side, so its relative path is the same as the form Form.

therefore, in general, in the jsp/html page and other references such as css,javascript.action attributes are best added
<%=request.getcontextpath ()%> to ensure that the referenced files belong to a directory in the Web app.
In addition, you should try to avoid the use of similar ".", "./", ". /.. /"such as relative path relative to the location of the file, so
When a file moves, it is prone to problems.


3. Get the relative path and absolute path of the current app in the
3.1 JSP to get the absolute path of the current App's relative and absolute path
  root jsp/servlet: request.getrequesturi ()
Absolute path to the   file    : application.getrealpath (request.getrequesturi ());
  Absolute path to the current web app: application.getrealpath ("/");
  Get the Upper-level directory of the requested file: new file (application.getrealpath (request.getrequesturi ())). getParent ()

Obtaining the relative and absolute paths of the current application in the 3.2 servlet
Absolute path for root directory: request.getservletpath ();
Absolute path to file: request.getsession (). getservletcontext (). Getrealpath
(request.getrequesturi ())
Absolute path to the current web app: servletconfig.getservletcontext (). getrealpath ("/");
(servletcontext objects are available in several ways:
Javax.servlet.http.HttpSession.getServletContext ()
Javax.servlet.jsp.PageContext.getServletContext ()
Javax.servlet.ServletConfig.getServletContext ()
)

Method of obtaining relative path, absolute path in 4.java class
4.1 Obtaining an absolute path in a separate Java class
According to Java.io.File's Doc text, we know:
By default, the directory represented by new File ("/") is: system.getproperty ("user.dir").
The program gets the current path of the execution class
Package org.cheng.file;
Import java.io.File;

public class Filetest {
public static void main (string[] Args) throws Exception {

System.out.println (thread.currentthread (). getcontextclassloader (). getresource (""));

System.out.println (FileTest.class.getClassLoader (). getresource (""));

System.out.println (classloader.getsystemresource (""));
System.out.println (FileTest.class.getResource (""));
System.out.println (FileTest.class.getResource ("/")); The path of the class file
System.out.println (new File ("/"). getabsolutepath ());
System.out.println (system.getproperty ("user.dir"));
}
}

4.2 Java classes in the server get the current path (from the Network)
(1). Weblogic

The WebApplication system file root directory is the root directory where your WebLogic installation is Located.
For example: If your weblogic is installed in c:/bea/weblogic700 .....
so, your file root path is c:/.
So there are two ways to get you to access your server-side files:
A. Using an absolute path:
For example, put your parameter file in c:/yourconfig/yourconf.properties,
Direct use of the new FileInputStream ("yourconfig/yourconf.properties");
B. Using relative paths:
The root of the relative path is the root path of your webapplication, which is the top-level directory of the web-inf, placing your parameter file

In yourwebapp/yourconfig/yourconf.properties,
This is used:
New FileInputStream ("./yourconfig/yourconf.properties");
Both of these methods can be chosen by themselves.

(2). Tomcat

Output System.getproperty ("user.dir") in the Class;%tomcat_home%/bin is Displayed.

(3). Resin

is not the relative path that your JSP puts, it is the JSP engine that executes this JSP compiled into a servlet
The path to the Root. for example, the new file method is used to test for file F = new ("a.htm");
This a.htm is under the resin installation directory.

(4). How to read the relative path?

In a Java file, getresource or getresourceasstream can be

Example: getclass (). getresourceasstream (filePath);//filepath can be "/filename", here/on behalf of the Web

Publish Root Path under Web-inf/classes

The default path for using this method is: Web-inf/classes. has been tested in Tomcat.

5. Relative path when reading files, avoid hard coding and absolute path Use. (from the Network)
5.1 Use Spring's di mechanism to get the file and avoid hard coding.
Refer to the following connection Content:
http://www.javajia.net/viewtopic.php?p=90213&
5.2 Reading of configuration files
Refer to the following connection Content:
Http://dev.csdn.net/develop/article/39/39681.shtm
5.3 Read an XML file from a virtual path or relative path, avoiding hard-coded
Refer to the following connection Content:
Http://club.gamvan.com/club/clubPage.jsp?iPage=1&tID=10708&ccID=8

Common operations for files in 6.Java (copy, move, delete, create, etc.) (from the Network)
Common Java File Operation classes
Http://www.easydone.cn/014/200604022353065155.htm

Java File Operations Encyclopedia (in Jsp)
Http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html

Java file operation details (java Chinese network)
Http://www.51cto.com/html/2005/1108/10947.htm

JAVA How to create/delete/modify/copy directories and files
Http://www.gamvan.com/developer/java/2005/2/264.html

Summarize:
Through the use of the above content can be resolved on the Web application Server side, move files, find files, copy
Delete files and other operations, while the relative address of the server, the concept of absolute address clearer.
Recommended reference uri, RFC standard text BLOCK. At the same time, Java.io.File. Java.NET.URI and other content to understand thoroughly
Other aspects of the understanding can be more thorough and thorough.
==================================================================================

Resources:
java/docs/

Java.io.File
Java.io.InputStream
Java.io.OutputStream
Java.io.FileInputStream
java.io.FileReader;
Java.io.FileOutputStream
java.io.FileWriter;
Java.Net.URI
Java.net.URL


The absolute path and relative path of Xiang solution
Http://www.webjx.com/htmldata/2005-02-26/1109430310.html

["j Road practice"] Absolute and relative paths in JSPs and Servlets
http://w3china.org/blog/more.asp?name=pcthomas&id=9122&commentid=12376

Jsp,servlet,class obtaining the relative and absolute paths of the current application
http://cy.lzu.edu.cn/cy/club/clubPage.jsp?ccStyle=0&tID=886&ccID=77

How to get the current file path
Http://www.matrix.org.cn/resource/article/44/44113_java.html

Through the spring injection mechanism, get the file
http://www.javajia.net/viewtopic.php?p=90213&

Read the configuration file
Http://dev.csdn.net/develop/article/39/39681.shtm

Read the configuration file, read an XML file from a virtual path or relative path, and avoid hard coding!
Http://club.gamvan.com/club/clubPage.jsp?iPage=1&tID=10708&ccID=8

Common Java File Operation classes
Http://www.easydone.cn/014/200604022353065155.htm

Java file Operations Daquan
Http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html

Java file Operation detailed
Http://www.51cto.com/html/2005/1108/10947.htm

Absolute paths and relative paths in Java

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.