JSP Request Get Path

Source: Internet
Author: User

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

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.