(1), request.getRealPath("/");//It is not recommended to use the root path of the project to obtain the project
(2), request.getRealPath(request.getRequestURI());//Get the path to jsp. This method is easier to use and can be used directly in servlets and jsp
(3), request.getSession().getServletContext().getRealPath("/");//Get the root path of the project. This method is easier to use and can be used directly in servlet and jsp
(4), this.getClass().getClassLoader().getResource("").getPath();//Get the path under the project classes. This method can be used in any jsp, servlet, and java files, because no matter it is jsp, servlet, are actually Java programs and are all classes. So it should be a general approach.
0. About absolute and relative paths
1. Understanding the basic concepts Absolute path: The absolute path is the real path of the file or directory on your homepage on the hard disk (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 an absolute URL path. Relative path: The path relative to a certain benchmark directory. Contains the relative path of the web (relative directory in HTML), for example: in a servlet, "/" represents the directory of the web application. and the relative representation of the physical path. For example: "./" represents the current directory, "../" represents the previous directory. This similar representation also belongs to the relative path. In addition, for URI, URL, URN, etc., please refer to the RFC related documentation standards. RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax,(http://www.ietf.org/rfc/rfc2396.txt)2. Regarding relative and absolute paths in JSP/Servlets. 2.1 Server-side Address The relative address of the server-side refers to the address of your web application. This address is parsed on the server side (unlike the relative addresses in html and javascript, they are parsed by the client browser)
1. request.getRealPath
Method: request.getRealPath("/")
The path obtained: C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/strutsTest/
Method: request.getRealPath(".")
The path obtained: C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/strutsTest/.
Method: request.getRealPath("")
The path obtained: 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 path obtained: D:/VSS installation directory/users.txt
Method: String filepath = ActionForm.getMyFile();
The path obtained: D:/VSS installation directory/users.txt
--------------------------------------------------
strutsTest is the project name
myFile In ActionForm, private String myFile;
In the jsp page: <html:file property="myFile"></html:file>
--------------------------------------------------
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. Other 1
1. It can be in the init method of servlet
String path = getServletContext().getRealPath("/");
This will get the full path to the web project
For example: E:/eclipseM9/workspace/tree/
tree is the root directory of my web project
2. You can also call it in any class at any time
this.getClass().getClassLoader().getResource("").getPath(); // After testing, this method is safe and most 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/
There is also this.getClass().getResource("").getPath().toString();
This will get /D:/workspace/strutsTest/WebRoot/WEB-INF/classes/bl/
This method can also be used without determining the path in the web environment.
3.request.getContextPath();
Get the context environment of the web root
Like /tree
tree is the root context of my web project
5. Other 2
Several ways to obtain paths in java - -
1. How does jdk determine the path in the program? Generally, in programming, file paths are divided into relative paths and absolute paths. Absolute paths are easier to handle, but they are not flexible. Therefore, when we operate files in programming, we usually read the relative paths of the file.
The relative path may be a little more complicated, but it is also relatively simple. The relative path, mainly relative to whom, can be the path of the class loader, or the path under the current Java file. In jsp programming, it may be the path of the site. Compared with the path of the site, we can use 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 class loader: What is a class loader? Generally, class loaders are defined by the system and users themselves; the system's ClassLoader is provided by jdk, and its path is the path under jdk, or in jsp programming, such as Tomcat, the location of the class loader obtained is the path of the loader designed by tomaca.
After understanding these, the operation of file paths will be quite clear. When we are programming, we just need to figure out what path the file we are operating is relative to and get the relative path.
6. Summary
1. Get the file path under the web server
request.getRealPath("/")
application.getRealPath("")【jsp】
ServletContext().getRealPath("")
System.getProperty("user.dir") [Called at different locations, the path obtained is dynamically changed]
2. Get the local path
In jsp, <html:file property="myFile"/>
request.getParameter("myFile");
ActionForm.getMyFile();
The obtained value is the same: 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 the relative path
request.getContextPath();
in addition:
GetResource or getResourceAsStream is available in Java files
Example: getClass().getResourceAsStream(filePath); //filePath can be "/filename", here / represents the default path of WEB-INF/classes under the web publishing root path: WEB-INF/classes. It has been tested in Tomcat.
The above various methods (summary) of Java obtaining paths are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.