Jsp to obtain the current absolute path

Source: Internet
Author: User

In Asp, we can use Server. Mappath (".") to directly obtain the absolute path (also called the physical path) of the directory where the current program is located ),

However, in jsp (the preferred choice for SUN Enterprise applications), there seems to be no direct way to obtain the absolute path of the current directory.

Request. getRealPath () is only the absolute path corresponding to the root directory. After reading the Servlet API documentation, you can

Obtain the absolute path name of the current file in this way, for example, in my cqq. jsp (preferred for SUN Enterprise Applications) file:

<%

String strAbsPath = application. getRealPath (request. getRequestURI ());

Out. println (strAbsPath );

%>

In fact, the application object in jsp (the preferred choice for SUN Enterprise Applications) is the ServerContext in Servlet.

The obtained path is D: aaabbccccqq. jsp (preferred for SUN Enterprise Applications). If we do not want the file name,

If we want to get the path D: aaabbccc, we can use the File class directly.


The complete statement should be as follows:

String strAbsPath = new java. io. File (application. getRealPath (request. getRequestURI (). getParent ();

 

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.