通过application获取资源,它的根路径是WebContent,它可以获取web-inf下的资源

通过getclassload()获取资源,它的根路径是classes,不能获取web-inf下的资源

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestServletContext</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>username</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123123</param-value>
</context-param>

<servlet>
<description></description>
<display-name>TestContext01</display-name>
<servlet-name>TestContext01</servlet-name>
<servlet-class>com.cdsxt.context.TestContext01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestContext01</servlet-name>
<url-pattern>/context01</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>TestContext02</display-name>
<servlet-name>TestContext02</servlet-name>
<servlet-class>com.cdsxt.context.TestContext02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestContext02</servlet-name>
<url-pattern>/context02</url-pattern>
</servlet-mapping>
</web-app>

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext application = this.getServletContext();

String username = application.getInitParameter("username");
String password = application.getInitParameter("password");
System.out.println(username);
System.out.println(password);
System.out.println("--------");

application.setAttribute("username", "lisi");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
/**
* 获取ServletContext application
*
* ServletConfig.getServletContext()
* this.getServletContext();
* 不宜放大量数据
*
*
*/
System.out.println(this.getServletContext().getAttribute("username"));

//关于得到项目路径里面的文件

//有ServletContext对象去得到文件的路径

ServletContext context = this.getServletContext();
//得到系统文件
System.out.println(context.getResourceAsStream("WEB-INF/web.xml"));
System.out.println(context.getResource("WEB-INF/web.xml").getPath());

System.out.println(context.getRealPath("WEB-INF/web.xml"));

Set<String> path = context.getResourcePaths("/WEB-INF/");

for(String p:path){
System.out.println(p);
}

}

application获取资源的更多相关文章

  1. wpf 前台获取资源文件路径问题

    1 <ImageBrush ImageSource="YT.CM.CommonUI;component/Resource/FloadwindowImage/middle.png&quo ...

  2. win10 uwp 后台获取资源

    本文告诉大家,从后台代码获取界面定义的资源. 如果一个资源是写在 App 的资源,那么如何使用代码去获得他? 简单的方法是使用下面的代码 Application.Current.Resources[& ...

  3. 【spring Boot】spring boot获取资源文件的三种方式【两种情况下】

    首先声明一点,springboot获取资源文件,需要看是 1>从spring boot默认的application.properties资源文件中获取 2>还是从自定义的资源文件中获取 带 ...

  4. 2018-8-10-win10-uwp-后台获取资源

    title author date CreateTime categories win10 uwp 后台获取资源 lindexi 2018-08-10 19:17:19 +0800 2018-2-13 ...

  5. 【Spring】获取资源文件+从File+从InputStream对象获取正文数据

    1.获取资源文件或者获取文本文件等,可以通过Spring的Resource的方式获取 2.仅有File对象即可获取正文数据 3.仅有InputStream即可获取正文数据 package com.sx ...

  6. 向指定URL发送GET方法获取资源,编码问题。 Rest风格

    http编码.今天遇到获取网页上的数据,用HTTP的GET请求访问url获取资源,网上有相应的方法.以前一直不知道什么事rest风格,现在我想就是开一个Controller,然后使人可以调用你的后台代 ...

  7. Oracle中修改表名遇到“ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效”

    Oracle 11g中想修改表名: rename ASSETPORJECT to ASSETPROJECT; 结果提示:ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超 ...

  8. Android热身:通过网络获取资源并更新UI组件

    Android热身:通过网络获取资源并更新UI组件 目标 点击"发送请求"按钮,下载某网页的html源码,并显示在TextView控件上:点击"清空",清除Te ...

  9. JavaWeb基础: 获取资源文件

    Web工程在编译构建完毕以后,需要部署到Tomcat上运行,资源的硬盘路径也会随着改变.要想对资源文件进行读写操作需要获取其硬盘地址,在Web工程中通常通过ServletContext/ClassLo ...

随机推荐

  1. PHP:第五章——字符串输出函数

    <?php header("Content-Type:text/html;charset=utf-8"); /*字符串输出函数*/ //1.echo 输出一个或多个字符 // ...

  2. (C#基础)深浅拷贝理解

    一方面感觉实在无趣,不知道做什么了,纯粹来个打字练习,于是有了这个. 二方面深感自己C#基础之薄弱,于是对着园友的文章一边看,一边练习,因为很多次看了,没有多久就忘了,还有练习过程中会出现一些问题,这 ...

  3. 虚函数不应该是inlined(More Effective C++ 笔记)

    在实际运行中,虚函数所需的代价与内联函数有关. 实际上虚函数不能是内联的. 这是因为“内联”是指“在编译期间用被调用的函数体本身来代替函数调用的指令,” 但是虚函数的“虚”是指“直到运行时才能知道要调 ...

  4. Java中String.valueOf()方法的解释

    1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下 ...

  5. L3-014 周游世界 (30 分)

    周游世界是件浪漫事,但规划旅行路线就不一定了…… 全世界有成千上万条航线.铁路线.大巴线,令人眼花缭乱.所以旅行社会选择部分运输公司组成联盟,每家公司提供一条线路,然后帮助客户规划由联盟内企业支持的旅 ...

  6. eclipse工程 'cocostudio/CocoStudio.h' file not found

    android.mk里有这样配置: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) $(call import-add-path,$(LOCAL_ ...

  7. CentOS7 RPM安装 rabbitmqDownloads on Bintray

    下载 0依赖Erlang RPM for RabbitMQ包(https://github.com/rabbitmq/erlang-rpm) https://dl.bintray.com/rabbit ...

  8. python 常见问题总结

    1.ModuleNotFoundError: No module named 'urllib2' 在python3.x版本中,urllib和urllib2包集合成在一个包了import urllib2 ...

  9. emacs配置emacs-clang-complete-async

    debian下需要安装apt安装下clang和llvm sudo apt-get install llvm-dev sudo apt-get install libclang-dev 网上抄来一个大神 ...

  10. ffmpeg+nginx+video实现rtsp流转hls流,通过H5查看监控视频

    一.FFmpeg下载:http://ffmpeg.zeranoe.com/builds/ 下载并解压FFmpeg文件夹,配置环境变量:在“Path”变量原有变量值内容上加上d:\ffmpeg\bin, ...