本文整理匯總了Java中javax.servlet.ServletContext.addFilter方法的典型用法代碼示例。如果您正苦於以下問題:Java ServletContext.addFilter方法的具體用法?Java ServletContext.addFilter怎麽用?Java ServletContext.addFilter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.servlet.ServletContext
的用法示例。
在下文中一共展示了ServletContext.addFilter方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onStartup
import javax.servlet.ServletContext; //導入方法依賴的package包/類
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//register config classes
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(WebMvcConfig.class);
rootContext.register(JPAConfig.class);
rootContext.register(WebSecurityConfig.class);
rootContext.register(ServiceConfig.class);
//set session timeout
servletContext.addListener(new SessionListener(maxInactiveInterval));
//set dispatcher servlet and mapping
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
new DispatcherServlet(rootContext));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);
//register filters
FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("endcodingFilter", new CharacterEncodingFilter());
filterRegistration.setInitParameter("encoding", "UTF-8");
filterRegistration.setInitParameter("forceEncoding", "true");
//make sure encodingFilter is matched first
filterRegistration.addMappingForUrlPatterns(null, false, "/*");
//disable appending jsessionid to the URL
filterRegistration = servletContext.addFilter("disableUrlSessionFilter", new DisableUrlSessionFilter());
filterRegistration.addMappingForUrlPatterns(null, true, "/*");
}
示例2: initMetrics
import javax.servlet.ServletContext; //導入方法依賴的package包/類
/**
* Initializes Metrics.
*/
private void initMetrics(ServletContext servletContext, EnumSet<DispatcherType> disps) {
log.debug("Initializing Metrics registries");
servletContext.setAttribute(InstrumentedFilter.REGISTRY_ATTRIBUTE,
metricRegistry);
servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY,
metricRegistry);
log.debug("Registering Metrics Filter");
FilterRegistration.Dynamic metricsFilter = servletContext.addFilter("webappMetricsFilter",
new InstrumentedFilter());
metricsFilter.addMappingForUrlPatterns(disps, true, "/*");
metricsFilter.setAsyncSupported(true);
log.debug("Registering Metrics Servlet");
ServletRegistration.Dynamic metricsAdminServlet =
servletContext.addServlet("metricsServlet", new MetricsServlet());
metricsAdminServlet.addMapping("/management/metrics/*");
metricsAdminServlet.setAsyncSupported(true);
metricsAdminServlet.setLoadOnStartup(2);
}
示例3: contextInitialized
import javax.servlet.ServletContext; //導入方法依賴的package包/類
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext ctx = sce.getServletContext();
ServletRegistration.Dynamic sd = ctx.addServlet("DynamicServlet",
"com.creditease.monitorframework.fat.DynamicServlet");
sd.addMapping("/DynamicServlet");
sd.setInitParameter("test", "test");
sd.setLoadOnStartup(1);
sd.setAsyncSupported(false);
FilterRegistration.Dynamic fd = ctx.addFilter("DynamicFilter",
"com.creditease.monitorframework.fat.filters.DynamicFilter");
fd.addMappingForUrlPatterns(null, true, "/DynamicServlet");
fd.setInitParameter("test2", "test2");
fd.setAsyncSupported(false);
ctx.addListener("com.creditease.monitorframework.fat.listeners.TestServletInitListener");
}
示例4: addDispatcherContext
import javax.servlet.ServletContext; //導入方法依賴的package包/類
private void addDispatcherContext(ServletContext container) {
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(SpringDispatcherConfig.class);
// Declare <servlet> and <servlet-mapping> for the DispatcherServlet
ServletRegistration.Dynamic dispatcher = container.addServlet("ch03-servlet",
new DispatcherServlet(dispatcherContext));
dispatcher.addMapping("*.html");
dispatcher.setLoadOnStartup(1);
FilterRegistration.Dynamic corsFilter = container.addFilter("corsFilter", new CorsFilter());
corsFilter.setInitParameter("cors.allowed.methods", "GET, POST, HEAD, OPTIONS, PUT, DELETE");
corsFilter.addMappingForUrlPatterns(null, true, "/*");
FilterRegistration.Dynamic filter = container.addFilter("hiddenmethodfilter", new HiddenHttpMethodFilter());
filter.addMappingForServletNames(null, true, "/*");
FilterRegistration.Dynamic multipartFilter = container.addFilter("multipartFilter", new MultipartFilter());
multipartFilter.addMappingForUrlPatterns(null, true, "/*");
}
示例5: onStartup
import javax.servlet.ServletContext; //導入方法依賴的package包/類
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
servletContext.addFilter("characterEncodingFilter", new CharacterEncodingFilter("UTF-8"));
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("Dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
CXFServlet cxf = new CXFServlet();
BusFactory.setDefaultBus(cxf.getBus());
ServletRegistration.Dynamic cxfServlet = servletContext.addServlet("CXFServlet", cxf);
cxfServlet.setLoadOnStartup(1);
cxfServlet.addMapping("/services/*");
servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain")).addMappingForUrlPatterns(null, false,
"/*");
servletContext.getSessionCookieConfig().setSecure(cookieSecure);
}
示例6: onStartup
import javax.servlet.ServletContext; //導入方法依賴的package包/類
/**
* Configure the given {@link ServletContext} with any servlets, filters, listeners
* context-params and attributes necessary for initializing this web application. See examples
* {@linkplain WebApplicationInitializer above}.
*
* @param servletContext the {@code ServletContext} to initialize
* @throws ServletException if any call against the given {@code ServletContext} throws a {@code ServletException}
*/
public void onStartup(ServletContext servletContext) throws ServletException {
// Spring Context Bootstrapping
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
rootAppContext.register(AutoPivotConfig.class);
servletContext.addListener(new ContextLoaderListener(rootAppContext));
// Set the session cookie name. Must be done when there are several servers (AP,
// Content server, ActiveMonitor) with the same URL but running on different ports.
// Cookies ignore the port (See RFC 6265).
CookieUtil.configure(servletContext.getSessionCookieConfig(), CookieUtil.COOKIE_NAME);
// The main servlet/the central dispatcher
final DispatcherServlet servlet = new DispatcherServlet(rootAppContext);
servlet.setDispatchOptionsRequest(true);
Dynamic dispatcher = servletContext.addServlet("springDispatcherServlet", servlet);
dispatcher.addMapping("/*");
dispatcher.setLoadOnStartup(1);
// Spring Security Filter
final FilterRegistration.Dynamic springSecurity = servletContext.addFilter(SPRING_SECURITY_FILTER_CHAIN, new DelegatingFilterProxy());
springSecurity.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
}
示例7: onStartup
import javax.servlet.ServletContext; //導入方法依賴的package包/類
@Override
public void onStartup(ServletContext sc) throws ServletException {
logger.info("wicket servlet initializer startup, configuration: {}", wicketConfiguration);
FilterRegistration filter = sc.addFilter("wicket-filter", JavaxWebSocketFilter.class);
filter.setInitParameter(JavaxWebSocketFilter.APP_FACT_PARAM, SpringWebApplicationFactory.class.getName());
filter.setInitParameter("applicationBean", "wicketApplication");
filter.setInitParameter(JavaxWebSocketFilter.FILTER_MAPPING_PARAM, "/*");
filter.setInitParameter("configuration", wicketConfiguration);
filter.addMappingForUrlPatterns(null, false, "/*");
}
示例8: onStartup
import javax.servlet.ServletContext; //導入方法依賴的package包/類
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//If you want to use the XML configuration, comment the following two lines out.
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(ApplicationContext.class);
// rootContext.scan("com.pigdroid.social.config");
//If you want to use the XML configuration, uncomment the following lines.
//XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
//rootContext.setConfigLocation("classpath:exampleApplicationContext.xml");
ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING);
EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("characterEncoding", characterEncodingFilter);
characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
FilterRegistration.Dynamic security = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
security.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
FilterRegistration.Dynamic sitemesh = servletContext.addFilter("sitemesh", new ConfigurableSiteMeshFilter());
sitemesh.addMappingForUrlPatterns(dispatcherTypes, true, "*.jsp");
servletContext.addListener(new ContextLoaderListener(rootContext));
}
示例9: beforeSpringSecurityFilterChain
import javax.servlet.ServletContext; //導入方法依賴的package包/類
@Override
public void beforeSpringSecurityFilterChain(final ServletContext container) {
final FilterRegistration.Dynamic logFilter = container.addFilter("logFilter", LogFilter.class);
logFilter.addMappingForUrlPatterns(null, false, MAPPING_ALLES);
final FilterRegistration.Dynamic corsFilter = container.addFilter("corsFilter", CorsFilter.class);
corsFilter.addMappingForUrlPatterns(null, false, MAPPING_ALLES);
}
示例10: WsServerContainer
import javax.servlet.ServletContext; //導入方法依賴的package包/類
WsServerContainer(ServletContext servletContext) {
this.servletContext = servletContext;
setInstanceManager((InstanceManager) servletContext.getAttribute(InstanceManager.class.getName()));
// Configure servlet context wide defaults
String value = servletContext.getInitParameter(
Constants.BINARY_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
if (value != null) {
setDefaultMaxBinaryMessageBufferSize(Integer.parseInt(value));
}
value = servletContext.getInitParameter(
Constants.TEXT_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
if (value != null) {
setDefaultMaxTextMessageBufferSize(Integer.parseInt(value));
}
value = servletContext.getInitParameter(
Constants.ENFORCE_NO_ADD_AFTER_HANDSHAKE_CONTEXT_INIT_PARAM);
if (value != null) {
setEnforceNoAddAfterHandshake(Boolean.parseBoolean(value));
}
// Executor config
int executorCoreSize = 0;
long executorKeepAliveTimeSeconds = 60;
value = servletContext.getInitParameter(
Constants.EXECUTOR_CORE_SIZE_INIT_PARAM);
if (value != null) {
executorCoreSize = Integer.parseInt(value);
}
value = servletContext.getInitParameter(
Constants.EXECUTOR_KEEPALIVETIME_SECONDS_INIT_PARAM);
if (value != null) {
executorKeepAliveTimeSeconds = Long.parseLong(value);
}
FilterRegistration.Dynamic fr = servletContext.addFilter(
"Tomcat WebSocket (JSR356) Filter", new WsFilter());
fr.setAsyncSupported(true);
EnumSet<DispatcherType> types = EnumSet.of(DispatcherType.REQUEST,
DispatcherType.FORWARD);
fr.addMappingForUrlPatterns(types, true, "/*");
// Use a per web application executor for any threads that the WebSocket
// server code needs to create. Group all of the threads under a single
// ThreadGroup.
StringBuffer threadGroupName = new StringBuffer("WebSocketServer-");
if ("".equals(servletContext.getContextPath())) {
threadGroupName.append("ROOT");
} else {
threadGroupName.append(servletContext.getContextPath());
}
threadGroup = new ThreadGroup(threadGroupName.toString());
WsThreadFactory wsThreadFactory = new WsThreadFactory(threadGroup);
executorService = new ThreadPoolExecutor(executorCoreSize,
Integer.MAX_VALUE, executorKeepAliveTimeSeconds, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(), wsThreadFactory);
}
示例11: WsServerContainer
import javax.servlet.ServletContext; //導入方法依賴的package包/類
WsServerContainer(ServletContext servletContext) {
this.servletContext = servletContext;
setInstanceManager((InstanceManager) servletContext.getAttribute(InstanceManager.class.getName()));
// Configure servlet context wide defaults
String value = servletContext.getInitParameter(Constants.BINARY_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
if (value != null) {
setDefaultMaxBinaryMessageBufferSize(Integer.parseInt(value));
}
value = servletContext.getInitParameter(Constants.TEXT_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
if (value != null) {
setDefaultMaxTextMessageBufferSize(Integer.parseInt(value));
}
value = servletContext.getInitParameter(Constants.ENFORCE_NO_ADD_AFTER_HANDSHAKE_CONTEXT_INIT_PARAM);
if (value != null) {
setEnforceNoAddAfterHandshake(Boolean.parseBoolean(value));
}
// Executor config
int executorCoreSize = 0;
long executorKeepAliveTimeSeconds = 60;
value = servletContext.getInitParameter(Constants.EXECUTOR_CORE_SIZE_INIT_PARAM);
if (value != null) {
executorCoreSize = Integer.parseInt(value);
}
value = servletContext.getInitParameter(Constants.EXECUTOR_KEEPALIVETIME_SECONDS_INIT_PARAM);
if (value != null) {
executorKeepAliveTimeSeconds = Long.parseLong(value);
}
FilterRegistration.Dynamic fr = servletContext.addFilter("Tomcat WebSocket (JSR356) Filter", new WsFilter());
fr.setAsyncSupported(true);
EnumSet<DispatcherType> types = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
fr.addMappingForUrlPatterns(types, true, "/*");
// Use a per web application executor for any threads that the WebSocket
// server code needs to create. Group all of the threads under a single
// ThreadGroup.
StringBuffer threadGroupName = new StringBuffer("WebSocketServer-");
if ("".equals(servletContext.getContextPath())) {
threadGroupName.append("ROOT");
} else {
threadGroupName.append(servletContext.getContextPath());
}
threadGroup = new ThreadGroup(threadGroupName.toString());
WsThreadFactory wsThreadFactory = new WsThreadFactory(threadGroup);
executorService = new ThreadPoolExecutor(executorCoreSize, Integer.MAX_VALUE, executorKeepAliveTimeSeconds,
TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), wsThreadFactory);
}