Request get Path,uri,url in Web application

Source: Internet
Author: User

There are various ways to get path or Uri,url in a Web application, assuming a Web page access address:

Http://localhost:8080/tradeload/TestServlet

Web Application Context:/tradeload

Each path is identified as follows:

Java code
  1. Request.getcontextpath () =/tradeload
  2. Request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () =/http localhost:8080
  3. Request.getrequesturl () = http://localhost:8080/tradeload/testservlet
  4. Request.getrequesturi () =/tradeload/testservlet
  5. Request.getpathinfo () = null
  6. Request.getservletpath () =/testservlet
  7. Getservletcontext (). Getrealpath ('/') = C:\server\glassfish\domains\domain1\applications\j2ee-modules\ Tradeload\

The Servletpath is the Url-pattern configured in Web. Xml.

-------------------------------------------------------------------------------

Assuming your Web application name is news, you enter the request path in the browser: http://localhost:8080/news/main/list.jsp executes the following line code to print out the following results: 1, System.out.println (Request.getcontextpath ()); Printing results:/news 2, System.out.println (Request.getservletpath ()); Printing results:/main/list.jsp 3, System.out.println (Request.getrequesturi ()); Printing results:/news/main/list.jsp 4, System.out.println (Request.getrealpath ("/"));  Print Result: F:\Tomcat 6.0\webapps\news\test
-------------------------------------------------------------------------------------------------------

String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";

Out.println ("BasePath:" +basepath);
Out.println ("<br/>");
Out.println ("Getcontextpath:" +request.getcontextpath ());
Out.println ("<br/>");
Out.println ("Getservletpath:" +request.getservletpath ());
Out.println ("<br/>");
Out.println ("Getrequesturi:" +request.getrequesturi ());
Out.println ("<br/>");
Out.println ("Getrequesturl:" +request.getrequesturl ());
Out.println ("<br/>");
Out.println ("Getrealpath:" +request.getrealpath ("/"));
Out.println ("<br/>");
Out.println ("Getservletcontext (). Getrealpath:" +getservletcontext (). Getrealpath ("/"));
Out.println ("<br/>");
Out.println ("GetQueryString:" +request.getquerystring ());

Show Results:

basepath: http://localhost:8080/test/

Getcontextpath:/test
Getservletpath:/test.jsp
Getrequesturi:/test/test.jsp
getrequesturl: http://localhost:8080/test/test.jsp
getrealpath:D: \tomcat 6.0\webapps\test\
Getservletcontext (). Getrealpath:D: \tomcat 6.0\webapps\test\
getquerystring:p =fuck

In some applications, users are prompted to log in when they request a resource that must be logged on, remembering the URL of the current page that the user accesses, and then jumping back to the page that the user last visited, based on the URL that is remembered after the login is successful:

String Lastaccessurl = Request.getrequesturl () + "?" + request.getquerystring ();






Request get Path,uri,url in Web application

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.