本文整理汇总了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);
}
示例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;
}
示例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();
}
}
示例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");
}
示例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);
}
示例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 );
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
}
示例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);
}
}
}
示例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;
}
示例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;
}