通过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. 从客户端中检测到有潜在危险的 Request.Form 值 方法

    由于在.net中,Request时出现有HTML或Javascript等字符串时,系统会认为是危险性值.立马报出“从客户端 中检测到有潜在危险的Request.Form值”这样的错. 解决方案一:   ...

  2. halcon之共线连接union_collinear_contours_xld

    union_collinear_contours_xld 很多时候当我们用edges_sub_pix, threshold_sub_pix  等算子得到边缘后,因为有噪声.物体本身断裂等原因 很多边缘 ...

  3. 互评Alpha版本

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2323] 队名:二次元梦之队 组长:刘莹莹 组员:周昊 潘世维  王玉潘 赵美增 ...

  4. openGL之着色器程序的使用

    #define GLEW_STATIC #include <GL\glew.h> #include <GLFW\glfw3.h> #include<iostream> ...

  5. 各开源 bbs 程序比较

    主要是集中在 php 开源轻巧的程序. 搜索到一个逼乎的一个帖子:https://www.zhihu.com/question/20655704 ,顺藤摸瓜 下. carbon forum 第一个测试 ...

  6. 使用jquery获取url以及jquery获取url参数的方法(转)

    使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下 1.window.location.href; 其实只是用到了javas ...

  7. (简单)冒泡和直接选择排序同时调用swap算法

    void swap(int &a , int &b) { int temp; temp = a; a=b; b=temp; } void bubble(int a[],int n) { ...

  8. nginx中使用 md5

    core/ngx_md5.h 头文件 /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #ifndef _NGX_MD5_H_ ...

  9. System.IO.Path类

    System.IO.Path为路径的操作封装了很多很有的东西,利用该类提供的方法能够快速处理路径操作的问题.下面详细了解一下. 1.属性 属性太复杂了,反映什么系统平台的信息,看不懂,等以后看得懂了再 ...

  10. 【python】FTP客户端

    Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,函数列举如下 ftp登陆连接 from ftplib import FTP #加 ...