Reprint: http://blog.csdn.net/piaoxuan1987/article/details/8541839
Equest.getrealpath () This method has been deprecated, instead of the following method:
Request.getsession (). Getservletcontext (). Getrealpath ()
Various path information can be obtained from the request object, in the following example: Assuming that the requested page is index.jsp and the project is Webdemo, the various path information about the request object is obtained in index.jsp as follows String path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; String remoteaddress=request.getremoteaddr (); String Servletpath=Request.getservletpath (); String Realpath=request.getrealpath ("/"); String RemoteUser=Request.getremoteuser (); String RequestUri=Request.getrequesturi (); Out.println ("Path:" +path+ "<br>"); Out.println ("BasePath:" +basepath+ "<br>"); Out.println ("Remoteaddr:" +remoteaddress+ "<br>"); Out.println ("Servletpath:" +servletpath+ "<br>"); Out.println ("Realpath:" +realpath+ "<br>"); Out.println ("RemoteUser:" +remoteuser+ "<br>"); Out.println ("RequestUri:" +requesturi+ "<br>"); Result: path:/Webdemo basepath:http://localhost:8683/webdemo/remoteaddr:127.0.0.1Servletpath:/index.jsp Realpath:d:\apache-tomcat-6.0.13\webapps\webdemo\ RemoteUser:NULLRequestUri:/webdemo/It is not difficult for index.jsp to see the meaning that each of the corresponding methods of the request represents
Refer to the interfaces in the servlet:
Request.getscheme ();
The protocol name returned by default is HTTP
Request.getservername ()
Return the host name shown in your browser, and you'll find out if you try it yourself.
Getserverport ()
Get Server port number
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
Get a summary of the various paths from request
Request.getrealpath ("url"); Virtual directory mapping to the 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 the project name test as an example:
(1) Get the full path of the current page containing the project name: Request.getrequesturi ()
Results:/test/test.jsp
(2) Get project name: Request.getcontextpath ()
Results:/test
(3) Get the current page in the same directory under the full name: Request.getservletpath ()
Result: If the page is/test/jsp/test.jsp in the JSP directory
(4) Get the full path of the server on which the page is located: Application.getrealpath ("page. JSP")
Results: D:\resin\webapps\TEST\test.jsp
(5) Obtain 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\TEST
2. 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/
(2) The path to get the project: System.getproperty ("User.dir")
Results: D:\TEST
3. Get 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\TEST
(2) Get IE Address bar address: Request.getrequesturl ()
Results: Http://localhost:8080/TEST/test
(3) Get relative Address: Request.getrequesturi ()
Results:/test/test
Get various path summary request.getrealpath ("url") from request