Java Web classpath, Project path acquisition issues __java

Source: Internet
Author: User
Class load Root path
String ClassPath = This.getclass (). GetResource ("/"). GetPath ();

Class load Root path
URL Xmlpath = This.getclass (). getClassLoader (). GetResource ("");

Class is in the project root path
String Proclasspath = This.getclass (). GetResource (""). GetPath ();

Project Server script files path file
directory = new File ("");//parameter is empty
String Prorootpath = Directory.getcanonicalpath ();

Project Server script file path
String Propath = System.getproperty ("User.dir");

Gets all classpath including jar package path
String Allclasspath = System.getproperty ("Java.class.path");

Path to project deployment
String Path = Request.getsession (). Getservletcontext (). Getrealpath ("/");

SYSTEM.OUT.PRINTLN ("Class load Root path:" + ClassPath);
SYSTEM.OUT.PRINTLN ("Class load Root path:" + Xmlpath);
System.out.println ("The class is located in the project path:" + Proclasspath);
SYSTEM.OUT.PRINTLN ("Project Server script file path:" + Prorootpath);
SYSTEM.OUT.PRINTLN ("Project Server script file path:" + Propath);
SYSTEM.OUT.PRINTLN ("Path of the Project deployment:" + Allclasspath);
System.out.println ("Get all classpath:" + path);

The results are as follows:

Class load Root path:/d:/workspace/training2016/04-pg/0401-source/base2016/base_web/target/base_web-1.0-snapshot/web-inf/ Classes/
class Load root path: File:/d:/workspace/training2016/04-pg/0401-source/base2016/base_web/target/base_
Project Path of web-1.0-snapshot/web-inf/classes/class:/d:/workspace/training2016/04-pg/0401-source/base2016/base_web/ TARGET/BASE_WEB-1.0-SNAPSHOT/WEB-INF/CLASSES/COM/STUDY/MVC/
Project Server script file path: D:\apache-tomcat-8.0.361\bin
Project Server script file path: D:\apache-tomcat-8.0.361\bin
Project deployment path: D:\workspace\Training2016\04-PG\0401-Source\base2016\base _web\target\base_web-1.0-snapshot\
Get all classpath: d:\apache-tomcat-8.0.361\bin\bootstrap.jar;d:\ Apache-tomcat-8.0.361\bin\tomcat-juli.jar

Project path as shown in the figure, Target is the location of the deployment

Note that you may not get the request here, there are many ways to get the request:

1. Obtain by annotation (recommended): Public
class Hello {
@Autowired  
httpservletrequest request;//This is available to request
}

2. Configure a listener in Web.xml:
<listener>  
        <listener-class>  Org.springframework.web.context.request.RequestContextListener  
        </listener-class>  
</listener >  
Java code:
httpservletrequest request = ((servletrequestattributes) Requestcontextholder.getrequestattributes ()). Getrequest ();  

3. Pass directly in Parameters: public
String Hello (httpservletrequest request)

4. If you have struts:

Sometimes request.getsession (). Getservletcontext (). Getrealpath ("/"); The path you are getting is not a tomcat path

D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ .....

The reason for this is actually D:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\ this directory is a clone of Eclipse's D:\Tomcat 7.0 directory, So that D:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\ can also have the function of the source server.

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.