【JSP开发】用ServletContext读取资源文件
2015-01-25 18:06
323 查看
两种读取资源文件的方法。
db.properties文件中放了三个参数,分别是url、username和password信息。
测试代码:
package cn.edu;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//用ServletContext读取资源文件的方法
public class ServletDemo9 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream in =this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
//模板代码(采用传统FileInputStream直接读不好(相对路径),用ServletContext)
Properties props = new Properties();//map
props.load(in);
String url=props.getProperty("url");
String username=props.getProperty("username");
String password=props.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
System.out.println("方法二:");
text2();
}
//通过ServletContext()的getRealPath得到资源的绝对路径后,再通过传统流读取资源文件
//采用ServletContext()利用传统FileInputStream方法读资源文件
//这样得到的路径是真实路径(绝对路径),而不是上面说的相对路径
public void text2() throws IOException{
//这个方法可以拿到资源的名称
String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
FileInputStream in =new FileInputStream(path);
String filename=path.substring(path.lastIndexOf("\\")+1);
System.out.println("资源文件名称为:"+filename);
//System.out.println(path);
//结果D:\apache-tomcat-6.0.24\webapps\day05\WEB-INF\classes\db.properties
Properties props = new Properties();//map
props.load(in);
String url=props.getProperty("url");
String username=props.getProperty("username");
String password=props.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
db.properties文件中放了三个参数,分别是url、username和password信息。
测试代码:
package cn.edu;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//用ServletContext读取资源文件的方法
public class ServletDemo9 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream in =this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
//模板代码(采用传统FileInputStream直接读不好(相对路径),用ServletContext)
Properties props = new Properties();//map
props.load(in);
String url=props.getProperty("url");
String username=props.getProperty("username");
String password=props.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
System.out.println("方法二:");
text2();
}
//通过ServletContext()的getRealPath得到资源的绝对路径后,再通过传统流读取资源文件
//采用ServletContext()利用传统FileInputStream方法读资源文件
//这样得到的路径是真实路径(绝对路径),而不是上面说的相对路径
public void text2() throws IOException{
//这个方法可以拿到资源的名称
String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
FileInputStream in =new FileInputStream(path);
String filename=path.substring(path.lastIndexOf("\\")+1);
System.out.println("资源文件名称为:"+filename);
//System.out.println(path);
//结果D:\apache-tomcat-6.0.24\webapps\day05\WEB-INF\classes\db.properties
Properties props = new Properties();//map
props.load(in);
String url=props.getProperty("url");
String username=props.getProperty("username");
String password=props.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
相关文章推荐
- ServletContext笔记和servlet和传统方法读取资源文件
- ServletContext对象读取资源文件
- J2EE之ServletContext读取资源文件
- 使用Spring开发web应用,读取外部资源文件问题
- ServletContext读取web应用中的资源文件
- java_web11-ServletContext读取Web应用中的资源文件
- 使用servletContext读取资源文件
- Unity3D开发之不同平台二进制资源文件的读取
- javaWeb开发中读取资源文件方法总结
- Windows Phone 7(WP7)开发 读取本地资源文件
- ServletContext读取Web应用中的资源文件
- web项目中servlet利用servletContext对象读取资源文件
- servletcontext读取web资源文件
- Java web开发中读取资源文件
- Windows Phone 7(WP7)开发 读取本地资源文件
- 【ServletConfig、ServletContext】使用ServletContext、类加载器读取资源文件
- android开发--- jni读取assets资源文件
- (18)servletContext应用:获取web应用的初始化参数、实现servlet转发、利用servletContext对象读取资源文件
- JSP项目从普通类中读取资源文件路径解决方案
- Servlet之ServletContext读取web应用中的资源文件