Various path issues for Request requests

Keywords: Java xml JavaEE Eclipse

From: https://blog.csdn.net/qq_27770257/article/details/79438987

Recently, there has been some confusion about several "paths" in request, and the search for online resources is not well summarized. I hope this article can help me understand these "paths".(

+++++++++++++++++++++++++++++++++++++++++++++++++ 
This article mainly discusses the following methods for request s to get paths:
request.getServletPath()
request.getPathInfo()
request.getContextPath()
request.getRequestURI()
request.getRequestURL()
request.getServletContext().getRealPath()

To illustrate with a simple example:
web.xml configuration (note the url-pattern item here)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>aab</display-name>
  <welcome-file-list>
    <welcome-file>a.jsp</welcome-file>
  </welcome-file-list>
    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>com.java.test.TestServlet</servlet-class>

    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/*</url-pattern><!-- Notice here -->
    </servle

TestServlet.java file:

package com.java.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servletPath:"+req.getServletPath());
        System.out.println("contextPath:"+req.getContextPath());
        System.out.println("contextPath2:"+req.getServletContext().getContextPath());
        System.out.println("pageInfo:"+req.getPathInfo());
        System.out.println("uri:"+req.getRequestURI());
        System.out.println("url:"+req.getRequestURL());
        System.out.println("realPath:"+req.getServletContext().getRealPath("/"));

    }

}

Request at this time http://localhost:8080/testweb (url-pattern=/*) 
The printed values are:

servletPath:
contextPath:/testweb
contextPath2:/testweb
pageInfo:null
uri:/testweb
url:http://localhost:8080/testweb
realPath:G:\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

request http://localhost:8080/testweb/abc Printed values are:

servletPath:
contextPath:/testweb
contextPath2:/testweb
pageInfo:/abc
uri:/testweb/abc
url:http://localhost:8080/testweb/abc
realPath:G:\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

When we modify the web.xml to be as follows (note the url-pattern change):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>aab</display-name>
  <welcome-file-list>
    <welcome-file>a.jsp</welcome-file>
  </welcome-file-list>


    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>com.java.test.TestServlet</servlet-class>

    </servlet>

    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/abc/def/*</url-pattern><!-- Notice here -->
    </servlet-mapping>

</web-app>

request http://localhost:8080/testweb/abc/def/ghi/test.html (url-pattern=/abc/def/*) 
Printed values are:

servletPath:/abc/def
contextPath:/testweb
contextPath2:/testweb
pageInfo:/ghi/test.html
uri:/testweb/abc/def/ghi/test.html
url:http://localhost:8080/testweb/abc/def/ghi/test.html
realPath:G:\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

By observing the print results, we can conclude that:
1. getServletPath(): Get a path that matches the "url-pattern", note that the exact matching part is not included in the *part.(
2. getPageInfo(): Complementary to the path obtained by getServletPath(), the path portion of *d in the "url-pattern"
3. getContextPath(): Get the root path of the project
4. getRequestURI: Get the root path to the end of the address
5. getRequestURL: Get the requested address link (address entered in the browser)
6. getServletContext().getRealPath('/'): Get the actual address of'/'in the machine
7. getScheme(): Get the protocol used (http or https)
8. getProtocol(): Get the name of the protocol (HTTP/1.11)
9. getServerName(): Get the domain name (xxx.com)
10. getLocalName: Get IP


Posted by benutne on Wed, 25 Mar 2020 09:32:39 -0700