WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象通讯。ServletContext对象通常也被称之为Context域对象。
查看ServletContext API文档,了解ServletContext对象的功能。
ServletContext应用
多个Servlet通过ServletContext对象实现数据共享
1 package cn.itcast.context; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletContext; 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 * Servlet implementation class ContextDemo1 14 * 多个ServletContext实现数据共享 15 */ 16 17 public class ContextDemo1 extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 21 /** 22 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 23 */ 24 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 String data = "abcdefd"; 26 ServletContext context = this.getServletConfig().getServletContext(); 27 context.setAttribute("data", data); 28 } 29 30 /** 31 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 // TODO Auto-generated method stub 35 doGet(request, response); 36 } 37 38 }
1 package cn.itcast.context; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletContext; 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 * Servlet implementation class ContextDemo2 14 */ 15 16 public class ContextDemo2 extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 19 20 21 /** 22 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 23 */ 24 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 ServletContext context = this.getServletContext(); 26 String data = (String) context.getAttribute("data"); 27 System.out.println(data); 28 } 29 30 /** 31 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 // TODO Auto-generated method stub 35 doGet(request, response); 36 } 37 38 }
可以在web.xml文件中插入<context-param>
1 <context-param> 2 <param-name>url</param-name> 3 <param-value>yyy</param-value> 4 </context-param>
获取WEB应用的初始化参数
实现Servlet的转发
利用ServletContext对象读取资源文件
得到文件路径
读取资源文件的三种方式
.properties文件(属性文件)
通过类装载器读取资源文件的注意事项:不适合装在大文件,否则会导师java文件内存溢出
如果需要装载大文件怎么做?
1 //读取pranavMistry.mp4文件,并拷贝到e:根目录下 2 //文件比较大,只能用servletContext 3 public void test4() throws IOException{ 4 //获取文件名 5 String path = this.getServletContext().getRealPath("/WEB-INF/classes/pranavMistry.mp4"); 6 String filename = path.substring(path.lastIndexOf("\"+1)); 7 8 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes//pranavMistry.mp4"); 9 byte buffer[] = new byte[1024]; 10 int len = 0; 11 12 FileOutputStream out = new FileOutputStream("e:\" + filename); 13 while((len=in.read(buffer))>0){ 14 out.write(buffer, 0, len); 15 } 16 out.close(); 17 in.close(); 18 }
Servlet缓存技术
String data = "abcdefg"; response.setDateHeader("expires", System.currentTimeMillis()+ 24*3600*1000); response.getOutpotStream().write(data.getBytes());
这样可以减轻服务器的压力,提高服务器性能。
1 package cn.itcast.context; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletContext; 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 *获取整个web站点的初始化参数 14 * Servlet implementation class ContextDemo3 15 */ 16 17 public class ContextDemo3 extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 21 /** 22 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 23 */ 24 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 ServletContext context = this.getServletContext(); 26 String url = context.getInitParameter("url"); 27 System.out.println(url); 28 } 29 30 /** 31 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 // TODO Auto-generated method stub 35 doGet(request, response); 36 } 37 38 }
1 package cn.itcast.context; 2 3 import java.io.IOException; 4 5 import javax.servlet.RequestDispatcher; 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 /**用servletContext实现请求转发:mvc 14 * 注意: 15 * 1.转发之前的所有写入无效 16 * 2.转发之前response不能提交,否则转发的时候服务器会抛出异常。Cannot forward after response has been committed 17 * 18 * 19 * Servlet implementation class ContextDemo4 20 */ 21 22 public class ContextDemo4 extends HttpServlet { 23 private static final long serialVersionUID = 1L; 24 25 26 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 27 ServletContext context = this.getServletContext(); 28 RequestDispatcher rd = context.getRequestDispatcher("/ContextDemo5"); 29 rd.forward(request, response); 30 } 31 32 /** 33 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 34 */ 35 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 // TODO Auto-generated method stub 37 doGet(request, response); 38 } 39 40 }
1 package cn.itcast.context; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 /** 11 * Servlet implementation class ContextDemo5 12 */ 13 14 public class ContextDemo5 extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 /** 18 * @see HttpServlet#HttpServlet() 19 */ 20 public ContextDemo5() { 21 super(); 22 // TODO Auto-generated constructor stub 23 } 24 25 /** 26 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 27 */ 28 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 29 response.getOutputStream().write("servletdemo5".getBytes()); 30 } 31 32 /** 33 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 34 */ 35 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 // TODO Auto-generated method stub 37 doGet(request, response); 38 } 39 40 }
1 package cn.itcast.context; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.util.Properties; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.annotation.WebServlet; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 /** 15 * Servlet implementation class ContextDemo6 16 */ 17 18 public class ContextDemo6 extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 21 /** 22 * @see HttpServlet#HttpServlet() 23 */ 24 public ContextDemo6() { 25 super(); 26 // TODO Auto-generated constructor stub 27 } 28 29 /** 30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 31 */ 32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 //在做web工程的时候,不建议采用传统方式读取文件数据。 34 //FileInputStream in = new FileInputStream("src/db.properties"); 35 //System.out.println(in); 36 37 //使用Context读取文件 38 //读取web工程中资源文件的模板代码 39 //InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); 40 //Properties prop = new Properties(); 41 //prop.load(in); 42 //String driver = prop.getProperty("driver"); 43 //String url = prop.getProperty("url"); 44 //String username = prop.getProperty("username"); 45 //String password = prop.getProperty("password"); 46 //System.out.println(driver); 47 48 49 //获得web资源的绝对路径 50 String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties"); 51 FileInputStream in = new FileInputStream(path); 52 Properties prop = new Properties(); 53 prop.load(in); 54 String driver = prop.getProperty("driver"); 55 String url = prop.getProperty("url"); 56 String username = prop.getProperty("username"); 57 String password = prop.getProperty("password"); 58 System.out.println(driver); 59 } 60 61 /** 62 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 63 */ 64 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 65 // TODO Auto-generated method stub 66 doGet(request, response); 67 } 68 69 }
1 package cn.itcast.context; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.util.Properties; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 /** 16 * Servlet implementation class ContextDemo7 17 */ 18 //用类装载器读取资源文件 19 public class ContextDemo7 extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public ContextDemo7() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 ClassLoader loader = ContextDemo7.class.getClassLoader(); 35 InputStream in = loader.getResourceAsStream("db.properties"); 36 Properties prop = new Properties(); 37 prop.load(in); 38 String driver = prop.getProperty("driver"); 39 String url = prop.getProperty("url"); 40 String username = prop.getProperty("username"); 41 String password = prop.getProperty("password"); 42 System.out.println(driver); 43 } 44 45 //读取pranavMistry.mp4文件,并拷贝到e:根目录下 46 //文件比较大,只能用servletContext 47 public void test4() throws IOException{ 48 //获取文件名 49 String path = this.getServletContext().getRealPath("/WEB-INF/classes/pranavMistry.mp4"); 50 String filename = path.substring(path.lastIndexOf("\"+1)); 51 52 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes//pranavMistry.mp4"); 53 byte buffer[] = new byte[1024]; 54 int len = 0; 55 56 FileOutputStream out = new FileOutputStream("e:\" + filename); 57 while((len=in.read(buffer))>0){ 58 out.write(buffer, 0, len); 59 } 60 out.close(); 61 in.close(); 62 } 63 64 /** 65 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 66 */ 67 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 68 // TODO Auto-generated method stub 69 doGet(request, response); 70 } 71 72 }