Java * path acquisition implementation

Source: Internet
Author: User

(1) request. getRealPath ("/"); // It is not recommended to obtain the root path of the project.
(2) request. getRealPath (request. getRequestURI (); // obtain the jsp path. This method is easy to use and can be directly used in servlet and jsp.
(3), request. getSession (). getServletContext (). getRealPath ("/"); // obtain the root path of the project. This method is easy to use and can be directly used in servlet and jsp.
(4), this. getClass (). getClassLoader (). getResource (""). getPath (); // obtain the path under the project classes. This method can be used in any jsp, servlet, or java file, because both jsp and servlet are actually java programs, is a class. So it should be a general method.

0. absolute and relative paths

1. basic concepts: absolute paths are the true paths of files or directories on your home page on the hard disk. Examples of URL and physical paths are as follows: C: xyz est.txt represents the absolute path of the test.txt file. Http://www.sun.com/index.htmalso represents a urlabsolute volume. Relative Path: the path relative to a base quasi-directory. Contains the relative path of the Web (relative directory in HTML). For example, in Servlet, "/" indicates the directory of the Web application. The relative representation of the physical path. For example, "./" indicates the current directory, and "../" indicates the upper-level directory. This similar representation is also a relative path. For more information about URI, URL, and URN, see RFC standards. RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, (http://www.ietf.org/rfc/rfc2396.txt) 2. About relative paths and absolute paths in JSP/Servlet. 2.1 server-side address the server-side relative address refers to the address of your web application, which is parsed on the server side (different from the relative address in html and javascript, they are parsed by the client browser)

1. request. getRealPath

Method: request. getRealPath ("/")
Path: C: \ Program Files \ Apache Software Foundation \ Tomcat 5.5 \ webapps \ strutsTest \

Method: request. getRealPath (".")
Path: C: \ Program Files \ Apache Software Foundation \ Tomcat 5.5 \ webapps \ strutsTest \.

Method: request. getRealPath ("")
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 ");
Path: D: \ VSS installation directory \ users.txt

Method: String filepath = ActionForm. getMyFile ();
Path: D: \ VSS installation directory \ users.txt

--------------------------------------------------
StrutsTest is the project name

MyFile is private String myFile in ActionForm;
On the jsp page:
--------------------------------------------------

3. Obtain the system path

In Application:
System. getProperty ("user. dir ")

In Servlet:
ServletContext servletContext = config. getServletContext ();
String rootPath = servletContext. getRealPath ("/");

In jsp:
Application. getRealPath ("")

4. Miscellaneous 1

1. You can use the init method of servlet.

String path = getServletContext (). getRealPath ("/");

This will obtain the full path of the web Project

Example: E: \ eclipseM9 \ workspace \ tree \

Tree is the root directory of my web project

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

This. getClass (). getClassLoader (). getResource (""). getPath ();

This will get the full path of the classes directory

Example:/D:/workspace/strutsTest/WebRoot/WEB-INF/classes/

This. getClass (). getResource (""). getPath (). toString ();

This will get/D:/workspace/strutsTest/WebRoot/WEB-INF/classes/bl/

This method can also be used to determine the path in the web environment.

3. request. getContextPath ();

Obtain the context of the web root

For example,/tree

Tree is the root context of my web project

5. Other 2

Several ways for java to obtain paths --
1. How does jdk determine the path in the program? Generally, in programming, file paths are classified into relative paths and absolute paths. absolute paths are relatively easy to process, but not flexible. Therefore, when we perform operations on files in programming, it is generally the relative path for reading files,

The relative path may be a little more complex, but it is also relatively simple. The relative path is mainly relative to who, it can be the path of the class loader or the path under the current java file, in jsp programming, the path may be relative to the site path. Compared to the site path, we can use getServletContext (). getRealPath ("\") and request. getRealPath ("\"): This is the absolute path for obtaining the site, while getContextPath (): gets the virtual path of the site;

2. class. getClassLoader. getPath (): Get the class Loader path: What is the class loader? Generally, the class loader has a system defined by the user. The ClassLoader of the system is provided by jdk, and its path is the path under jdk, or it is programmed in jsp, such as Tomcat, the position of the obtained class loader is the path of the loader designed by tomaca,

After understanding this, the operations on the file path will be quite clear. When programming, we only need to figure out what path the files we operate are relative, you can obtain the relative path.

6. Summary

1. Obtain the file path under the web Server
Request. getRealPath ("/")
Application. getRealPath ("") [in jsp]
ServletContext (). getRealPath ("")

System. getProperty ("user. dir") [call at different locations and the obtained path changes dynamically]

2. Obtain the local path

In jsp,
Request. getParameter ("myFile ");
ActionForm. getMyFile ();
The obtained values are the same, for example, 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. Obtain the relative path

Request. getContextPath ();

For example:/strutsTest

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.