Various ways to "reprint" Java to get paths

Source: Internet
Author: User

reproduced for individual study reference, please visit the original source:http://www.cnblogs.com/guoyuqiangf8/p/3506768.html

The main methods are:

(1),Request.getrealpath ("/"); Get the root path of the project is deprecated
(2),Request.getrealpath (Request.getrequesturi ()); //Get the path of JSP, this method is easy to use, can be used directly in servlet and JSP
(3),request.getsession (). Getservletcontext (). Getrealpath ("/"); //Get the project root path, 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 Project classes, this method can be used in any Jsp,servlet,java file, because either Jsp,servlet is actually a Java program, it is a class. So it should be a common method.

Preface, about absolute path and relative path

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: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 server-side relative address refers to the address of your Web application, this address is resolved on the server side (different from HTML and JavaScript relative address, they are resolved by the client browser)

1, Request.getrealpath

Method: Request.getrealpath ("/")
Get the path: C:\Program files\apache software Foundation\tomcat 5.5\webapps\strutstest\

Method: Request.getrealpath (".")
Get the path: C:\Program files\apache software foundation\tomcat 5.5\webapps\strutstest\.

Method: Request.getrealpath ("")
Get the 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");
Get the path: D:\VSS installation directory \users.txt

Method: String filepath = Actionform.getmyfile ();
Get the 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. You can do this in the Init method of the servlet

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

This will get the full path of the Web project

Example: E:\eclipseM9\workspace\tree\

Tree is the root directory of my Web project

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

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

Example:/d:/workspace/strutstest/webroot/web-inf/classes/

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

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

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

3.request.getcontextpath ();

Get the Web root context

such as/tree

Tree is the root context of my web project

5. Other 2

Several ways of obtaining paths in Java--

1. How does the JDK determine the path in the program? Generally in programming, the file path is divided into relative paths and absolute paths, the absolute path is relatively good processing, but not flexible, so we in the programming of the file operation, is generally read the relative path of the file,

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

2. Class.getClassLoader.getPath (): Get the path to the ClassLoader: What is the ClassLoader? The General class loader has the system and the user's own definition, the system ClassLoader is provided by the JDK, his path is the path under the JDK, or in JSP programming, such as Tomcat, the location of the class loader obtained is the path of Tomaca's own design loader.

After understanding these, the operation of the file path is quite clear, when we are programming, as long as we want to know that the file we are manipulating is relative to what path, the relative path can be achieved.

6. Summary

1. Get the file path under the Web server
Request.getrealpath ("/")
Application.getrealpath ("") "in JSP"
ServletContext (). Getrealpath ("")

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

2. Get Local Path

JSP,

Request.getparameter ("MyFile");
Actionform.getmyfile ();
Get the same value: such as 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:

In a Java file, getresource or getresourceasstream can be

Example: GetClass (). getResourceAsStream (FilePath);//filepath can be "/filename", here/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.

Various ways to "reprint" Java to get 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.