package cn.itcast;

 import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //读取资源文件
public class ServletDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { test2();
} //通过servletContext的getReadlPath得到资源的绝对路径后,再通过传统流读取资源文件
public void test2() throws IOException { String path = this.getServletContext().getRealPath("/WEB-INF/classes/cn/itcast/db.properties");
System.out.println(path);
String filename = path.substring(path.lastIndexOf("\\")+1);
System.out.println("当前读取到资源名称是:"+filename); FileInputStream in = new FileInputStream(path); Properties props = new Properties();
props.load(in); String url = props.getProperty("url");
String username = props.getProperty("username");
String password = props.getProperty("password"); System.out.println(url+username+password);
} public void test1() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/db.properties"); Properties props = new Properties();
props.load(in); String url = props.getProperty("url");
String username = props.getProperty("username");
String password = props.getProperty("password"); System.out.println(url+username+password);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }
 url=jdbc:mysql://localhost:3306/test
username=root
password=root

ServletContext读取Web应用中的资源文件的更多相关文章

  1. 读取web工程中.properties资源文件的模板代码

    读取web工程中.properties资源文件的模板代码 // 读取web工程中.properties资源文件的模板代码 private void test2() throws IOException ...

  2. 读取web应用下的资源文件(例如properties)

    package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...

  3. Jar中的Java程序如何读取Jar包中的资源文件

    Jar中的Java程序如何读取Jar包中的资源文件 比如项目的组织结构如下(以idea中的项目为例): |-ProjectName |-.idea/  //这个目录是idea中项目的属性文件夹 |-s ...

  4. 【初码干货】使用阿里云对Web开发中的资源文件进行CDN加速的深入研究和实践

    提示:阅读本文需提前了解的相关知识 1.阿里云(https://www.aliyun.com) 2.阿里云CDN(https://www.aliyun.com/product/cdn) 3.阿里云OS ...

  5. java web项目中打开资源文件中文乱码

    1 java web项目中经常使用多模块管理.在某一个模块中添加了一些资源文件.但不是启动项目.有时候需要在程序中读取资源文件内容,打包后放到容器中就不能正常运行了.需要将所有资源文件放到启动项目的 ...

  6. 读取Jar包中的资源问题探究

    最近在写一个可执行jar的程序,程序中包含了2个资源包,一个是images,一个是files.问题来了,在Eclipse里开发的时候,当用File类来获取files下面的文件时,没有任何问题.但是当程 ...

  7. java基础知识3--如何获取资源文件(Java中获取资源文件的url)

    java开发中,常见的resource文件有:.xml,.properties,.txt文件等,后台开发中经常用到读取资源文件,处理业务逻辑,然后返回结果. 获取资源文件的方法说明getResourc ...

  8. springboot jar包运行中获取资源文件

    1. 今天晚上写了一个程序,基于Spring boot的一个小网站,发现使用FileUtils.class.getResource(path)来获取jar包中的资源文件并不能成功,其路径很奇怪 fil ...

  9. Android开发---如何操作资源目录中的资源文件4 ---访问xml的配置资源文件的内容

    Android开发---如何操作资源目录中的资源文件4 XML,位于res/xml/,这些静态的XML文件用于保存程序的数据和结构. XmlPullParser可以用于解释xml文件 效果图: 描述: ...

随机推荐

  1. css3 总结02

    盒子尺寸 .border-box{ -webkit-box-sizing: border-box; -moz-box-sizing: border-box; border-sizing: border ...

  2. ol新属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. footer元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. WPF在DLL中读取Resource的方法

    WPF是个用户控件,被WinForm调用.而WinForm是在一个DLL类库中被调用.试了很多方法,都无法将Resource中的图读进程序.用下面的方法总算实现了.   /根据图片的名称,从资源中找到 ...

  5. Python: 常用list, string处理功能

    #1. keep strings in double quote as one word when split string to words #e.g. str = ‘a b "is si ...

  6. BZOJ4379 : [POI2015]Modernizacja autostrady

    两遍树形DP求出每个点开始往上往下走的前3长路以及每个点上下部分的直径. 枚举每条边断开,设两边直径分别为$A,B$,则: 对于第一问,连接两边直径的中点可得直径为$\max(A,B,\lfloor\ ...

  7. [转]OpenVPN 安装与配置

    一.服务器端安装及配置 服务器环境:干净的CentOS6.3 64位系统 内网IP:10.143.80.116 外网IP:203.195.xxx.xxx OpenVPN版本:OpenVPN 2.3.2 ...

  8. Document 按照xml格式输出

    private void GetXMLDocument(Document doc) { OutputFormat format1 = new OutputFormat(" ", t ...

  9. POJ 3276 (开关问题)

    题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...

  10. ACM 疯牛

    疯牛 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...