Process virtual paths in JSP

Source: Internet
Author: User


Summary
When programming for server components, you may need to obtain the actual path of a file from the path relative to the web root, but this file is actually on a virtual path of the site.
What is a virtual path?
On a web server, the virtual path combines physically separated files and places them on the same site path on the application server, each application is positioned on its own virtual path, which is actually perfectly separated from each other.
GetRealPath () method
The JSP servlet API provides the getRealPath (path) method to return the real path of the given virtual path. If the conversion is incorrect, null is returned.
GetRealPath syntax definition:
Public java. lang. String getRealPath (java. lang. String path)
Returns a string containing the real path of a given virtual path. For example, the virtual path "/index.html"
Regardless of the actual path on the server file system, you can always find it using "/index.html. The actual returned PATH uses a format similar to that of the computer or operating system where the servlet container (srvlet container) is located, and contains the appropriate path separator. If the servlet container cannot be converted, this method returns null.
Parameters:
Path-a string that describes the virtual path
Return Value:
String that describes the real path or null
Unfortunately, getRealPath often returns different items, depending on the path location where the server or jsp file calls this method.
One example site
Assume that our site is organized as follows:
The root path contains the root of our site: http: // address/
The a_virtual directory contains the virtual path files provided by our site. For example:
Http: // addess/virtual_dir/
Find the true paths of file1.txtand file2.txt, one under the root path of the site and the other 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.
However, getRealPath () does not always return the same result, which depends on the js engine you are using.
JSP Engine
Tomcat 3.1
Application dependant ):
It depends on the location of the jsp file that calls the getRealPath method.
In fact, when page1.jsp (in the Origin Site) calls txtgetRealPath () to file1.txtand file2.txt, it returns the correct result. (This is the Error Path returned for file2.txt in tomcat 3.1 and versions)
However, when page2.jsp (located in another application, in a virtual path) calls getRealPath, it returns the wrong path: It connects the path of the jsp file 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 line is a typical processing method that makes different applications independent from each other.
JRun 2.3.3 and inprise application server 4.0 (IAS)
Both jrunand iasreturn correct results for file1.txtand file2.txt.
However, all these engines share the same behavior: When getRealPath processes non-existent files, they do not return null!
Solution
Since getRealPath always returns a path, how do we know if it is correct? The simplest way is to check whether the returned path exists.
This is what the isVirtual method should do: after calling getRealPath for a given file, it uses java. io
Access this file, so you can know whether 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.