Handling virtual path _jsp programming in JSP

Source: Internet
Author: User

Summary
When programming for server-side components, you are likely to get a true path to a file from a path relative to the Web root, but this file is actually on a virtual path in the site.
What is a virtual path?
On a Web server, the virtual path combines the physically separated files on the same site path, and on the application server, each application is positioned on its own virtual path, actually separating from each other perfectly.
Getrealpath () method
The JSP servlet API provides a Getrealpath (path) method that returns the true path of a given virtual path and returns null if a conversion error occurs.
Getrealpath Syntax Definition:
Public java.lang.String Getrealpath (java.lang.String path)
Returns a string containing the true path of a given virtual path. For example, the virtual path "/index.html"
No matter what the true path is on the server file system, you can always find it by using "/index.html". The true path returned uses a format similar to the computer or operating system where the servlet container (Srvlet container) resides, including the appropriate path separator. This method returns null if the servlet container cannot be converted.
Parameters:
Path-A string that describes the virtual path
return value:
String that describes the real path or null
Unfortunately, Getrealpath often returns different things, depending on the path location where the server or JSP file calls this method.
A example site
Suppose our site is organized as follows:
The root path contains the root of our site: http://address/
The A_virtual directory contains files for the virtual path provided by our site, such as:
http://addess/virtual_dir/
We look for the real paths of File1.txt and File2.txt, one under the site root path and one under the virtual path.
Getrealpath ("/file1.txt") should return "C:\site\site_root\file1.txt",
Getrealpath ("/virtual_dir/file2.txt") should return "C:\site\a_virtual\file2.txt"
Getrealpath ("/file3.txt") should return null because the file does not exist.
But Getrealpath () does not always return the same result, depending on the JS engine you use.
JSP engine
Tomcat 3.1
The result of Tomcat return has an applied independence (application dependant):
It depends on the location of the JSP file that invokes the Getrealpath method.
In fact, when page1.jsp (at the site root) calls Txtgetrealpath () on File1.txt and File2.txt, it returns the correct result. (This is the path to the File2.txt return error for Tomcat 3.1, version 3.0)
But when page2.jsp (in another application, in a virtual path) calls Getrealpath, it returns the wrong path: it connects to the path where the JSP file resides and the virtual path of the request.
For example, calling Getrealpath (/file1.txt) from page2.jsp will return C:\site\a_virtual\file1.txt.
This behavior is actually a typical approach to making different applications independent of each other.
JRun 2.3.3 and Inprise application SERVER 4.0 (IAS)
JRun and IAS return the correct results for both file1.txt and File2.txt.
However, all of these engines have a common behavior: they do not return when Getrealpath handles files that do not exist null!
The way to solve it
Since Getrealpath always returns a path, how do we know if it is correct? The easiest way to do this is to check whether the returned path exists.
This is what the IsVirtual method does: After invoking Getrealpath on a given file, it uses java.io to
Access to this file, so you can know if it exists.
/**
* IsVirtual
*
* Check If the path name is a virtual or not.
*
* @param pathName The name of the path to check.
*/
Private Boolean isvirtual (String pathName) {
Check If it is a virtual path
if (M_application.getrealpath (pathName)!=null) {
Java.io.File virtualfile = new Java.io.File (M_application.getrealpath (pathName));
if (virtualfile.exists ()) {return true;}
else {return false;}
}
else {return false;}
}
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.