Various methods of Java to obtain paths (summary)
(1)、request. getRealPath("/");// Getting the root path of the project is not recommended
(2)、request. getRealPath(request.getRequestURI());// Get the path of JSP. This method is easy to use and can be used directly in servlets and JSPS
(3)、request. getSession(). getServletContext(). getRealPath("/");// Get the root path of the project. This method is easy to use and can be used directly in servlets and JSPS
(4)、 this. getClass(). getClassLoader(). getResource(""). getPath();// Get the path under the project classes. This method can be used in any JSP, servlet and Java file, because no matter JSP, servlet is actually a java program and a class. So it should be a general method.
0. About absolute path and relative path
1. Understanding of basic concepts absolute path: absolute path is the real path (URL and physical path) of the file or directory on your home page on the hard disk, such as C: XYZ est Txt stands for test Txt file. http://www.sun.com/index.htm It also represents an absolute URL path. Relative path: the path relative to a base directory. Contains the relative path of the web (the relative directory in HTML). For example, "/" in the servlet represents the following directory of the web application and the relative representation of the physical path. For example, ". /" represents the current directory, ".. /" Represents the parent directory. This similar representation also belongs to relative path. In addition, for URI, URL, urn and other contents, please refer to relevant RFC documents and standards. RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax,( http://www.ietf.org/rfc/rfc2396.txt )2. About relative path and absolute path in JSP / servlet. 2.1 server side address the server side relative address refers to the address relative to your web application, which is resolved on the server side (different from the relative addresses in HTML and JavaScript, which are resolved by the client browser)
1、request. getRealPath
Method: request getRealPath("/")
Obtained path: C: \ program files \ Apache Software Foundation \ Tomcat 5.5 \ webapps \ strutstest\
Method: request getRealPath(".")
Obtained path: C: \ program files \ Apache Software Foundation \ Tomcat 5.5 \ webapps \ strutstest \
Method: request getRealPath("")
Obtained 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");
Obtained path: D: \ VSS installation directory \ users txt
Method: string filepath = ActionForm getMyFile();
Obtained path: D: \ VSS installation directory \ users txt
--------------------------------------------------Struts test is the project name
Myfile in ActionForm is private string myfile;
In the JSP page: it is < HTML: file property = "myfile" > < / HTML: File >
--------------------------------------------------
3. Get system path
In application:
System. getProperty("user.dir")
In the servlet:
ServletContext servletContext = config. getServletContext();
String rootPath = servletContext. getRealPath("/");
In jsp:
application. getRealPath("")
4. Other 1
1. It can be in the init method of servlet
String path = getServletContext(). getRealPath("/");
This will get the full path of the web project
For example: e: \ eclipse M9 \ 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 effective
this. getClass(). getResource("/conf"). getPath();// After testing, this method is also safe
This will get the full path to the classes directory
For example: / D: / workspace / strutstest / Webroot / WEB-INF / classes/
And this getClass(). getResource(""). getPath(). toString();
This will get / D: / workspace / strutstest / Webroot / WEB-INF / classes / BL/
This method can also not determine the path in the web environment, which is easier to use
3.request. getContextPath();
Get the context of the web root
E.g. / tree
Tree is the root context of my web project
5. Other 2
Several ways to obtain path in Java-
1. How does JDK determine the path in the program? Generally, in programming, the file path is divided into relative path and absolute path. The absolute path is easy to handle, but it is not flexible. Therefore, when we operate the file in programming, we usually read the relative path of the file,
The relative path may be a little complicated, but it is also relatively simple. The relative path is mainly relative to who. It can be the path of the classloader or the path under the current java file. In JSP programming, it may be the path relative to the site. Relative to the site, we can use 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 path of the classloader: what is a classloader? General class loaders are systematic and user-defined; The classloader of the system is provided by the JDK, and its path is the path under the JDK, or the location of the classloader obtained in JSP programming, such as tomcat, is the path of the loader designed by tomaca,
After understanding these, the operation of file path will be quite clear. When programming, we just want to know what path the file we operate is relative to and obtain the relative path
6. Summary
1. Get the file path under the web server
request. getRealPath("/") application. Getrealpath ("") [in JSP] servletcontext(). Getrealpath ("")
System. Getproperty ("user. Dir") [call from different locations, and the obtained path changes dynamically]
2. Get local path
JSP, < HTML: file property = "myfile" / >
request. getParameter("myFile"); ActionForm. getMyFile();
The values obtained 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. Get relative path
request. getContextPath();
In addition:
Either getresource or getresourceasstream can be in the java file
Example: getclass(). Getresourceasstream (filepath); / / filepath can be "/ filename", where / represents WEB-INF / classes under the web publishing root path. The default path using this method is: WEB-INF / classes. It has been tested in Tomcat.
The above various methods (summary) of obtaining paths in Java are all the contents shared by Xiaobian. I hope to give you a reference and support more programming tips.