Discussion on Java acquisition * PATH implementation _JSP programming
Source: Internet
Author: User
(1), Request.getrealpath ("/");//Do not recommend using the root path of the acquisition project
(2), Request.getrealpath (Request.getrequesturi ());//Get the path of the JSP, this method is easy to use, can be used directly in the servlet and JSP
(3), Request.getsession (). Getservletcontext (). Getrealpath ("/")//Get the root path of the project, this method is easy to use, can be used directly in the servlet and JSP
(4), This.getclass (). getClassLoader (). GetResource (""). GetPath ()//Get the path under the project classes, this method can be in any jsp,servlet, Java files, because no matter jsp,servlet is actually a Java program, is a class. So it should be a common method.
0, about absolute path and relative path
1. The basic concept of Understanding absolute path: Absolute path is your home page file or directory on the hard disk real path, (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. Contains the relative path of the web (relative directories in HTML), for example: In a servlet, "/" represents a Web application's directory. 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. For additional information about Uri,url,urn, please refer to the RFC related documentation standard. RFC 2396:uniform Resource Identifiers (URI): Generic Syntax, (http://www.ietf.org/rfc/rfc2396.txt) 2. About jsp/ Relative and absolute paths in the servlet. 2.1 Server-side address server-side relative address is relative to your Web application address, this address is resolved on the server side (different from the HTML and JavaScript in the relative address, they are resolved by the client browser)
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 to the/d:/workspace/strutstest/webroot/web-inf/classes/bl/
This method can also not determine the path in the Web environment, more useful
3.request.getcontextpath ();
Get the context environment for web roots
such as/tree
Tree is the root context of my web project
5, other 2
Java Access path several ways--
1. How does the JDK determine the path in the program? In general programming, the file path is divided into relative path and absolute path, absolute path is better to deal with, but not flexible, so we are in the programming of files to operate, is generally read the relative path of the file,
A relative path can be a bit more complex, but also relatively simple, relative path, mainly relative to who, can be the path of the ClassLoader, or the current Java file path, in the JSP programming may be relative to the site of the path, relative to the site's path, we can through 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 class loader path: What is a classloader? The General class loader has the system and the user defines, the system ClassLoader is the JDK to provide, his path is the JDK path, or in the JSP programming, for instance Tomcat, obtains the class loader position is Tomaca own design loader's path,
Understand these, the operation of the file path will be quite clear, when we are programming, as long as we want to know the file we are operating relative to what path, to obtain a relative path can be.
6, Summary
1, get the file path under the Web server
Request.getrealpath ("/")
Application.getrealpath ("") "JSP"
ServletContext (). Getrealpath ("")
System.getproperty ("User.dir") "called in different locations, the path obtained is dynamically changing"
2. Get Local Path
JSP,
Request.getparameter ("MyFile");
Actionform.getmyfile ();
The values obtained are the same: D:\VSS installation directory \users.txt
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