有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么HttpServletRequest会截断#字符上的url输入?

下面的servlet读取url路径参数。 问题:如果输入包含特殊字符,例如我发现#,则字符串被截断

@RestController
public class MyServlet {    
    @GetMapping("/hash")
    @ApiIgnore
    public String hash(HttpServletRequest req) {
        String pw = req.getPathInfo(); //asdfgh
    }
}

`localhost:8080/hash/asdfgh#jkl`

问题:我如何以本机方式传递输入参数


共 (1) 个答案

  1. # 1 楼答案

    #开始的部分不会发送到servlet或被servlet接收
    这是一种仅从客户端(浏览器)使用的信息,不会产生任何影响 URI的一部分

    RFC2396确实指出:

    When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch ("#") character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.