下面列出了怎么用javax.servlet.http.HttpServletMapping的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = ((HttpServletRequest) request);
HttpServletMapping mapping = ((HttpServletRequest) request).getHttpServletMapping();
String resourceURI = mapping.getMatchValue();
if (mapping.getServletName().equals("jsp")) {
// Push resources
resourceCache.keySet().stream()
.filter(resourceURI::contains)
.findFirst()
.ifPresent(s -> resourceCache.get(s)
.forEach(path -> httpServletRequest.newPushBuilder().path(path).push()));
// create empty resource list if absent
resourceCache.putIfAbsent(resourceURI, Collections.newSetFromMap(new ConcurrentHashMap<>()));
} else {
// Add resource
resourceCache.keySet().stream()
.filter(httpServletRequest.getHeader("Referer")::contains)
.forEach(page -> resourceCache.get(page).add(resourceURI));
}
chain.doFilter(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
HttpServletMapping mapping = request.getHttpServletMapping();
response.getWriter()
.append("Mapping match:")
.append(mapping.getMappingMatch().name())
.append("\n")
.append("Match value:")
.append(mapping.getMatchValue())
.append("\n")
.append("Pattern:")
.append(mapping.getPattern());
}
@Override
public HttpServletMapping getHttpServletMapping() {
ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
ServletPathMatch match = src.getOriginalServletPathMatch();
if (getDispatcherType() == DispatcherType.FORWARD) {
match = src.getServletPathMatch();
}
String matchValue;
switch (match.getMappingMatch()) {
case EXACT:
matchValue = match.getMatched();
if (matchValue.startsWith("/")) {
matchValue = matchValue.substring(1);
}
break;
case DEFAULT:
case CONTEXT_ROOT:
matchValue = "";
break;
case PATH:
matchValue = match.getRemaining();
if (matchValue.startsWith("/")) {
matchValue = matchValue.substring(1);
}
break;
case EXTENSION:
matchValue = match.getMatched().substring(0, match.getMatched().length() - match.getMatchString().length() + 1);
if (matchValue.startsWith("/")) {
matchValue = matchValue.substring(1);
}
break;
default:
matchValue = match.getRemaining();
}
return new MappingImpl(matchValue, match.getMatchString(), match.getMappingMatch(), match.getServletChain().getManagedServlet().getServletInfo().getName());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpServletMapping mapping = request.getHttpServletMapping();
response.getWriter()
.append("Mapping match:")
.append(mapping.getMappingMatch().name())
.append("\n")
.append("Match value:")
.append(mapping.getMatchValue())
.append("\n")
.append("Pattern:")
.append(mapping.getPattern());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpServletMapping servletMapping = request.getHttpServletMapping();
response.getWriter()
.append("<html><body>")
.append("Value Matched: <b>").append(servletMapping.getMatchValue())
.append("</b><br/>")
.append("Pattern Used: <b>").append(servletMapping.getPattern())
.append("</b><br/>")
.append("Mapping Matched: <b>").append(servletMapping.getMappingMatch().name())
.append("</b><br/>")
.append("Servlet Name: <b>").append(servletMapping.getServletName())
.append("</b><br/>")
.append("</body></html>");
}
@Override
public HttpServletMapping getHttpServletMapping() {
ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
ServletPathMatch match = src.getOriginalServletPathMatch();
if(getDispatcherType() == DispatcherType.FORWARD) {
match = src.getServletPathMatch();
}
String matchValue;
switch (match.getMappingMatch()) {
case EXACT:
matchValue = match.getMatched();
if(matchValue.startsWith("/")) {
matchValue = matchValue.substring(1);
}
break;
case DEFAULT:
case CONTEXT_ROOT:
matchValue = "";
break;
case PATH:
matchValue = match.getRemaining();
if(matchValue.startsWith("/")) {
matchValue = matchValue.substring(1);
}
break;
case EXTENSION:
matchValue = match.getMatched().substring(0, match.getMatched().length() - match.getMatchString().length() + 1);
if(matchValue.startsWith("/")) {
matchValue = matchValue.substring(1);
}
break;
default:
matchValue = match.getRemaining();
}
return new MappingImpl(matchValue, match.getMatchString(), match.getMappingMatch(), match.getServletChain().getManagedServlet().getServletInfo().getName());
}
public static void printCurrentMappingDetails(HttpServletRequest request,
PrintWriter out) throws IOException {
HttpServletMapping forwardMapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.FORWARD_MAPPING);
HttpServletMapping includeMapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING);
HttpServletMapping asyncMapping = (HttpServletMapping) request.getAttribute(AsyncContext.ASYNC_MAPPING);
out.print("<p> " + request.getHttpServletMapping() + "</p>");
out.print("<p> FORWARD_MAPPING: " + forwardMapping + "</p>");
out.print("<p> INCLUDE_MAPPING: " + includeMapping + "</p>");
out.print("<p> ASYNC_MAPPING: " + asyncMapping + "</p>");
out.print("<hr />");
}