JavaWeb学习笔记(7)——ServletConfig和ServletContext对象

时间:2022-01-01 13:03:33
/*
ServletConfig对象
 ·在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。
 ·Servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,
 并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
ServletContext对象
一、原理简介:
 ·WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
 ·ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
 ·由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。

二、应用:
1、多个Servlet通过ServletContext对象实现数据共享。
2、获取WEB应用的初始化参数。 3、实现Servlet的转发。 4、利用ServletContext对象读取资源文件。
 ·得到文件路径
 ·读取资源文件的三种方式
 ·properties文件(属性文件)
*/
//多个servlet通过servletContext实现数据共享
public class ServletContextDemo1 extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  String data = "abcddffdf";
  
  ServletContext context = this.getServletConfig().getServletContext();
  context.setAttribute("data", data);  //map
  
 }  public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }
}
public class ServletContextDemo2 extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  ServletContext context = this.getServletContext();
  String data = (String) context.getAttribute("data");
  System.out.println(data);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }
} //获取整个web站点的初始化参数
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  ServletContext context = this.getServletContext();
  String url = context.getInitParameter("url");
  System.out.println(url);
 }
/*
用servletContext实现请求转发:mvc
注意:
 ·转发之前的所有写入都无效
 ·转发之前,response不能提交,否则转发的时候服务器会抛:Cannot forward after response has been committed
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  response.getOutputStream().write("111".getBytes());
  ServletContext context = this.getServletContext();
  RequestDispatcher rd = context.getRequestDispatcher("/servlet/ServletDemo2");
  rd.forward(request, response);
  
  response.getOutputStream().write("222".getBytes());
 }
 
//读取资源文件的三种方式
 
//使用servletContext读取资源文件
public class ServletContextDemo1 extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  /*
    做web工程时,不建议采用传统方式读取文件数据
   FileInputStream in = new FileInputStream("db.properties");
   System.out.println(in);
   */
  test1(response);
 }
 private void test1(HttpServletResponse response) throws IOException {   // 获取web资源的绝对路径后,再用传统方式得到该资源的文件流
  String path = this.getServletContext().getRealPath(
    "/WEB-INF/classes/db.properties");
  FileInputStream in = new FileInputStream(path);
  Properties prop = new Properties();
  prop.load(in);
  // 该方法的好处是可以得到资源的名称
  String filename = path.substring(path.lastIndexOf("\\")+1);
  String password = prop.getProperty("password");
  System.out.println(filename + " " + password);
 }
 private void test2(HttpServletResponse response) throws IOException {   // 直接得到该资源文件以流的形式返回
  InputStream in = this.getServletContext().getResourceAsStream(
    "/WEB-INF/classes/db.properties");
  Properties prop = new Properties();
  prop.load(in);
  String password = prop.getProperty("password");
  System.out.println(password);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }
}
//用类装载器读取资源文件
public class ServletContextDemo2 extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  //获取到装载当前类的类装载器
  ClassLoader loader = ServletContextDemo2.class.getClassLoader();
  //读取指定目录下的文件资源
  //通过类装载器读取资源文件的注意事项:不适合装载大文件,否则会导致jvm内存溢出
  InputStream in = loader.getResourceAsStream("he/junhua/Servlet/db.properties");
  Properties prop = new Properties();
  prop.load(in);
  
  System.out.println(prop.getProperty("driver"));
  
  
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }
} //在Servlet外读取资源文件
public class ServletContextDemo3 extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  Dao dao = new Dao();
  dao.run();
  
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }
}
public class Dao {
 public void run() throws IOException {   URL url = Dao.class.getClassLoader().getResource("db2.properties");
  String path = url.getPath();
  
  FileInputStream in = new FileInputStream(path);
  
  Properties prop = new Properties();
  prop.load(in);
  System.out.println(prop.size());
  System.out.println(prop.getProperty("username"));
  prop.setProperty("username", "xxx");
  FileOutputStream out = new FileOutputStream(path);
  prop.store(out, "");     System.out.println(prop.getProperty("username"));
  
  out.close();
  in.close();
 }
}

//一个简单的应用:读取PranavMistry_2009I_480.mp4,并拷贝到e:\根目录下
public class ServletContextDemo5 extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  ServletContext context =  this.getServletContext();
  
  //得到文件名
  String path = context.getRealPath("/1.jpg");
  String filename = path.substring(path.lastIndexOf("\\")+1);
  
  //得到资源文件读的流,并写入指定目录
  InputStream in = context.getResourceAsStream("/1.jpg");
  byte buffer[] = new byte[1024];
  int len = 0;
  
  FileOutputStream out = new FileOutputStream("E:\\temp\\"+filename);
  while((len=in.read(buffer))>0){
   out.write(buffer,0,len);
  }
  
  out.close();
  in.close();
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }
}