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
- Request.getcontextpath () =/tradeload
- Request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () =/http localhost:8080
- Request.getrequesturl () = http://localhost:8080/tradeload/testservlet
- Request.getrequesturi () =/tradeload/testservlet
- Request.getpathinfo () = null
- Request.getservletpath () =/testservlet
- 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