下面列出了javax.servlet.ServletContext#getEffectiveMajorVersion ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
servletContext = sc;
// Try and use one of the Servlet 3.0 methods that should be blocked
try {
sc.getEffectiveMajorVersion();
log.append("FAIL-01");
} catch (UnsupportedOperationException uoe) {
log.append("PASS-01");
} catch (Exception e) {
log.append("FAIL-02");
}
}
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
// Try and use one of the Servlet 3.0 methods that should be blocked
try {
servletContext = sce.getServletContext();
sc.getEffectiveMajorVersion();
log.append("FAIL-01");
} catch (UnsupportedOperationException uoe) {
log.append("PASS-01");
} catch (Exception e) {
log.append("FAIL-02");
}
}
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
// Try and use one of the Servlet 3.0 methods that should be blocked
try {
servletContext = sce.getServletContext();
sc.getEffectiveMajorVersion();
log.append("FAIL-01");
} catch (UnsupportedOperationException uoe) {
log.append("PASS-01");
} catch (Exception e) {
log.append("FAIL-02");
}
}
public static WebApiBinder newBinder(ApiBinder apiBinder) throws IOException {
Environment environment = apiBinder.getEnvironment();
Object context = environment.getContext();
if (!(context instanceof ServletContext)) {
return null;
}
ServletContext servletContext = (ServletContext) context;
//
// .MimeType
MimeTypeSupplier mimeTypeContext = new MimeTypeSupplier(servletContext);
mimeTypeContext.loadResource("/META-INF/mime.types.xml");
mimeTypeContext.loadResource("mime.types.xml");
apiBinder.bindType(MimeType.class, mimeTypeContext);
//
//.ServletVersion
ServletVersion curVersion = ServletVersion.V2_3;
try {
environment.getClassLoader().loadClass("javax.servlet.ServletRequestListener");
curVersion = ServletVersion.V2_4;
servletContext.getContextPath();
curVersion = ServletVersion.V2_5;
servletContext.getEffectiveMajorVersion();
curVersion = ServletVersion.V3_0;
servletContext.getVirtualServerName();
curVersion = ServletVersion.V3_1;
} catch (Throwable e) { /* 忽略 */ }
//
// .Binder
apiBinder.bindType(ServletContext.class).toInstance(servletContext);
apiBinder.bindType(ServletVersion.class).toInstance(curVersion);
//
return new InvokerWebApiBinder(curVersion, mimeTypeContext, apiBinder);
}
@Override
public void onStartup(final Set<Class<?>> classes, final ServletContext servletContext) throws ServletException {
if (servletContext.getMajorVersion() > 2 && servletContext.getEffectiveMajorVersion() > 2 &&
!"true".equalsIgnoreCase(servletContext.getInitParameter(
Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED
))) {
final Logger LOGGER = StatusLogger.getLogger();
LOGGER.debug("Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.");
final FilterRegistration.Dynamic filter =
servletContext.addFilter("log4jServletFilter", Log4jServletFilter.class);
if (filter == null) {
LOGGER.warn("WARNING: In a Servlet 3.0+ application, you should not define a " +
"log4jServletFilter in web.xml. Log4j 2 normally does this for you automatically. Log4j 2 " +
"web auto-initialization has been canceled.");
return;
}
final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
initializer.start();
initializer.setLoggerContext(); // the application is just now starting to start up
servletContext.addListener(new Log4jServletContextListener());
filter.setAsyncSupported(true); // supporting async when the user isn't using async has no downsides
filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
}
}