将控制器 servlet 映射到一个更具体的类似上,将静态内容放在一个特定的文件夹中,并创建一个侦听,该侦听透明地继续任何静态内容的链,并将其他内容的请求分派到控制器 servlet。url-pattern
/pages/*
/static
Filter
/*
简而言之:
<filter>
<filter-name>filter</filter-name>
<filter-class>com.example.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>com.example.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/pages/*</url-pattern>
</servlet-mapping>
在过滤器中包含以下内容:doFilter()
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getRequestURI().substring(req.getContextPath().length());
if (path.startsWith("/static")) {
chain.doFilter(request, response); // Goes to default servlet.
} else {
request.getRequestDispatcher("/pages" + path).forward(request, response);
}
不,这不会在浏览器地址栏中结束。它是完全透明的。如有必要,您可以制作和/或过滤器。/pages
"/static"
"/pages"
init-param