I have the following code snippet.
@Bean
public ServletRegistrationBean<S1Servlet> s1Servlet() {
ServletRegistrationBean<S1Servlet> registrationBean = new ServletRegistrationBean<>(new S1Servlet());
registrationBean.addUrlMappings("/sub/*");
registrationBean.setLoadOnStartup(1);
return registrationBean;
}
I access the following url:http://localhost:8080/home/sub/st1 (contextpath: home)
When I don't register the ServletRegistrationBean, the result returned by request.getServletPath() is /sub/st1.
When I register the ServletRegistrationBean, the result returned by request.getServletPath() is /sub.
Why does request.getServletPath() change? I have searched through all the Spring Boot libraries to find how getServletPath() works, but I couldn't find it. I really want to understand how it works and the architecture behind getting the value of request.getServletPath(). Please help me.
Comment From: wilkinsona
This is defined by the Servlet specification. From section 3.5, the servlet path is:
the path section that directly corresponds to the mapping which activated this request. This path starts with a ’/’ character except in the case where the request is matched with the ‘/*’ or ““ pattern, in which case it is an empty string.
With the servlet registration bean, the mapping is /sub/*. The portion of the URL that matches this mapping is /sub/st1 t this is the servlet path. You haven't provided enough information for us to know what the mapping is without the ServletRegistrationBean.
If you have any further questions, please follow up on Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.