javax.servlet.ServletContext.getContext()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(97)

本文整理了Java中javax.servlet.ServletContext.getContext()方法的一些代码示例,展示了ServletContext.getContext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServletContext.getContext()方法的具体详情如下:
包路径:javax.servlet.ServletContext
类名称:ServletContext
方法名:getContext

ServletContext.getContext介绍

[英]Returns a ServletContext object that corresponds to a specified URL on the server.

This method allows servlets to gain access to the context for various parts of the server, and as needed obtain RequestDispatcherobjects from the context. The given path must be begin with "/", is interpreted relative to the server's document root and is matched against the context roots of other web applications hosted on this container.

In a security conscious environment, the servlet container may return null for a given URL.
[中]返回与服务器上指定URL相对应的ServletContext对象。
此方法允许servlet访问服务器各个部分的上下文,并根据需要从上下文中获取RequestDispatcherObject。给定路径必须以“/”开头,相对于服务器的文档根进行解释,并与托管在此容器上的其他web应用程序的上下文根进行匹配。
在有安全意识的环境中,servlet容器可能会为给定的URL返回null

代码示例

代码示例来源:origin: igniterealtime/Openfire

@Override
public ServletContext getContext( String s )
{
  return proxy.getContext( s );
}

代码示例来源:origin: com.betfair.cougar/jetty-transport

@Override
  public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    if (pathAliases.containsKey(target)) {
      String newTarget = pathAliases.get(target);
      ServletContext context = request.getServletContext().getContext(newTarget);
      String newPath = newTarget.substring(context.getContextPath().length());
      context.getRequestDispatcher(newPath).forward(request, response);
      return;
    }
    super.handle(target, baseRequest, request, response);
  }
}

代码示例来源:origin: apache/nifi

private void forwardToMessagePage(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final String message) throws Exception {
  httpServletRequest.setAttribute("title", OIDC_ERROR_TITLE);
  httpServletRequest.setAttribute("messages", message);
  final ServletContext uiContext = httpServletRequest.getServletContext().getContext("/nifi");
  uiContext.getRequestDispatcher("/WEB-INF/pages/message-page.jsp").forward(httpServletRequest, httpServletResponse);
}

代码示例来源:origin: Red5/red5-server

/**
 * Sets the local app context variable based on host id if available in the servlet context.
 */
private final void getAppContext() {
  //get the host id
  String hostId = null;
  //get host from servlet context
  if (servletContext != null) {
    ServletContext sctx = servletContext.getContext(contextPath);
    if (sctx != null) {
      hostId = (String) sctx.getAttribute("red5.host.id");
      log.trace("Host id from init param: {}", hostId);
    }
  }
  if (hostId != null) {
    appContext = LoaderBase.getRed5ApplicationContext(hostId + contextPath);
  } else {
    appContext = LoaderBase.getRed5ApplicationContext(contextPath);
  }
}

代码示例来源:origin: Red5/red5-server

