Request in web application to get path, URI, URL

There are various methods for obtaining path or URI, URL in web applications, assuming the 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 that your web application name is news, you enter the request path in the browser: http://localhost:8080/news/main/list.jsp , then execute the following line of code and print the following results: 1. System.out .println(request.getContextPath()); print result: /news 2, System.out.println(request.getServletPath()); print result: /main/list.jsp 3, System.out.println(request.getRequestURI ()); print result: /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, when an unlogged user requests a resource that must be logged in, the user is prompted to log in. At this time, the URL of the current page visited by the user should be remembered. When he successfully logs in, he will jump back to the last page the user visited according to the remembered URL. :

String lastAccessUrl = request.getRequestURL() + "?" + request.getQueryString();






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324836925&siteId=291194637