当前位置: 首页>>代码示例>>Java>>正文


Java ServletContext.getResource方法代码示例

本文整理汇总了Java中javax.servlet.ServletContext.getResource方法的典型用法代码示例。如果您正苦于以下问题:Java ServletContext.getResource方法的具体用法?Java ServletContext.getResource怎么用?Java ServletContext.getResource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.servlet.ServletContext的用法示例。


在下文中一共展示了ServletContext.getResource方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: requestHandler

import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
 * Process our request and locate right SSI command.
 * 
 * @param req
 *            a value of type 'HttpServletRequest'
 * @param res
 *            a value of type 'HttpServletResponse'
 */
protected void requestHandler(HttpServletRequest req,
        HttpServletResponse res) throws IOException {
    ServletContext servletContext = getServletContext();
    String path = SSIServletRequestUtil.getRelativePath(req);
    if (debug > 0)
        log("SSIServlet.requestHandler()\n" + "Serving "
                + (buffered?"buffered ":"unbuffered ") + "resource '"
                + path + "'");
    // Exclude any resource in the /WEB-INF and /META-INF subdirectories
    // (the "toUpperCase()" avoids problems on Windows systems)
    if (path == null || path.toUpperCase(Locale.ENGLISH).startsWith("/WEB-INF")
            || path.toUpperCase(Locale.ENGLISH).startsWith("/META-INF")) {
        res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
        log("Can't serve file: " + path);
        return;
    }
    URL resource = servletContext.getResource(path);
    if (resource == null) {
        res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
        log("Can't find file: " + path);
        return;
    }
    String resourceMimeType = servletContext.getMimeType(path);
    if (resourceMimeType == null) {
        resourceMimeType = "text/html";
    }
    res.setContentType(resourceMimeType + ";charset=" + outputEncoding);
    if (expires != null) {
        res.setDateHeader("Expires", (new java.util.Date()).getTime()
                + expires.longValue() * 1000);
    }
    req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
    processSSI(req, res, resource);
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:43,代码来源:SSIServlet.java

示例2: getURLConnection

import javax.servlet.ServletContext; //导入方法依赖的package包/类
protected URLConnection getURLConnection(String originalPath,
        boolean virtual) throws IOException {
    ServletContextAndPath csAndP = getServletContextAndPath(originalPath,
            virtual);
    ServletContext context = csAndP.getServletContext();
    String path = csAndP.getPath();
    URL url = context.getResource(path);
    if (url == null) {
        throw new IOException("Context did not contain resource: " + path);
    }
    URLConnection urlConnection = url.openConnection();
    return urlConnection;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:14,代码来源:SSIServletExternalResolver.java

示例3: doGet

import javax.servlet.ServletContext; //导入方法依赖的package包/类
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    resp.setContentType("text/plain");

    ServletContext context = getServletContext();

    // Check resources individually
    URL url = context.getResource(req.getParameter("path"));
    if (url == null) {
        resp.getWriter().println("Not found");
        return;
    }

    InputStream input = url.openStream();
    OutputStream output = resp.getOutputStream();
    try {
        byte[] buffer = new byte[4000];
        for (int len; (len = input.read(buffer)) > 0;) {
            output.write(buffer, 0, len);
        }
    } finally {
        input.close();
        output.close();
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:28,代码来源:TestStandardContextResources.java

示例4: doGet

import javax.servlet.ServletContext; //导入方法依赖的package包/类
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    resp.setContentType("text/plain");

    ServletContext context = getServletContext();

    // Check resources individually
    URL url = context.getResource("/WEB-INF/lib/taglibs-standard-spec-1.2.5.jar");
    if (url != null) {
        resp.getWriter().write("00-PASS\n");
    }

    url = context.getResource("/WEB-INF/lib/taglibs-standard-impl-1.2.5.jar");
    if (url != null) {
        resp.getWriter().write("01-PASS\n");
    }

    // Check a directory listing
    Set<String> libs = context.getResourcePaths("/WEB-INF/lib");
    if (libs == null) {
        return;
    }

    if (!libs.contains("/WEB-INF/lib/taglibs-standard-spec-1.2.5.jar")) {
        return;
    }
    if (!libs.contains("/WEB-INF/lib/taglibs-standard-impl-1.2.5.jar")) {
        return;
    }

    resp.getWriter().write("02-PASS\n");
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:35,代码来源:TestStandardContextAliases.java

示例5: requestHandler

import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
 * Process our request and locate right SSI command.
 * 
 * @param req
 *            a value of type 'HttpServletRequest'
 * @param res
 *            a value of type 'HttpServletResponse'
 */
protected void requestHandler(HttpServletRequest req,
        HttpServletResponse res) throws IOException, ServletException {
    ServletContext servletContext = getServletContext();
    String path = SSIServletRequestUtil.getRelativePath(req);
    if (debug > 0)
        log("SSIServlet.requestHandler()\n" + "Serving "
                + (buffered?"buffered ":"unbuffered ") + "resource '"
                + path + "'");
    // Exclude any resource in the /WEB-INF and /META-INF subdirectories
    // (the "toUpperCase()" avoids problems on Windows systems)
    if (path == null || path.toUpperCase().startsWith("/WEB-INF")
            || path.toUpperCase().startsWith("/META-INF")) {
        res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
        log("Can't serve file: " + path);
        return;
    }
    URL resource = servletContext.getResource(path);
    if (resource == null) {
        res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
        log("Can't find file: " + path);
        return;
    }
    String resourceMimeType = servletContext.getMimeType(path);
    if (resourceMimeType == null) {
        resourceMimeType = "text/html";
    }
    res.setContentType(resourceMimeType + ";charset=" + outputEncoding);
    if (expires != null) {
        res.setDateHeader("Expires", (new java.util.Date()).getTime()
                + expires.longValue() * 1000);
    }
    req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
    processSSI(req, res, resource);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:43,代码来源:SSIServlet.java

示例6: requestHandler

import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
    * Process our request and locate right SSI command.
    * @param req a value of type 'HttpServletRequest'
    * @param res a value of type 'HttpServletResponse'
    */
   protected void requestHandler(HttpServletRequest req,
                               HttpServletResponse res)
       throws IOException, ServletException {

       ServletContext servletContext = getServletContext();
       String path = SSIServletRequestUtil.getRelativePath( req );

       if (debug > 0)
           log("SSIServlet.requestHandler()\n" +
               "Serving " + (buffered ? "buffered " : "unbuffered ") +
               "resource '" + path + "'");

       // Exclude any resource in the /WEB-INF and /META-INF subdirectories
       // (the "toUpperCase()" avoids problems on Windows systems)
       if ( path == null ||
            path.toUpperCase().startsWith("/WEB-INF") ||
            path.toUpperCase().startsWith("/META-INF") ) {

           res.sendError(res.SC_NOT_FOUND, path);
    log( "Can't serve file: " + path );
           return;
       }

URL resource = servletContext.getResource(path);
       if (resource==null) {
           res.sendError(res.SC_NOT_FOUND, path);
    log( "Can't find file: " + path );
           return;
       }

       res.setContentType("text/html;charset=UTF-8");

       if (expires != null) {
           res.setDateHeader("Expires", (
               new java.util.Date()).getTime() + expires.longValue() * 1000);
       }

processSSI( req, res, resource );
   }
 
开发者ID:c-rainstorm,项目名称:jerrydog,代码行数:45,代码来源:SSIServlet.java

示例7: getURLConnection

import javax.servlet.ServletContext; //导入方法依赖的package包/类
protected URLConnection getURLConnection( String originalPath, boolean virtual ) throws IOException {
ServletContextAndPath csAndP = getServletContextAndPath( originalPath, virtual );
ServletContext context = csAndP.getServletContext();
String path = csAndP.getPath();
   
URL url = context.getResource( path );
if ( url == null ) {
    throw new IOException("Context did not contain resource: " + path );
}
URLConnection urlConnection = url.openConnection();				    
return urlConnection;
   }
 
开发者ID:c-rainstorm,项目名称:jerrydog,代码行数:13,代码来源:SSIServletExternalResolver.java

示例8: requestHandler

import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
 * Process our request and locate right SSI command.
 * 
 * @param req
 *            a value of type 'HttpServletRequest'
 * @param res
 *            a value of type 'HttpServletResponse'
 */
protected void requestHandler(HttpServletRequest req, HttpServletResponse res) throws IOException {
	ServletContext servletContext = getServletContext();
	String path = SSIServletRequestUtil.getRelativePath(req);
	if (debug > 0)
		log("SSIServlet.requestHandler()\n" + "Serving " + (buffered ? "buffered " : "unbuffered ") + "resource '"
				+ path + "'");
	// Exclude any resource in the /WEB-INF and /META-INF subdirectories
	// (the "toUpperCase()" avoids problems on Windows systems)
	if (path == null || path.toUpperCase(Locale.ENGLISH).startsWith("/WEB-INF")
			|| path.toUpperCase(Locale.ENGLISH).startsWith("/META-INF")) {
		res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
		log("Can't serve file: " + path);
		return;
	}
	URL resource = servletContext.getResource(path);
	if (resource == null) {
		res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
		log("Can't find file: " + path);
		return;
	}
	String resourceMimeType = servletContext.getMimeType(path);
	if (resourceMimeType == null) {
		resourceMimeType = "text/html";
	}
	res.setContentType(resourceMimeType + ";charset=" + outputEncoding);
	if (expires != null) {
		res.setDateHeader("Expires", (new java.util.Date()).getTime() + expires.longValue() * 1000);
	}
	req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
	processSSI(req, res, resource);
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:40,代码来源:SSIServlet.java

示例9: getURLConnection

import javax.servlet.ServletContext; //导入方法依赖的package包/类
protected URLConnection getURLConnection(String originalPath, boolean virtual) throws IOException {
	ServletContextAndPath csAndP = getServletContextAndPath(originalPath, virtual);
	ServletContext context = csAndP.getServletContext();
	String path = csAndP.getPath();
	URL url = context.getResource(path);
	if (url == null) {
		throw new IOException("Context did not contain resource: " + path);
	}
	URLConnection urlConnection = url.openConnection();
	return urlConnection;
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:12,代码来源:SSIServletExternalResolver.java

示例10: I18nStarter

import javax.servlet.ServletContext; //导入方法依赖的package包/类
public I18nStarter(I18nConfiguration configuration, Collection<URL> classpath, ServletContext servletContext){

        if(configuration==null)
            throw new IllegalArgumentException("I18nConfiguration must not be null");

        if(servletContext==null)
            throw new IllegalArgumentException("ServletContext must not be null");

        this.configuration = configuration;
        this.classpath = Optional.ofNullable(classpath);
        this.servletContext = servletContext;

        try {

            this.MAPPINGS_XSD = servletContext.getResource("/i18n-mappings.xsd");

        }catch (IOException ex){

            throw new I18nException("Failed to load the i18n-mappings.xsd resource",ex);

        }

    }
 
开发者ID:Emerjoin,项目名称:Hi-Framework,代码行数:24,代码来源:I18nStarter.java

示例11: prepareTemplates

import javax.servlet.ServletContext; //导入方法依赖的package包/类
public static void prepareTemplates(ServletContext context) throws HiException{

        AppConfigurations appConfigurations = AppConfigurations.get();

        for(String template : appConfigurations.getTemplates()) {

            try {

                URL templateHTML = context.getResource("/" + template + ".html");
                URL templateController = context.getResource("/" + template + ".js");

                if(!(templateHTML!=null&&templateController!=null)){
                    throw new NoSuchTemplateException(template);
                }

                String templHTML = Helper.readLines(templateHTML.openStream(),null);
                MVCReqHandler.storeTemplate(template,templHTML);

                String templtController = Helper.readLines(templateController.openStream(), null);

                if(!appConfigurations.underDevelopment())
                    templateControllers.put(template, templtController);

            }catch (MalformedURLException e){

                throw new HiException("Invalid template path <"+template+">",e);

            }catch (IOException ex){

                throw new NoSuchTemplateException(template);

            }

        }

    }
 
开发者ID:Emerjoin,项目名称:Hi-Framework,代码行数:37,代码来源:ES5ReqHandler.java

示例12: getFilesContext

import javax.servlet.ServletContext; //导入方法依赖的package包/类
private static Set<URL> getFilesContext(ServletContext context,String path,Set<URL> set){

        Set<String> libs = context.getResourcePaths(path);

        if(libs==null)
            return set;

        for(String filePath : libs) {

            try {

                //Directory
                if(filePath.substring(filePath.length()-1,filePath.length()).equals("/")){

                    getFilesContext(context,filePath,set);
                    continue;

                }


                URL resource = context.getResource(filePath);
                if(resource!=null)
                    set.add(resource);

            }catch (Exception ex){

                log.error(String.format("Error getting files in path %s",path),ex);

            }
        }

        return set;


    }
 
开发者ID:Emerjoin,项目名称:Hi-Framework,代码行数:36,代码来源:BootstrapUtils.java

示例13: load

import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
 * Loads a plugin by the specified plugin directory and put it into the
 * specified holder.
 *
 * @param pluginDirPath
 *            the specified plugin directory
 * @param holder
 *            the specified holder
 * @return loaded plugin
 * @throws Exception
 *             exception
 */
private AbstractPlugin load(final String pluginDirPath, final Map<String, HashSet<AbstractPlugin>> holder)
		throws Exception {
	final ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();

	String plugin = StringUtils.substringAfter(pluginDirPath, "/plugins");

	plugin = plugin.replace("/", "");

	final File file = Latkes.getWebFile("/plugins/" + plugin + "/plugin.properties");

	PropsUtil.loadFromInputStream(new FileInputStream(file));

	final URL defaultClassesFileDirURL = servletContext.getResource("/plugins/" + plugin + "classes");

	URL classesFileDirURL = null;

	try {
		classesFileDirURL = servletContext.getResource(PropsUtil.getProperty("classesDirPath"));
	} catch (final MalformedURLException e) {
		logger.error("Reads [" + PropsUtil.getProperty("classesDirPath") + "] failed", e);
	}

	final URLClassLoader classLoader = new URLClassLoader(new URL[] { defaultClassesFileDirURL, classesFileDirURL },
			PluginManager.class.getClassLoader());

	classLoaders.add(classLoader);

	String pluginClassName = PropsUtil.getProperty(Plugin.PLUGIN_CLASS);

	if (StringUtils.isBlank(pluginClassName)) {
		pluginClassName = NotInteractivePlugin.class.getName();
	}

	final String rendererId = PropsUtil.getProperty(Plugin.PLUGIN_RENDERER_ID);

	if (StringUtils.isBlank(rendererId)) {
		logger.warn("no renderer defined by this plugin[" + plugin + "],this plugin will be ignore!");
		return null;
	}

	final Class<?> pluginClass = classLoader.loadClass(pluginClassName);

	logger.trace("Loading plugin class[name={}]", pluginClassName);
	final AbstractPlugin ret = (AbstractPlugin) pluginClass.newInstance();

	ret.setRendererId(rendererId);

	setPluginProps(plugin, ret);

	register(ret, holder);

	ret.changeStatus();

	return ret;
}
 
开发者ID:daima,项目名称:solo-spring,代码行数:68,代码来源:PluginManager.java


注:本文中的javax.servlet.ServletContext.getResource方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。