public void registerSubContext(String webAppKey) {
  // get the sub contexts - servlet context
  ServletContext ctx = servletContext.getContext(webAppKey);
  if (ctx == null) {
    ctx = servletContext;
  }
  ContextLoader loader = new ContextLoader();
  ConfigurableWebApplicationContext appCtx = (ConfigurableWebApplicationContext) loader.initWebApplicationContext(ctx);
  appCtx.setParent(applicationContext);
  appCtx.refresh();
  ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
  ConfigurableBeanFactory appFactory = appCtx.getBeanFactory();
  logger.debug("About to grab Webcontext bean for {}", webAppKey);
  Context webContext = (Context) appCtx.getBean("web.context");
  webContext.setCoreBeanFactory(parentFactory);
  webContext.setClientRegistry(clientRegistry);
  webContext.setServiceInvoker(globalInvoker);
  webContext.setScopeResolver(globalResolver);
  webContext.setMappingStrategy(globalStrategy);
  WebScope scope = (WebScope) appFactory.getBean("web.scope");
  scope.setServer(server);
  scope.setParent(global);
  scope.register();
  scope.start();
  // register the context so we dont try to reinitialize it
  registeredContexts.add(ctx);
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

private static boolean _resourceExists(
    ServletContext servletContext, Theme theme, String portletId,
    String path)
  throws Exception {
  if (Validator.isNull(path)) {
    return false;
  }
  String resourcePath = getResourcePath(
    servletContext, theme, portletId, path);
  String extension = theme.getTemplateExtension();
  if (extension.equals(TEMPLATE_EXTENSION_FTL)) {
    return TemplateResourceLoaderUtil.hasTemplateResource(
      TemplateConstants.LANG_TYPE_FTL, resourcePath);
  }
  URL url = null;
  if (theme.isWARFile()) {
    ServletContext themeServletContext = servletContext.getContext(
      theme.getContextPath());
    url = themeServletContext.getResource(resourcePath);
  }
  else {
    url = servletContext.getResource(resourcePath);
  }
  if (url == null) {
    return false;
  }
  return true;
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

ServletContext portalServletContext = servletContext.getContext(
  uriPath);

代码示例来源:origin: OpenNMS/opennms

@Override
public ServletContext getContext(String uri)
{
  return this.context.getContext(uri);
}

代码示例来源:origin: apache/felix

@Override
public ServletContext getContext(String uripath)
{
  return delegatee.getContext(uripath);
}

代码示例来源:origin: org.apache.felix/org.apache.felix.http.base

@Override
public ServletContext getContext(final String uri)
{
  return this.context.getContext(uri);
}

代码示例来源:origin: org.apache.felix/org.apache.felix.http.base

@Override
public ServletContext getContext(String uripath)
{
  return delegatee.getContext(uripath);
}

代码示例来源:origin: org.apache.felix/org.apache.felix.http.base

@Override
public ServletContext getContext(String uri)
{
  return this.context.getContext(uri);
}

代码示例来源:origin: org.opennms.container/org.opennms.container.bridge

@Override
public ServletContext getContext(String uri)
{
  return this.context.getContext(uri);
}

代码示例来源:origin: apache/felix

@Override
public ServletContext getContext(final String uri)
{
  return this.context.getContext(uri);
}

代码示例来源:origin: apache/felix

@Override
public ServletContext getContext(String uri)
{
  return this.context.getContext(uri);
}

代码示例来源:origin: org.igniterealtime.openfire/xmppserver

@Override
public ServletContext getContext( String s )
{
  return proxy.getContext( s );
}

代码示例来源:origin: stackoverflow.com

ServletContext web1 = getServletContext();
ServletContext web2 = web1.getContext("/web2");
RequestDispatcher dispatcher = web2.getRequestDispatcher("/servlet2");
dispatcher.forward(request, response);

代码示例来源:origin: weld/core

@Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext cx =getServletContext();
    ServletContext secondCx=cx.getContext("/app2");
    secondCx.getRequestDispatcher("/included").forward(req, resp);
  }
}

代码示例来源:origin: org.zkoss.zk/zk

public WebApp getWebApp(String uripath) {
  final ServletContext another = _ctx.getContext(uripath);
  if (another != null) {
    final WebManager webman = WebManager.getWebManagerIfAny(another);
    if (webman != null)
      return webman.getWebApp();
  }
  return null;
}

代码示例来源:origin: org.apache.portals.jetspeed-2/jetspeed-commons

private PortletInstance getPortletInstance(String portletUniqueName) throws PortletException
{
  PortletRegistry registry =  Jetspeed.getComponentManager().lookupComponent("portletRegistry");
  PortletFactory portletFactory = Jetspeed.getComponentManager().lookupComponent("portletFactory");
  ServletContext portalAppContext = ((ServletConfig) Jetspeed.getComponentManager().lookupComponent("ServletConfig")).getServletContext();
  
  PortletDefinition portletDef = registry.getPortletDefinitionByUniqueName(portletUniqueName, true);
  PortletApplication portletApp = portletDef.getApplication();
  ServletContext portletAppContext = portalAppContext.getContext(portletApp.getContextPath());
  
  return portletFactory.getPortletInstance(portletAppContext, portletDef, false);
}

相关文章

微信公众号

最新文章

更多

ServletContext类方法