我想添加过滤器,它将新参数添加到WebSphere上每个请求的标头中。我发现 Global Filter for all Web Applications和Configuring global filter in websphere 8.5我尝试创建全局上下文侦听器,添加过滤器。但是,我的所有尝试都以异常结束,该异常表示我无法从以编程方式添加的侦听器添加过滤器。
[9/21/15 9:39:29:646 CEST] 00000000 ContainerHelp E WSVR0501E: Error creating component com.ibm.ws.runtime.component.CompositionUnitMgrImpl@a344950a
com.ibm.ws.exception.RuntimeWarning: com.ibm.ws.webcontainer.exception.WebAppNotLoadedException: Failed to load webapp: Failed to load webapp: SRVE8011E: This operation cannot be executed from a programmatically added listener.
at com.ibm.ws.webcontainer.component.WebContainerImpl.install(WebContainerImpl.java:428)
at com.ibm.ws.webcontainer.component.WebContainerImpl.start(WebContainerImpl.java:714)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:1165)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectStart(DeployedApplicationImpl.java:1369)
at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:639)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:967)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:771)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl$CUInitializer$1.run(CompositionUnitMgrImpl.java:945)
at com.ibm.ws.security.auth.ContextManagerImpl.runAs(ContextManagerImpl.java:5468)
at com.ibm.ws.security.auth.ContextManagerImpl.runAsSystem(ContextManagerImpl.java:5594)
at com.ibm.ws.security.core.SecurityContext.runAsSystem(SecurityContext.java:255)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl$CUInitializer.run(CompositionUnitMgrImpl.java:950)
at com.ibm.wsspi.runtime.component.WsComponentImpl$_AsynchInitializer.run(WsComponentImpl.java:496)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1702)
听众课程:
@WebListener
public class RequestListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
try {
SimpleFilter f = context.createFilter(SimpleFilter.class);
FilterRegistration fr = context.addFilter("simpleFilter", f);
fr.addMappingForUrlPatterns(null, false, "/*");
} catch (IllegalStateException e) {
System.out.println("Filter was not added: " + e.getMessage());
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}
}
过滤类:
public class SimpleFilter implements Filter {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse httpServletResponse = (HttpServletResponse)servletResponse;
httpServletResponse.setHeader("Via", ServerName.getDisplayName());
filterChain.doFilter(servletRequest, httpServletResponse);
}
}
有没有办法让它发挥作用?
由于