本文整理汇总了Java中javax.servlet.ServletRegistration.getMappings方法的典型用法代码示例。如果您正苦于以下问题:Java ServletRegistration.getMappings方法的具体用法?Java ServletRegistration.getMappings怎么用?Java ServletRegistration.getMappings使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletRegistration
的用法示例。
在下文中一共展示了ServletRegistration.getMappings方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: collectUrlPatterns
import javax.servlet.ServletRegistration; //导入方法依赖的package包/类
static String[] collectUrlPatterns(ServletContext servletContext, Class<?> servletCls) {
List<ServletRegistration> servlets = servletContext.getServletRegistrations()
.values()
.stream()
.filter(predicate -> {
return predicate.getClassName().equals(servletCls.getName());
})
.collect(Collectors.toList());
if (servlets.isEmpty()) {
return new String[] {};
}
ServletRegistration servletRegistration = servlets.get(0);
Collection<String> mappings = servletRegistration.getMappings();
if (servlets.size() > 1) {
LOGGER.info("Found {} {} registered, select the first one, mappings={}.",
servlets.size(),
servletCls.getName(),
mappings);
}
return filterUrlPatterns(mappings);
}
示例2: testSaveUrlPrefixNormal
import javax.servlet.ServletRegistration; //导入方法依赖的package包/类
@Test
public void testSaveUrlPrefixNormal(@Mocked ServletContext servletContext,
@Mocked ServletRegistration servletRegistration) {
System.clearProperty(Const.URL_PREFIX);
new Expectations() {
{
servletContext.getContextPath();
result = "/root";
servletRegistration.getClassName();
result = RestServlet.class.getName();
servletRegistration.getMappings();
result = Arrays.asList("/rest/*");
servletContext.getServletRegistrations();
result = Collections.singletonMap("test", servletRegistration);
}
};
ServletUtils.saveUrlPrefix(servletContext);
Assert.assertThat(System.getProperty(Const.URL_PREFIX), Matchers.is("/root/rest"));
System.clearProperty(Const.URL_PREFIX);
}
示例3: getServletUrlPattern
import javax.servlet.ServletRegistration; //导入方法依赖的package包/类
private static String getServletUrlPattern(final ServletContextEvent servletContextEvent) throws Exception {
final ServletContext servletContext = servletContextEvent.getServletContext();
ServletRegistration servletRegistration = servletContext.getServletRegistration(servletName);
if (servletRegistration == null) {
throw new NoSuchElementException("no servlet with name \"" + servletName + "\" is found.");
}
java.util.Collection<java.lang.String> mappings = servletRegistration.getMappings();
if (mappings.size() != 1) {
throw new NoSuchElementException("unable to identify servlet mappings for servlet with name \"" + servletName + "\".");
}
String mapping = (String) mappings.toArray()[0];
//url patterns in most cases end with '\*'. But a url-pattern with just '\' may be found for exact matches.
if (mapping.endsWith("*"))
mapping = mapping.substring(0, mapping.length()-1);
return mapping;
}
示例4: needJDBC
import javax.servlet.ServletRegistration; //导入方法依赖的package包/类
private boolean needJDBC(HttpServletRequest request) {
System.out.println("JDBC Filter");
//
// Servlet Url-pattern: /spath/*
//
// => /spath
String servletPath = request.getServletPath();
// => /abc/mnp
String pathInfo = request.getPathInfo();
String urlPattern = servletPath;
if (pathInfo != null) {
// => /spath/*
urlPattern = servletPath + "/*";
}
// Key: servletName.
// Value: ServletRegistration
Map<String, ? extends ServletRegistration> servletRegistrations =
request.getServletContext().getServletRegistrations();
// Collection of all servlet in your webapp.
Collection<? extends ServletRegistration> values = servletRegistrations.values();
for (ServletRegistration sr : values) {
Collection<String> mappings = sr.getMappings();
if (mappings.contains(urlPattern)) {
return true;
}
}
return false;
}