serVletContext对象

来源:互联网 发布:手机怎么恢复数据 编辑:程序博客网 时间:2024/04/29 17:39
lWEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
l
lServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
l
l由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context对象
l
l查看ServletContext API文档,了解ServletContext对象的功能
l多个Servlet通过ServletContext对象实现数据共享。
l
l获取WEB应用的初始化参数。
l实现Servlet的转发。
l
l利用ServletContext对象读取资源文件。
得到文件路径
读取资源文件的三种方式
.properties文件(属性文件)
l
package cn.itcast;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.Properties;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/* servletContext域:1,是一个容器 2。作用范围是应用程序范围 */public class ServletDemo11 extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//test5();File file=new File("");response.getOutputStream().write(file.getAbsolutePath().getBytes());}private void test5() throws IOException {//得到绝对路径String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");String filename=path.substring(path.lastIndexOf("\\"));FileInputStream in2=new FileInputStream(path);Properties props=new Properties();props.load(in2);}private void test4() throws IOException {//in2的相对路径是tomcat的bin目录,所以这种方法要在该目录下建立文件夹classes,并把//文件放在这里//类加载容器里,读取properties信息FileInputStream in2=new FileInputStream("classes/db.properties");Properties props=new Properties();props.load(in2);System.out.println(props.getProperty("url"));}private void test3() throws IOException {InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");Properties props=new Properties();props.load(in);System.out.println(props.getProperty("url"));}}

sservlet读取properties文件:
package cn.itcast.dao;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.Properties;//如果读取资源文件的程序不是servlet的话,//就只能通过类转载器去读了,文件不能太大//用传递参数方法不好,耦合性高public class UserDao {private static Properties dbconfig=new Properties();static {InputStream in=UserDao.class.getClassLoader().getResourceAsStream("db.properties");try {dbconfig.load(in);} catch (IOException e) {throw new ExceptionInInitializerError(e);}//上面代码类装载器只装载一次,下面代码用类装载方式得到文件位置URL url=UserDao.class.getClassLoader().getResource("db.properties");String str=url.getPath();//file:/C:/apache-tomcat-7.0.22/webapps/day05/WEB-INF/classes/db.propertiestry {InputStream in2=new FileInputStream(str);try {dbconfig.load(in2);} catch (IOException e) {throw new ExceptionInInitializerError(e);}} catch (FileNotFoundException e1) {throw new ExceptionInInitializerError(e1);}}public void update() {System.out.println(dbconfig.get("url"));}}



0 0
原创粉丝点击