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

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

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

ServletContext.getResource介绍

[英]Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.

This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.

This method returns null if no resource is mapped to the pathname.

Some containers may allow writing to the URL returned by this method using the methods of the URL class.

The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.

This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.
[中]返回映射到指定路径的资源的URL。路径必须以“/”开头,并被解释为相对于当前上下文根。
此方法允许servlet容器从任何源向servlet提供资源。资源可以位于本地或远程文件系统、数据库或.war文件中。
servlet容器必须实现访问资源所需的URL处理程序和URLConnection对象。
如果没有资源映射到路径名,则此方法返回null
某些容器可能允许使用URL类的方法写入此方法返回的URL。
资源内容直接返回,因此请注意,请求.jsp页面会返回JSP源代码。使用RequestDispatcher来包含执行结果。
此方法的用途与java.lang.Class.getResource不同,后者基于类装入器查找资源。此方法不使用类装入器。

代码示例

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

/**
 * This implementation checks {@code ServletContext.getResource}.
 * @see javax.servlet.ServletContext#getResource(String)
 */
@Override
public boolean exists() {
  try {
    URL url = this.servletContext.getResource(this.path);
    return (url != null);
  }
  catch (MalformedURLException ex) {
    return false;
  }
}

代码示例来源:origin: jenkinsci/jenkins

URL bundled = servletContext.getResource("/WEB-INF/"+ hookGroovy);
  execute(bundled);
} catch (IOException e) {
      URL bundled = servletContext.getResource(res);
      execute(bundled);
    } catch (IOException e) {

代码示例来源:origin: Atmosphere/atmosphere

public void loadConfiguration(ServletConfig sc) throws ServletException {
  if (!autoDetectHandlers) return;
  try {
    URL url = sc.getServletContext().getResource(handlersPath);
    URLClassLoader urlC = new URLClassLoader(new URL[]{url},
        Thread.currentThread().getContextClassLoader());
    loadAtmosphereDotXml(sc.getServletContext().
        getResourceAsStream(atmosphereDotXmlPath), urlC);
    if (atmosphereHandlers.isEmpty()) {
      autoDetectAtmosphereHandlers(sc.getServletContext(), urlC);
      if (atmosphereHandlers.isEmpty()) {
        detectSupportedFramework(sc);
      }
    }
    autoDetectWebSocketHandler(sc.getServletContext(), urlC);
  } catch (Throwable t) {
    throw new ServletException(t);
  }
}

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

ServletContext context = getContext();
URL resourceUrl = context.getResource("/WEB-INF/test/foo.txt");

代码示例来源:origin: org.freemarker/freemarker

private void addTldLocationsFromWebXml() throws SAXException, IOException {
  LOG.debug("Looking for TLD locations in servletContext:/WEB-INF/web.xml");
  WebXmlParser webXmlParser = new WebXmlParser();
  InputStream in = servletContext.getResourceAsStream("/WEB-INF/web.xml");
  if (in == null) {
    LOG.debug("No web.xml was found in servlet context");
    return;
  }
  try {
    parseXml(in, servletContext.getResource("/WEB-INF/web.xml").toExternalForm(), webXmlParser);
  } finally {
    in.close();
  }
}

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

/**
 * This implementation checks {@code ServletContext.getResource}.
 * @see javax.servlet.ServletContext#getResource(String)
 */
@Override
public boolean exists() {
  try {
    URL url = this.servletContext.getResource(this.path);
    return (url != null);
  }
  catch (MalformedURLException ex) {
    return false;
  }
}

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

public boolean exists() {
  try {
    return (this.servletContext.getResource(this.path) != null);
  } catch (final MalformedURLException e) {
    return false;
  }
}

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

/**
 * This implementation delegates to {@code ServletContext.getResource},
 * but throws a FileNotFoundException if no resource found.
 * @see javax.servlet.ServletContext#getResource(String)
 */
@Override
public URL getURL() throws IOException {
  URL url = this.servletContext.getResource(this.path);
  if (url == null) {
    throw new FileNotFoundException(
        getDescription() + " cannot be resolved to URL because it does not exist");
  }
  return url;
}

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

@Override
public boolean isFile() {
  try {
    URL url = this.servletContext.getResource(this.path);
    if (url != null && ResourceUtils.isFileURL(url)) {
      return true;
    }
    else {
      return (this.servletContext.getRealPath(this.path) != null);
    }
  }
  catch (MalformedURLException ex) {
    return false;
  }
}

代码示例来源:origin: Atmosphere/atmosphere

public static String realPath(ServletContext servletContext, String targetPath) throws MalformedURLException {
    String realPath = servletContext.getRealPath(targetPath);
    if (realPath == null) {
      URL u = servletContext.getResource(targetPath);
      if (u != null) {
        realPath = u.getPath();
      } else {
        return "";
      }
    }
    return realPath;
  }
}

