Request for various paths

Source: Internet
Author: User
Tags tomcat

Request for various paths

Use This.getservletcontect () in the servlet. Getrealpath ()

In struts with This.getservlet (). Getservletcontext (). Getrealpath ()

In action with Servletactioncontext.getrequest (). Getrealpath ();

All three of the above are absolute paths to the current running file on the server
Request.getrealpath ("/");//Get the root directory of the current site

Request.getrealpath ("");//Get the absolute path of the current running file on the server

Currently Request.getrealpath ("") is replaced by Request.getsession (). Getservletcontext (). Getrealpath ("")


Request Url:http://localhost:8080/manage/wmf/role_grid.do

(1) The name of the Web directory to be applied Request.getcontextpath ();

Results:/manage


(2) Get the full path of the current page containing the project name: Request.getrequesturi ()
Results:/manage/wmf/role_grid.do


(3) Get servlet request address: Request.getservletpath ()
Results:/wmf/role_grid.do


(4) Get HTTP Address: Request.getrequesturl ()
Results: http://localhost:8080/manage/wmf/role_grid.do


2. Get the path in the class:

(1) The absolute path of the class: Class.class.getClass (). GetResource ("/"). GetPath ()
Results:/d:/developwork/eclipse/gy-ga-hbgay/src/2.0/ga-wmf2/target/classes/


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

Results: D:\DEVELOPWORK\ECLIPSE\GY-GA-HBGAY\SRC\2.0\GA-WMF2



The class loads the root path
String ClassPath = This.getclass (). GetResource ("/"). GetPath ();

The class loads the root path
URL Xmlpath = This.getclass (). getClassLoader (). GetResource ("");

The class is located 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 the classpath including the path of the jar package
String Allclasspath = System.getproperty ("Java.class.path");

The path of the 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 ("Class 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 to 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 the classpath: d:\apache-tomcat-8.0.361\bin\bootstrap.jar;d:\ Apache-tomcat-8.0.361\bin\tomcat-juli.jar
1 2

Project path as shown, target is the location of the deployment

Note This may not be available here, there are many ways to get the request:

1. Get through annotations (recommended): Public
class Hello {
@Autowired  
httpservletrequest request;//can get to request
}

2. Configuring 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 the parameter: public
String Hello (httpservletrequest request)

4. If there is struts:
1 2

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

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

The reason for this is that the D:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\ directory is a clone of Eclipse's D:\Tomcat 7.0 directory, This allows the D:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\ to also have the functionality 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.