Relative path in Java development, complete summary of absolute paths

Source: Internet
Author: User
Tags rfc

Relative path in Java development, complete summary of absolute paths Blog Category: 
    • Java synthesis
JAVAJSP Application Server servlet Network application

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) For example: C:\xyz\test.txt represents the absolute path to the Test.txt file. The 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 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 parsed on the server side (different from the relative addresses in HTML and JavaScript, which 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 it is used: request.getrequestdispatcher (address) in Forward:servlet; This address is parsed on the server side, so You want to forward to a.jsp should write: Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application WebApp, its 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 client's address all the relative address of the HTML page is relative to the server root directory (HTTP://192.168.0.1/), not (with the directory of the Web App directory) http://192.168.0.1/ Webapp/'s. 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 committed to a.jsp: action= "/webapp/user/a.jsp"    or action= "<%=request.getcontextpath ()%>"/user/a.jsp; commit to Servlet 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, such as reference to the css,javascript.action and other attributes are preferably preceded by <%=request.getcontextpath ()%> To ensure that the referenced files belong to a directory in your Web app. In addition, you should try to avoid the use of similar ".", "./", ". /.. /"Similar relative path relative to the location of the file, so that when the file is moved, it is prone to problems.

3. Obtain the relative path and absolute path of the current application in the Jsp/servlet    3.1 JSP to obtain the absolute path corresponding to the current app's relative path and absolute path root: Absolute path to the Request.getrequesturi () file    : Application.getrealpath (Request.getrequesturi ()); The absolute path of the current Web application: Application.getrealpath ("/"); Gets the upper-level directory of the requested file: New file (Application.getrealpath (Request.getrequesturi ())). GetParent ()

The

3.2 servlet obtains the absolute path corresponding to the current app's relative path and absolute path root: Request.getservletpath (); absolute path to file    : Request.getsession (). Getservletcontext (). Getrealpath (Request.getrequesturi ())     Absolute path to the current web app: Servletconfig.getservletcontext (). Getrealpath ("/");       (ServletContext objects get several ways:         Javax.servlet.http.HttpSession.getServletContext ()           Javax.servlet.jsp.PageContext.getServletContext ()           javax.servlet.ServletConfig.getServletContext ()        )

4.java class obtains relative path, absolute path method 4.1 In separate Java class to get absolute path according to Java.io.File Doc block, we know: New File by default ("/") The directory represented is: System.getproperty ("User.dir"). The program obtains 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 ("/"));//class file is located in the path System.out.println (new file ("/"). Getab       Solutepath ());   System.out.println (System.getproperty ("User.dir"));   } }

4.2 The Java class in the server obtains 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 you can access your server-side files: A. Use absolute path: For example, put your parameter file in C:\yourconfig\yourconf.properties, and use the new FileInputStream directly (" Yourconfig/yourconf.properties "); B. Using relative paths: the root of the relative path is the root of your webapplication, which is the top level of the Web-inf directory, which puts 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 to compile the path of the servlet to the root. For example, the new file method is used for the test of file F = "new" File ("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 Take the Spring di mechanism to get the file, avoid hard coding. Refer to the following connection content: http://www.javajia.net/viewtopic.php?p=90213& 5.2 configuration file read reference the following connection content: http://dev.csdn.net/develop/ Article/39/39681.shtm 5.3 Reads an XML file from a virtual path or relative path, avoiding hard-coded references to the following connection contents: 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 class 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

How Java creates \ Deletes \ modifies \ Replicates directories and files http://www.gamvan.com/developer/java/2005/2/264.html

Summary: Through the use of the above content can be resolved in the Web application server side, move files, find files, copy delete files and other operations, while the relative address of the server, the absolute address concept clearer. Recommended reference URI, RFC standard text block. At the same time, the understanding of Java.io.File Java.net.URI and other aspects can be more thorough and thorough. ==================================================================================

Reference: 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 the 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 gets the relative path and absolute path 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

Get file http://www.javajia.net/viewtopic.php?p=90213& with spring injection mechanism

Read http://dev.csdn.net/develop/article/39/39681.shtm for configuration files

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 class 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

From:http://www.blogjava.net/simie/archive/2007/07/29/133094.html

Relative path in Java development, complete summary of absolute paths

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.