代码示例来源:origin: org.freemarker/freemarker

public String getXmlSystemId() throws IOException {
  final URL url = servletContext.getResource(fileResourcePath);
  return url != null ? url.toExternalForm() : null;
}

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

/**
 * This implementation delegates to {@code ServletContext.getResource},
 * but throws a FileNotFoundException if no resource found.
 * @see javax.servlet.ServletContext#getResource(String)
 */
@Override
public URL getURL() throws IOException {
  URL url = this.servletContext.getResource(this.path);
  if (url == null) {
    throw new FileNotFoundException(
        getDescription() + " cannot be resolved to URL because it does not exist");
  }
  return url;
}

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

@Override
public boolean isFile() {
  try {
    URL url = this.servletContext.getResource(this.path);
    if (url != null && ResourceUtils.isFileURL(url)) {
      return true;
    }
    else {
      return (this.servletContext.getRealPath(this.path) != null);
    }
  }
  catch (MalformedURLException ex) {
    return false;
  }
}

代码示例来源:origin: Atmosphere/atmosphere

void loadWebApplication(ServletConfig sc) throws Exception {
  URL url = sc.getServletContext().getResource("/WEB-INF/lib/");
  URLClassLoader urlC = new URLClassLoader(new URL[]{url},
      Thread.currentThread().getContextClassLoader());
  loadServlet(sc, urlC);
  if (!filters.isEmpty()) {
    loadFilterInstances(sc);
  } else {
    loadFilterClasses(sc, urlC);
  }
}

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

/**
 * This implementation resolves "file:" URLs or alternatively delegates to
 * {@code ServletContext.getRealPath}, throwing a FileNotFoundException
 * if not found or not resolvable.
 * @see javax.servlet.ServletContext#getResource(String)
 * @see javax.servlet.ServletContext#getRealPath(String)
 */
@Override
public File getFile() throws IOException {
  URL url = this.servletContext.getResource(this.path);
  if (url != null && ResourceUtils.isFileURL(url)) {
    // Proceed with file system resolution...
    return super.getFile();
  }
  else {
    String realPath = WebUtils.getRealPath(this.servletContext, this.path);
    return new File(realPath);
  }
}

代码示例来源:origin: jenkinsci/jenkins

URL res = Jenkins.getInstance().servletContext.getResource("/WEB-INF/" + name);
if(res==null) {
  throw new FileNotFoundException(name); // giving up

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

static void init(ServletContext servletContext) {
  String webPath = servletContext.getRealPath("/");
  if (webPath == null) {
    try {
      // 支持 weblogic: http://www.jfinal.com/feedback/1994
      webPath = servletContext.getResource("/").getPath();
    } catch (java.net.MalformedURLException e) {
      com.jfinal.kit.LogKit.error(e.getMessage(), e);
    }
  }
  properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, webPath);
  properties.setProperty(Velocity.ENCODING_DEFAULT, getEncoding()); 
  properties.setProperty(Velocity.INPUT_ENCODING, getEncoding()); 
  properties.setProperty(Velocity.OUTPUT_ENCODING, getEncoding());
}

代码示例来源:origin: jenkinsci/jenkins

URL dependencyURL = context.getResource(fromPath + "/" + artifactId + ".hpi");
  dependencyURL = context.getResource(fromPath + "/" + artifactId + ".jpi");

代码示例来源:origin: oblac/jodd

/**
 * Returns <code>true</code> if target exists.
 */
protected boolean targetExists(final ActionRequest actionRequest, final String target) {
  if (log.isDebugEnabled()) {
    log.debug("target check: " + target);
  }
  final ServletContext servletContext = actionRequest.getHttpServletRequest().getServletContext();
  try {
    return servletContext.getResource(target) != null;
  } catch (MalformedURLException ignore) {
    return false;
  }
}

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

/**
 * This implementation resolves "file:" URLs or alternatively delegates to
 * {@code ServletContext.getRealPath}, throwing a FileNotFoundException
 * if not found or not resolvable.
 * @see javax.servlet.ServletContext#getResource(String)
 * @see javax.servlet.ServletContext#getRealPath(String)
 */
@Override
public File getFile() throws IOException {
  URL url = this.servletContext.getResource(this.path);
  if (url != null && ResourceUtils.isFileURL(url)) {
    // Proceed with file system resolution...
    return super.getFile();
  }
  else {
    String realPath = WebUtils.getRealPath(this.servletContext, this.path);
    return new File(realPath);
  }
}

相关文章

微信公众号

最新文章

更多

ServletContext类方法