通过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. Git分支管理及合并

    Git分支管理   建立分支 git branch [name]   切换到分支 git checkout [name]   查看有哪些分支 git branch   比较分支 git diff [b ...

  2. MongoDB驱动程序快速入门

    http://mongodb.github.io/mongo-java-driver/3.6/driver/getting-started/quick-start/

  3. BigDecimal 、BigInteger

    package com.BigDecimal; public class BigDecimalDemo { /* * 下面的运算的结果出乎我们的意料,有些准确,有些不准确 * 这是为什么呢? * 我们 ...

  4. L203 词汇题

    Conditions for the growth of this plant are optimum in early summer.we will live as free people, not ...

  5. 2018-2019-2 《网络对抗技术》Exp3免杀原理与实践 20165222

    1. 实践内容  1.1 正确使用msf编码器 使用 msfvenom -p windows/meterpreter/reverse_tcp -e x86/shikata_ga_nai -i 7 -b ...

  6. 打造html右键菜单

    今天是给大家介绍一款在网页上使用的右键菜单,原作者的网址是:http://51jsr.javaeye.com/blog/305517 这个右键菜单已经非常优秀,不过呢.却是IE Only,而且在DTD ...

  7. C语言使用pthread多线程编程(windows系统)二

    我们进行多线程编程,可以有多种选择,可以使用WindowsAPI,如果你在使用GTK,也可以使用GTK实现了的线程库,如果你想让你的程序有更多的移植性你最好是选择POSIX中的Pthread函数库,我 ...

  8. erlang单独配置文件

    一种是erl启动的时候加参数 doudizhu.config [ {doudizhu,[ {listen_port, }, {node_caller_prefix,"ruby"}, ...

  9. emacs配置buffer的快捷键

    emacsConfig/buffer-setting.el (global-set-key (kbd "<M-left>") 'previous-buffer) (gl ...

  10. usbip install

    # README for usbip-utils## Copyright (C) 2011 matt mooney <mfm@muteddisk.com>#               2 ...