This tutorial is good: http://zjutsoft.iteye.com/blog/1084260
Test yourself as follows:
System.out.println ("-----------------servlet-----------------"); SYSTEM.OUT.PRINTLN ("Realpath null:" +request.getrealpath ("")); System.out.println ("Realpath index.jsp:" +request.getrealpath ("index.jsp")); System.out.println ("Realpath./:" +request.getrealpath ("./")); System.out.println ("Realpath. /: "+request.getrealpath (". /")); System.out.println ("ContextPath:" +request.getcontextpath ()); SYSTEM.OUT.PRINTLN ("Full path to Web project:" +request.getsession (). Getservletcontext (). Getrealpath ("/")); System.out.println ("URI:" +request.getrequesturi ()); System.out.println ("URL:" +request.getrequesturl ()); System.out.println ("Servletpath:" +request.getservletpath ())//system.out.println ("The Absolute path of the page on the server:" +new Java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ()); System.out.println ("~~~~~~~~~~~~~~~ page ~~~~~~~~~~~~~~~~~"); SYSTEM.OUT.PRINTLN ("Realpath null:" +request.getrealpath ("")); System.out.println ("Realpath index.jsp:" +request.getrealpath ("index.jsp")); System.out.println ("Realpath./:" +request.getrealpath ("./")); System.out.println ("Realpath. /: "+request.getrealpath (". /")); System.out.println ("ContextPath:" +request.getcontextpath ()); SYSTEM.OUT.PRINTLN ("Full path to Web project:" +request.getsession (). Getservletcontext (). Getrealpath ("/")); System.out.println ("URI:" +request.getrequesturi ()); System.out.println ("URL:" +request.getrequesturl ()); System.out.println ("Servletpath:" +request.getservletpath ()); System.out.println ("page absolute path on the server:" +new java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ( );//Results-----------------servlet-----------------realpath null:d:\eclipse-jee-luna-sr1-win32\eclipse\workspace\. Metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxtrealpath index.jsp:d:\ Eclipse-jee-luna-sr1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ Jyxt\index.jsprealpath./:D: \eclipse-jee-luna-sr1-win32\eclipse\workspace\.metadata\.plugins\ Org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\.realpath. /:D: \eclipse-jee-luna-sr1-win32\eclipse\worksPace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\. Full path to the Contextpath:/jyxtweb project: D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\ Org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxturi:/jyxt/hellourl:http://localhost:8080/jyxt/helloservletpath :/hello~~~~~~~~~~~~~~~ page ~~~~~~~~~~~~~~~~~realpath null:d:\eclipse-jee-luna-sr1-win32\eclipse\workspace\. Metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxtrealpath index.jsp:d:\ Eclipse-jee-luna-sr1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ Jyxt\index.jsprealpath./:D: \eclipse-jee-luna-sr1-win32\eclipse\workspace\.metadata\.plugins\ Org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\.realpath. /:D: \eclipse-jee-luna-sr1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\ Wtpwebapps\jyxt\. Full path to the Contextpath:/jyxtweb project: D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\ org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxturi:/jyxt/web-inf/view/index.jspurl:http://localhost:8080/jyxt/web-inf/view/index.jspservletpath:/web-inf/view/ Index.jsp page Absolute path on the server: D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\ Org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\jyxt\web-inf\view
There are only 10 ways to get the path in the class before you see it.
two scenarios:. JSP and Servlet. The Realpath in the JSP is discarded, but the return value is the same as in the servlet, Getrealpath ("./... /") return value is not right; the absolute path of the page on the server is not available.
Getcontextpath () and. GetSession (). Getservletcontext (). Getrealpath ("/") are the same as the return values in the. JSP and servlet; Uri,url, Servletpath are not the same.
HttpSystem.out.println ("scheme:" +request.getscheme ());//localhostsystem.out.println ("Name:" + Request.getservername ());//8080system.out.println ("Port:" +request.getserverport ());
Common methods
In the servlet: Getcontextpath (), URI, URL, Servletpath (), in JSP: Getcontextpath ()
JSP Request Get Path