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

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

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

ServletContext.getContextPath介绍

[英]Return the main path associated with this context.
[中]返回与此上下文关联的主路径。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public String getApplicationName() {
  return (this.servletContext != null ? this.servletContext.getContextPath() : "");
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public String getApplicationName() {
  return (this.servletContext != null ? this.servletContext.getContextPath() : "");
}

代码示例来源:origin: codecentric/spring-boot-admin

protected String getServerContextPath() {
  return servletContext.getContextPath();
}

代码示例来源:origin: alibaba/druid

private static String getContextPath_2_5(ServletContext context) {
  String contextPath = context.getContextPath();
  if (contextPath == null || contextPath.length() == 0) {
    contextPath = "/";
  }
  return contextPath;
}

代码示例来源:origin: org.springframework/spring-web

@Override
public String getApplicationName() {
  return (this.servletContext != null ? this.servletContext.getContextPath() : "");
}

代码示例来源:origin: org.springframework/spring-web

@Override
public String getApplicationName() {
  return (this.servletContext != null ? this.servletContext.getContextPath() : "");
}

代码示例来源:origin: gocd/gocd

public String getContextPath() {
  return this.servletContext.getContextPath();
}

代码示例来源:origin: SonarSource/sonarqube

private String loadHtmlFile(String path, String serverStatus, String instance) {
  try (InputStream input = servletContext.getResourceAsStream(path)) {
   String template = IOUtils.toString(requireNonNull(input), UTF_8);
   return template
    .replaceAll(WEB_CONTEXT_PLACEHOLDER, servletContext.getContextPath())
    .replaceAll(SERVER_STATUS_PLACEHOLDER, serverStatus)
    .replaceAll(INSTANCE_PLACEHOLDER, instance)
    .replaceAll(OFFICIAL_PLACEHOLDER, String.valueOf(officialDistribution.check()));
  } catch (Exception e) {
   throw new IllegalStateException("Fail to load file " + path, e);
  }
 }
}

代码示例来源:origin: alibaba/nacos

private void checkServerHealth() {
  long startCheckTime = System.currentTimeMillis();
  for (String serverIp : serverList) {
    // Compatible with old codes,use status.taobao
    String url = "http://" + serverIp + servletContext.getContextPath() + Constants.HEALTH_CONTROLLER_PATH;
    // "/nacos/health";
    HttpGet request = new HttpGet(url);
    httpclient.execute(request, new AyscCheckServerHealthCallBack(serverIp));
  }
  long endCheckTime = System.currentTimeMillis();
  long cost = endCheckTime - startCheckTime;
  defaultLog.debug("checkServerHealth cost: {}", cost);
}

代码示例来源:origin: alibaba/nacos

@SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
  Loggers.SRV_LOG.info("[SERVER-INIT] got port: {}", event.getWebServer().getPort());
  Loggers.SRV_LOG.info("[SERVER-INIT] got path: {}", servletContext.getContextPath());
  serverPort = event.getWebServer().getPort();
  contextPath = servletContext.getContextPath();
  try {
    RaftCore.init();
  } catch (Exception e) {
    Loggers.RAFT.error("[NACOS-RAFT] {} {}", "failed to initialize raft sub system", e);
  }
}

代码示例来源:origin: SonarSource/sonarqube

@Before
public void setUp() throws Exception {
 when(servletContext.getContextPath()).thenReturn(TEST_CONTEXT);
 when(servletContext.getResourceAsStream("/index.html")).thenAnswer(
  (Answer<InputStream>) invocationOnMock -> toInputStream("Content of default index.html with context [%WEB_CONTEXT%], status [%SERVER_STATUS%], instance [%INSTANCE%]",
   UTF_8));
 when(servletContext.getResourceAsStream("/integration/vsts/index.html"))
  .thenAnswer((Answer<InputStream>) invocationOnMock -> toInputStream("Content of vsts index.html with context [%WEB_CONTEXT%]", UTF_8));
}

代码示例来源:origin: SonarSource/sonarqube

@Before
public void setUp() throws Exception {
 when(servletContext.getContextPath()).thenReturn(TEST_CONTEXT);
}

