Discussion on realization of Java access * path

  • 2020-06-23 01:41:27
  • OfStack

(1), request. getRealPath ("/"); // Getting the root path of a project is not recommended
(2), request. getRealPath (request getRequestURI ()); // Get the path to jsp. This method is useful and can be used directly in servlet and jsp
(3), request. getSession () getServletContext () getRealPath ("/"); // Get the root path of the project. This method is useful and can be used directly in servlet and jsp
(4), this. getClass () getClassLoader () getResource (" "). getPath (); This method can be used in any jsp, servlet, java file, as both jsp and servlet are java programs. So it should be a general method.

0. About absolute path and relative path

Absolute path: An absolute path is the actual path of the file or directory on your home page on your 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 a URL absolute path. Relative path: The path relative to a base directory. The relative path that contains Web (relative directory in HTML), for example, in Servlet, "/" represents the directory where Web applies. And 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. In addition, regarding URI, URL,URN and other contents, please refer to RFC related document standards. RFC 2396: Uniform Resource Identifiers (URI) : Generic Syntax, (http: / / www. ietf. org/rfc/rfc2396 txt) 2. The relative path about JSP/Servlet and absolute path. 2.1 Server-side Address Server-side relative address refers to the address of your web application that is resolved on the server side (unlike the relative addresses in html and javascript, which are resolved by the client browser)

1, request. getRealPath

Methods: request. getRealPath ("/")
The resulting path: C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest\

Methods: request. getRealPath (". ")
The resulting path: C:\Program Files\Apache Foundation\Tomcat 5.5\webapps\strutsTest\.

Methods: request. getRealPath (" ")
The resulting path: C:\Program Files\ 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();

Methods: String filepath = request.getParameter("myFile");
The resulting path: D:\VSS Installation directory \ users.txt

Methods: String filepath = ES149en. getMyFile();
The resulting path: D:\VSS Installation directory \ ES154en.txt

--------------------------------------------------
strutsTest is the project name

myFile in ActionForm, private String myFile;
On the jsp page: yes < html:file property="myFile" > < /html:file >

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

3. Get 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. In servlet init

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 from any class at any time

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

This gets the full path to the classes directory

For example: / D: / workspace strutsTest/WebRoot/WEB - INF/classes /

And this. getClass (.) getResource (" "). getPath () toString ();

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

This method can also be used to determine the path outside of the web environment

3.request.getContextPath();

Get the context of the web root

Such as/tree

tree is the root context of my web project

5. Other 2

java gets paths in several ways --
1. How does jdk determine the path in a program? Generally, in programming, file path is divided into relative path and absolute path. Absolute path is relatively easy to handle, but not flexible. Therefore, when we operate on files in programming, 1 usually reads relative path of files.

Relative paths may be complex 1 point, but also is relatively simple, relatively, the main is relative to the who, can be the path of the class loader, or is the current java file path, in jsp programming may be relative to the path of the site, relative to the path of the site, we can through getServletContext () getRealPath (" \ ") and request getRealPath (" \ ") : this is the absolute path of the site; getContextPath(): get the virtual path of the site;

class. getClassLoader. getPath(): Get the path to the classloader: What is a classloader? 1 General class loaders are systematic and user-defined; ClassLoader of the system is provided by jdk, and its path is the path under jdk, or programming in jsp, such as Tomcat, the position of the classloader obtained is the path of the loader designed by tomaca itself.

With this in mind, the operation of the file path will be quite clear. When we are programming, we just need to figure out what path the file we are operating on is relative to and get the relative path.

6, summary

1. Get the file path under web server
request.getRealPath("/")
application.getRealPath("") [jsp medium]
ServletContext().getRealPath("")

System.getProperty (" user.dir ") [called from different locations, the path acquired is dynamically changed]

2. Get the local path

jsp, < html:file property="myFile"/ >

request.getParameter("myFile");
ActionForm.getMyFile();
Get the same value: D:\VSS installation directory \ ES337en.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();

Such as: / strutsTest

Related articles: