A summary of methods for obtaining various context paths in Java

Source: Internet
Author: User

First, get is the absolute path to the current running file on the server
In the servlet use:
This.getservletcontext (). Getrealpath ();

In struts by:
This.getservlet (). Getservletcontext (). Getrealpath ();

In action, use:
Servletactioncontext.getrequest (). Getrealpath ();

All three of the above are absolute paths to the current running file on the server
In fact:
Request.getrealpath ("url"); Virtual directory mapping to the actual directory
Request.getrealpath () This method has been deprecated, instead of the following method:
Request.getsession (). Getservletcontext (). Getrealpath ();

Ii. Request for a summary of various paths
request.getrealpath ("url");//virtual directory map to actual directory
Request.getrealpath ("./"); The directory where the Web page resides
Request.getrealpath (".. /"); The previous level of the directory where the Web page resides
Request.getcontextpath (); The name of the app's web directory

such as Http://localhost:7001/bookStore/
/bookstore/= [ContextPath] (Request.getcontextpath ())

get the full path of a Web project
String Strdirpath =request.getsession (). Getservletcontext (). Getrealpath ("/");

take project name News as an example: http://localhost:8080/news/main/list.jsp

(1) Get the full path of the current page containing the project name :request.getrequesturi ()
results:/news/main/list.jsp

(2) Get project name :request.getcontextpath ()
results:/news

(3) Get the current page in the same directory under the full name :request.getservletpath ()
Result: If the page is /main/list.jsp in the JSP directory

(4) Get IE Address bar address:request.getrequesturl ()
results:http://localhost:8080/news/main/list.jsp

(5) Get relative address:
Request.getrequesturi ()

results:/news/main/list.jsp

(6) Get the full path of the server on which the page is located
:application.getrealpath ("page. jsp")

results:D:\resin\webapps\news\main\list.jsp

(7) Get the absolute path of the server on which the page is located:abspath=new java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();
results:D:\resin\webapps\news

Third, get the path in the class

(1) The absolute path of the class: Class.class.getClass (). GetResource ("/"). GetPath ()
results:/d:/test/webroot/web-inf/classes/pack/

For example:

String path=this. getclass (). GetResource ("/"). GetPath (); // get d:/tomcat/webapps/project name web-inf/classes/path         Path=path.substring (1, Path.indexof ("web-inf/classes")); // To remove an engineering path from a path string

(2) The path to get the project: System.getproperty ("User.dir")

Results: D:\TEST

Iv. getting the path in the servlet
(1) Get the Project catalog: Request.getsession (). Getservletcontext (). Getrealpath ("") parameter can be specific to the package name.
Results: E:\Tomcat\webapps\news

import Java.io.File; Public classTest { Public Static voidMain (string[] args) throws Exception {System. out. println (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("")); System. out. println (Test.class. getClassLoader (). GetResource ("")); System. out. println (Classloader.getsystemresource ("")); System. out. println (Test.class. GetResource ("")); System. out. println (Test.class. GetResource ("/"));//The path of the class fileSystem. out. println (NewFile ("/"). GetAbsolutePath ()); System. out. println (System.getproperty ("User.dir")); }}

A summary of methods for obtaining various context paths in Java

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.