In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about the absolute path and relative path of java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
1. Understanding of basic concepts
Absolute path: the absolute path is the real 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 to the test.txt file.
Relative path: the path relative to a base directory. Contains the relative path of Web (relative directory in HTML), for example: in
In Servlet, "/" represents the directory of the Web application. Relative to the physical path. For example: ". /" represents the current directory, and ".. /" represents the parent directory. This similar representation also belongs to the relative path.
RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax
two。 About relative paths and absolute paths in JSP/Servlet.
2.1 address on the server side
The relative address on the server side refers to the address of your web application, which is resolved on the server side (unlike the relative address in html and javascript, they are resolved by the client browser), that is to say, the relative address in jsp and servlet should be relative to your web application.
It is used in the following places:
Request.getRequestDispatcher (address) in forward:servlet; this address is parsed on the server side, so if you want to forward to a.jsp, you should write: request.getRequestDispatcher ("/ user/a.jsp") this / relative to the current web application webapp, its absolute address is: http://192.168.0.1/webapp/user/a.jsp. SendRedirect: in jsp
2.22. Address of the client
The relative addresses in all html pages are relative to the server root directory (http://192.168.0.1/)), not http://192.168.0.1/webapp/ (with the directory of the Web application under the directory). The address of the action attribute of the form form in Html should be relative to the server root directory (http://192.168.0.1/), so if submitted to the a.jsp is: action= "/ webapp/user/a.jsp" or action= "" / user/a.jsp
The actiom= "/ webapp/handleservlet" Javascript submitted to servlet is also resolved on the client side, so its relative path is the same as the form form.
Therefore, in general, it is best to precede attributes such as CSS,Javascript.Action referenced by JSP/HTML pages with
To ensure that the referenced files belong to directories in the Web application In addition, you should try to avoid using similar relative paths to the location of the file, such as ".", ". /", ".. /" and so on, so that when the file moves, it is easy to cause problems.
3. Obtain the relative and absolute paths of the current application in JSP/Servlet
3.1 obtain the relative and absolute paths of the current application in JSP
Absolute path corresponding to the root directory: request.getRequestURI ()
Absolute path to the file: application.getRealPath (request.getRequestURI ())
Absolute path of current web application: application.getRealPath ("/")
Get the upper directory of the request file: new File (application.getRealPath (request.getRequestURI () .getParent ()
3.2 obtain the relative and absolute paths of the current application in Servlet
Absolute path corresponding to the root directory: request.getServletPath ()
The absolute path to the file: request.getSession (). GetServletContext (). GetRealPath
(request.getRequestURI ())
The absolute path of the current web application: servletConfig.getServletContext () .getRealPath ("/")
(the ServletContext object can be obtained in several ways:
Javax.servlet.http.HttpSession.getServletContext ()
Javax.servlet.jsp.PageContext.getServletContext ()
Javax.servlet.ServletConfig.getServletContext ()
)
The method of obtaining relative path and absolute path in Class of 4.java
4.1 get the absolute path in a separate Java class
According to java.io.File 's Doc document, you can see:
By default, the directory represented by new File ("/") is: System.getProperty ("user.dir").
The following program gets the current path of the execution class
Package org.cheng.file
Import java.io.File
Public class FileTest {
Public static void main (String [] args) throws Exception {
System.out.println (Thread.currentThread (). GetContextClassLoader (). GetResource ("))
System.out.println (FileTest.class.getClassLoader () .getResource ("))
System.out.println (ClassLoader.getSystemResource ("))
System.out.println (FileTest.class.getResource ("))
System.out.println (FileTest.class.getResource (/))
/ / path where the Class file is located
System.out.println (new File ("/") .getAbsolutePath ()
System.out.println (System.getProperty ("user.dir"))
}
}
4.2 the Java class in the server gets the current path (from the network)
(1) Weblogic
WebApplication's system file root directory is the root directory where your weblogic installation is located.
For example, if your weblogic is installed on c:eaweblogic700.
So, your file root path is cvv.
So, there are two ways to access your server-side files:
a. Use absolute paths:
For example, put your parameter file in c:yourconfigyourconf.properties
Use new FileInputStream directly ("yourconfig/yourconf.properties")
b. Use relative paths:
The root directory of the relative path is the root path of your webapplication, that is, the directory one level above WEB-INF, and put your parameter files
In yourwebappyourconfigyourconf.properties
Use as follows:
New FileInputStream (". / yourconfig/yourconf.properties")
You can choose either of these ways.
(2) Tomcat
Output System.getProperty ("user.dir") in the class; shows Tomcat_Home%/bin
(3) Resin
It's not the relative path that your JSP puts, it's the JSP engine that executes the JSP to compile into SERVLET.
The path is the root. For example, use the new file method to test File f = new File ("a.htm")
This a.htm is in the installation directory of resin
(4)。 How to read the relative path?
Either getResource or getResourceAsStream can be found in the Java file
Example: getClass () .getResourceAsStream (filePath); / / filePath can be "/ filename", where / stands for web
WEB-INF/classes under the release root path
The default path to use this method is: WEB-INF/classes. Has been tested in Tomcat.
5. The relative path when reading the file, avoiding the use of hard coding and absolute path.
These are the absolute and relative paths of java shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.