Context Path + servlet path + path info = request uri

                        |-- Context Path --|-- Servlet Path -|--Path Info--|
http://www.myserver.com     /mywebapp        /helloServlet      /hello
                        |-------- Request URI  ----------------------------|


Remember the following three points:
1. Request URI = context path + servlet path + path info.
2. Context paths and servlet paths start with a / but do not end with it.
3. HttpServletRequest provides three methods getContextPath(),
    getServletPath() and getPathInfo() to retrieve the context path,
    the servlet path, and the path info, respectively, associated with a request.


Identifying the servlet path
To match a request URI with a servlet, the servlet container follows a simple algorithm.
Once it identifies the context path, if any, it evaluates the remaining part of the
request URI with the servlet mappings specified in the deployment descriptor, in the
following order. If it finds a match at any step, it does not take the next step.

1 The container tries to match the request URI to a servlet mapping. If it finds a
match, the complete request URI (except the context path) is the servlet path. In
this case, the path info is null.
2 It tries to recursively match the longest path by stepping down the request URI
path tree a directory at a time, using the / character as a path separator, and determining
if there is a match with a servlet. If there is a match, the matching part
of the request URI is the servlet path and the remaining part is the path info.
3 If the last node of the request URI contains an extension (.jsp, for example),
the servlet container tries to match it to a servlet that handles requests for the
specified extension. In this case, the complete request URI is the servlet path
and the path info is null.
4 If the container is still unable to find a match, it will forward the request to the
default servlet. If there is no default servlet, it will send an error message indicating
the servlet was not found.



<servlet-mapping>
    <servlet-name>RedServlet</servlet-name>
    <url-pattern>/red/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>RedServlet</servlet-name>
    <url-pattern>/red/red/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>RedBlueServlet</servlet-name>
    <url-pattern>/red/blue/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>BlueServlet</servlet-name>
    <url-pattern>/blue/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>GreenServlet</servlet-name>
    <url-pattern>/green</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ColorServlet</servlet-name>
    <url-pattern>*.col</url-pattern>
</servlet-mapping>


Request URI                Servlet Used            Servlet Path        Path Info
/colorapp/red                RedServlet              /red                 null
/colorapp/red/               RedServlet              /red                 /
/colorapp/red/aaa            RedServlet              /red                 /aaa

/colorapp/red/blue/aa        RedBlueServlet          /red/blue            /aa
/colorapp/red/red/aaa        RedServlet              /red/red             /aaa
/colorapp/aa.col             ColorServlet            /aa.col              null

/colorapp/hello/aa.col       ColorServlet            /hello/aa.col        null
/colorapp/red/aa.col         RedServlet              /red                 /aa.col
/colorapp/blue               NONE(Error message)                         
/colorapp/hello/blue/        NONE(Error message)                         
/colorapp/blue/mydir         NONE(Error message)
    
/colorapp/blue/dir/aa.col    ColorServlet            /blue/dir/aa.col     null 
/colorapp/green              GreenServlet            /green               null

解释一下:
这里的三个错误,都是错在blue上,注意blue的mapping url是/blue/而不是/blue或者/blue/*这是造成错误的主要原因

在Spring Boot中,可以通过配置`server.servlet.context-path`来设置应用程序的上下文路径(即context-path)。如果需要统一添加前缀,则可以使用Spring MVC中的`HandlerInterceptor`来拦截请求,并在请求路径前添加前缀。 具体步骤如下: 1. 创建一个`HandlerInterceptor`实现类,实现`preHandle`方法,在该方法中获取请求路径并添加前缀,然后将修改后的路径设置回请求中。 ``` @Component public class PrefixInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String prefix = "/api"; // 前缀 String requestURI = request.getRequestURI(); // 请求路径 String newURI = prefix + requestURI; // 添加前缀 request.getRequestDispatcher(newURI).forward(request, response); // 设置修改后的路径 return true; } } ``` 2. 在Spring Boot配置类中添加`InterceptorRegistry`,并将上面创建的`HandlerInterceptor`添加到其中。 ``` @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Autowired private PrefixInterceptor prefixInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(prefixInterceptor).addPathPatterns("/**"); // 添加拦截器并设置拦截路径 } } ``` 3. 在`Controller`中使用`@RequestMapping`注解指定接口路径,如`/user`,拦截器会自动添加前缀,最终的请求路径为`/api/user`。 ``` @RestController @RequestMapping("/user") public class UserController { @GetMapping("/list") public List<User> list() { // ... } // ... } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值