Methods for obtaining the current directory in JSP _jsp programming

Source: Internet
Author: User

This article describes the JSP to obtain the current directory implementation methods, share for everyone to reference. The implementation methods are as follows:

1, using the System.getproperty () function to get the current path:

Copy Code code as follows:
System.out.println (System.getproperty ("User.dir"));//user.dir specified the current path

2, use the function provided by the file to get the current path:
Copy Code code as follows:
File directory = new file ("");//Set as current folder
try{
System.out.println (Directory.getcanonicalpath ());//Get the standard path
System.out.println (Directory.getabsolutepath ());//Get absolute path
}catch (Exceptin e) {}

File.getcanonicalpath () and File.getabsolutepath () are about just the new File (".") and new File ("..") There is a difference between these two paths.

# for the Getcanonicalpath () function, "." Represents the current folder, and the "..." Represents the folder up to the current folder
# for the GetAbsolutePath () function, regardless of ".", "...", return the current path plus the path you set at New File ()
# As for the GetPath () function, you get only the path you set at New File ()

For example, the current path is c:test:

Copy Code code as follows:
File directory = new file ("abc");
Directory.getcanonicalpath (); Got a c:testabc.
Directory.getabsolutepath (); Got a c:testabc.
Direcotry.getpath (); I got ABC.
File directory = new file (".");
Directory.getcanonicalpath (); Got a c:test.
Directory.getabsolutepath (); Get the c:test.
Direcotry.getpath (); Get is.
File directory = new file ("..");
Directory.getcanonicalpath (); Get the C:
Directory.getabsolutepath (); Get the c:test.
Direcotry.getpath (); Get a..

Get the current working directory for JAVA Programs

Copy Code code as follows:
File File = new file ("T.tmp");
String FullPath = File.getabsolutepath ();

①request.getrealpath:

Method: Request.getrealpath ("/")
The resulting path: C:Program filesapache Software foundationtomcat 5.5webappsstrutsTest

Method: Request.getrealpath (".")
The resulting path: C:Program filesapache Software foundationtomcat 5.5webappsstrutsTest.

Method: Request.getrealpath ("")
The resulting path: C:Program filesapache Software foundationtomcat 5.5webappsstrutsTest

Method: Request.getrealpath ("Web.xml")
The resulting path: C:Program filesapache Software foundationtomcat 5.5webappsstrutstestweb.xml

②request.getparameter ("");
Actionform.getmyfile ();
Method: String filepath = request.getparameter ("MyFile");
The resulting path: D:vss installation directory Users.txt

Method: String filepath = Actionform.getmyfile ();
The resulting path: D:vss installation directory Users.txt

I hope this article will help you with the JSP program design.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.