代码示例来源:origin: jersey/jersey

@Override
public void contextInitialized(final ServletContextEvent sce) {
  if (getEa() != null) {
    final String contextPath = sce.getServletContext().getContextPath();
    getEa().sendEvent(new Event("jersey/test/DEPLOYED", new HashMap<String, String>() {{
      put("context-path", contextPath);
    }}));
  }
}

代码示例来源:origin: jersey/jersey

@Override
public void contextDestroyed(final ServletContextEvent sce) {
  if (getEa() != null) {
    getEa().sendEvent(new Event("jersey/test/UNDEPLOYED", new HashMap<String, String>() {{
      put("context-path", sce.getServletContext().getContextPath());
    }}));
  }
}

代码示例来源:origin: alibaba/nacos

@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
  setServerPort(event.getWebServer().getPort());
  setContextPath(servletContext.getContextPath());
}

代码示例来源:origin: SonarSource/sonarqube

@Before
public void initialize() {
 FilterConfig filterConfig = mock(FilterConfig.class);
 ServletContext context = mock(ServletContext.class);
 when(context.getContextPath()).thenReturn("/context");
 when(filterConfig.getServletContext()).thenReturn(context);
 underTest = new RootFilter();
 underTest.init(filterConfig);
}

代码示例来源:origin: spring-projects/spring-framework

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
  if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
    // The application context id is still set to its original default value
    // -> assign a more useful id based on available information
    String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
    if (idParam != null) {
      wac.setId(idParam);
    }
    else {
      // Generate default id...
      wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
          ObjectUtils.getDisplayString(sc.getContextPath()));
    }
  }
  wac.setServletContext(sc);
  String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
  if (configLocationParam != null) {
    wac.setConfigLocation(configLocationParam);
  }
  // The wac environment's #initPropertySources will be called in any case when the context
  // is refreshed; do it eagerly here to ensure servlet property sources are in place for
  // use in any post-processing or initialization that occurs below prior to #refresh
  ConfigurableEnvironment env = wac.getEnvironment();
  if (env instanceof ConfigurableWebEnvironment) {
    ((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
  }
  customizeContext(sc, wac);
  wac.refresh();
}

代码示例来源:origin: wildfly/wildfly

/**
 * <p>
 * Builds the JASPIC application context.
 * </p>
 *
 * @return a {@code String} representing the application context.
 */
private String buildAppContext() {
  final ServletRequestContext requestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
  ServletRequest servletRequest = requestContext.getServletRequest();
  return servletRequest.getServletContext().getVirtualServerName() + " " + servletRequest.getServletContext().getContextPath();
}

代码示例来源:origin: jfinal/jfinal

void init(JFinalConfig jfinalConfig, ServletContext servletContext) {
  this.servletContext = servletContext;
  this.contextPath = servletContext.getContextPath();
  
  initPathKit();
  
  Config.configJFinal(jfinalConfig);    // start plugin, init log factory and init engine in this method
  constants = Config.getConstants();
  
  initActionMapping();
  initHandler();
  initRender();
  initOreillyCos();
  initTokenManager();
}

代码示例来源:origin: spring-projects/spring-framework

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
  if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
    // The application context id is still set to its original default value
    // -> assign a more useful id based on available information
    if (this.contextId != null) {
      wac.setId(this.contextId);
    }
    else {
      // Generate default id...
      wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
          ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName());
    }
  }
  wac.setServletContext(getServletContext());
  wac.setServletConfig(getServletConfig());
  wac.setNamespace(getNamespace());
  wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
  // The wac environment's #initPropertySources will be called in any case when the context
  // is refreshed; do it eagerly here to ensure servlet property sources are in place for
  // use in any post-processing or initialization that occurs below prior to #refresh
  ConfigurableEnvironment env = wac.getEnvironment();
  if (env instanceof ConfigurableWebEnvironment) {
    ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
  }
  postProcessWebApplicationContext(wac);
  applyInitializers(wac);
  wac.refresh();
}

相关文章

微信公众号

最新文章

更多

ServletContext类方法