Search code examples
javaspringservletsspring-web

Why HttpServletRequest truncates url input on a # character?


The following servlet reads the url path parameter. Problem: if the input contains special chars, eg I discovered #, then the string is truncated!

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

`localhost:8080/hash/asdfgh#jkl`

Question: how can I natively pass through the input parameter?


Solution

  • The part starting from the # is not send to/received by to your servlet.
    It is an information used only from the client side (browser) and doesn't make part of a URI.

    The RFC2396 states indeed :

    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.