Java EE absolute Path, relative path acquisition method __java

Source: Internet
Author: User
  The paths used in Java are divided into two types: absolute and relative. In the final analysis, Java essentially only uses absolute paths to find resources. All the relative paths to finding resources are just a few convenient ways.

  But the API helps us build an absolute path at the bottom to find resources.

  When developing Web applications, it is often necessary to obtain the physical path of the current Webroot in the server. If it's a servlet, Action, Controller, or filter, Listener, interceptor, etc, we just need to get the ServletContext and then pass Servletcontext.getrealpath ("

  /"To obtain the physical address currently applied to the server. If you don't get ServletContext in a class, there are two ways to do it: 1 use Java's class loading mechanism: Invoke XXX.class.getClassLoader (). GetResource ("");
  method to obtain the classpath and then process the Webroot directory. This method can only be valid if the class is under Webroot/web-inf/classes, and if the class is packaged into a jar file, it fails.

  This should be done in one of the following ways. 2 The idea of the spring framework, in Web-inf/web.xml, creates a webapprootkey param, specifies a value (the default is Webapp.root) as the key value, and then through listener, or filter, Or the servlet executes string webapprootkey = Getservletcontext (). Getrealpath ("/"); The corresponding webapp.root of Webapprootkey are written as Key,value to the system properties.

  The physical path of the Webroot is then obtained by System.getproperty ("Webapp.root") in the program. According to the second idea, we can expand it.

  But for applications that are deployed on a single server, look down if it's not what you need. Here are some ways to get classpath and the absolute path of the current class. You can use some of these methods to get the absolute path to the resources you need: 1.debitnoteaction.class.getresource ("") Gets theThe URI directory of the current class Filetest.class file.
  does not include himself. such as: FILE:/D:/ECLIPSE/SPRINGTEST/WEBROOT/WEB-INF/CLASSES/ATACARNET/SRC/COM/EVI/MODULES/ATACARNET/ACTION/2.
  DebitNoteAction.class.getResource ("/") gets the absolute URI path of the current classpath. such as: File:/d:/eclipse/springtest/webroot/web-inf/classes/3.thread.currentthread (). Getcontextclassloader ().

  GetResource ("") is also the absolute URI path of the current classpath such as: File:/d:/eclipse/springtest/webroot/web-inf/classes/recommended to use this method to obtain.
  4.debitnoteaction.class.getclassloader (). GetResource ("") or Classloader.getsystemresource ("")
  The absolute URI path of the current classpath is also obtained. such as: FILE:/D:/ECLIPSE/SPRINGTEST/WEBROOT/WEB-INF/CLASSES/5. Get Server relative path System.getproperty ("User.dir") For example: E:\ Apache-tomcat-5.5.16\apache-tomcat-5.5.16\bin 6. Get the absolute path in the project generally with Request.getrealpath ("/") or Request.getrealpath ("/

  config/") But now does not advocate using Request.getrealpath ("/"), you can try the Servletcontext.getrealpath ("/") method to get the absolute path of the Web application's root directory.

  It is easy to get src files, because SRC is the default relative directory, such as you said to obtain the SRC under the COM directory of the Test.java file, you only need this is enough to file F = new file (Com/test.java); But asI want to get the files that are not in the SRC directory or the Webroot directory, but to take it from the SRC or webroot directory, such as Doc.
  My hard method is implemented in this way: String path = This.getservletcontext (). Getrealpath ("/");
  Properties P = new properties ();
  P.load (New FileInputStream (path.substring (0, (Path.lastindexof ("\\WebRoot") + 1)) + "doc/db.properties"));
System.out.println (P.getproperty ("drivername")); -------------------------------another: Request Getcontextpath, Getservletpath, Getrequesturi, Getrequesturl,

  Getrealpath The difference assumes that your Web application name is news, and you enter the request path in the browser: http://localhost:8080/news/main/list.jsp
  The following line code is executed to print out the following results: 1, System.out.println (Request.getcontextpath ());
  Printed results:/news 2, System.out.println (Request.getservletpath ());
  Printed results:/main/list.jsp 3, System.out.println (Request.getrequesturi ());
  Printed results:/news/main/list.jsp 4, System.out.println (Request.getrequesturl ());
  Printed results: http://localhost:8080/news/main/list.jsp 5, System.out.println (Request.getrealpath ("/")); Print Result: F:\Tomcat 6.0\webapps\news\test

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.