Various methods of Java Fetch Path (summary) _java

Source: Internet
Author: User
Tags parent directory relative rfc

(1),Request.getrealpath ("/");//Do not recommend using the root path of the acquisition project

(2),Request.getrealpath (Request.getrequesturi ());//Get the path of the JSP, this method is easy to use, can be used directly in the servlet and JSP

(3),request.getsession (). Getservletcontext (). Getrealpath ("/")//Get the root path of the project, this method is easy to use, can be used directly in the servlet and JSP

(4), This.getclass (). getClassLoader (). GetResource (""). GetPath ()//Get the path under the project classes, this method can be in any Jsp,servlet , Java files, because whether Jsp,servlet is actually a Java program, is a class. So it should be a common method.

0, about absolute path and relative path

1, the basic concept of Understanding absolute path: absolute path is your home page file or directory on the hard disk real path, (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 a Web application's directory. 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. For additional information about Uri,url,urn, please refer to the RFC related documentation standard. RFC 2396:uniform Resource Identifiers (URI): Generic Syntax, (http://www.ietf.org/rfc/rfc2396.txt) 2. About jsp/ Relative and absolute paths in the servlet. 2.1 Server-side address server-side relative address is relative to your Web application address, this address is resolved on the server side (different from the HTML and JavaScript in the relative address, they are resolved by the client browser)

1, Request.getrealpath

Method: Request.getrealpath ("/")

The resulting path: C:\Program files\apache Software foundation\tomcat 5.5\webapps\strutstest\

Method: Request.getrealpath (".")

The resulting path: C:\Program files\apache Software foundation\tomcat 5.5\webapps\strutstest\.

Method: Request.getrealpath ("")

The resulting path: C:\Program files\apache Software foundation\tomcat 5.5\webapps\strutstest

Request.getrealpath ("Web.xml")

C:\Program Files\apache Software Foundation\tomcat 5.5\webapps\strutstest\web.xml

2, Request.getparameter ("");

Actionform.getmyfile ();

Method: String filepath = request.getparameter ("MyFile");

The resulting path: D:\VSS installation directory \users.txt

Method: String filepath = Actionform.getmyfile ();

The resulting path: D:\VSS installation directory \users.txt

--------------------------------------------------
Strutstest for Project name

MyFile in Actionform, for private String myFile;

In the JSP page: for

--------------------------------------------------

3. Get the system path

In the application:

System.getproperty ("User.dir")

In the servlet:

ServletContext ServletContext = Config.getservletcontext ();

String RootPath = Servletcontext.getrealpath ("/");

In the JSP:

Application.getrealpath ("")

4, other 1

1. Can be in the servlet's Init method

String path = Getservletcontext (). Getrealpath ("/");

This gets the full path to the Web project

For example: E:\eclipseM9\workspace\tree\

Tree is the root of my web project

2. You can also call at any time in any class

This.getclass (). getClassLoader (). GetResource (""). GetPath (); After testing, this method is safe and the most effective

This.getclass (). GetResource ("/conf"). GetPath ().//Tested, this method is also safe

This will get the full path to the classes directory

For example:/d:/workspace/strutstest/webroot/web-inf/classes/

There is also This.getclass (). GetResource (""). GetPath (). toString ();

This will get to the/d:/workspace/strutstest/webroot/web-inf/classes/bl/

This method can also not determine the path in the Web environment, more useful

3.request.getcontextpath ();

Get the context environment for web roots

such as/tree

Tree is the root context of my web project

5, other 2

Java Access path several ways--

1. How does the JDK determine the path in the program? in general programming, the file path is divided into relative path and absolute path, absolute path is better to deal with, but not flexible, so we are in the programming of files to operate, is generally read the relative path of the file,

A relative path can be a bit more complex, but also relatively simple, relative path, mainly relative to who, can be the path of the ClassLoader, or the current Java file path, in the JSP programming may be relative to the site of the path, relative to the site's path, we can through Getservletcontext (). Getrealpath ("\") and Request.getrealpath ("\"): This is the absolute path to get the site; Getcontextpath (): Get the virtual path of the site;

2. Class.getClassLoader.getPath (): Get the class loader path: What is a classloader? The General class loader has the system and the user defines, the system ClassLoader is the JDK to provide, his path is the JDK path, or in the JSP programming, for instance Tomcat, obtains the class loader position is Tomaca own design loader's path,

Understand these, the operation of the file path will be quite clear, when we are programming, as long as we want to know the file we are operating relative to what path, to obtain a relative path can be.

6, summary

1, get the file path under the Web server

Request.getrealpath ("/")
Application.getrealpath ("") "JSP"
ServletContext (). Getrealpath ("")

System.getproperty ("User.dir") "called in different locations, the path obtained is dynamically changing"

2. Get Local Path

JSP,

Request.getparameter ("MyFile");
Actionform.getmyfile ();

The values obtained are the same: D:\VSS installation directory \users.txt

*********************************

This.getclass (). getClassLoader (). GetResource (""). GetPath ();
==/d:/workspace/strutstest/webroot/web-inf/classes/
This.getclass (). GetResource (""). GetPath (). toString ();
==/d:/workspace/strutstest/webroot/web-inf/classes/bl/

3. Get relative path

Request.getcontextpath ();

Other than that:

GetResource or getResourceAsStream in a Java file can be

Example: GetClass (). getResourceAsStream (FilePath);//filepath can be "/filename", where/on behalf of the Web publishing root path web-inf/ Classes the path to use this method by default is: Web-inf/classes. has been tested in Tomcat.

The above Java access path of the various methods (summary) is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.