1、ServletContext的概述
- 一个项目只有一个ServletContext对象!application
- 我们可以在N多个Servlet中获取这个唯一的对象,使用它来给多个Servlet传递数据!
- 这个对象在Toncat启动时就创建,在Toncat关闭时才死去!
2、获取ServletContext对象
- ServletConfig ### getSetvletContext()
- GenericServlet ## getServletContext()
- HttpServlet ## getServletContext()
- ServletContextEvent ## getServletContext()
1 public abstract class MyServlet implements Servlet { 2 3 public void init(ServletConfig config) { 4 ServletContext context = config.getServletContext(); 5 } 6 7 }
1 public class MyServlet02 extends HttpServlet{ 2 3 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 4 throws ServletException, IOException { 5 ServletContext context = this.getServletContext(); 6 } 7 8 }
3、 域对象的功能
-
void setAttribute(String name,Object object) 用来储存一个对象,也可以说用来储存一个属性,例如,servletContext.setAttribute("xxx",XXX); 在ServletContext就保存了一个域属性,域属性名为xxx,域属性值为XXX。与Map相同。
-
ObjectgetAttribute(String name) 通过一个域属性名取得域属性值,如String value=(String)servletContext.getAttribute("xxx");
-
EnumerationgetAttributeNames() 取得所有域属性名,是所有,不是一个
- void removeAttribute(String name) 用来移除指定name的域属性值,如果域属性值name不存在 ,则该方法什么也不做
下面举个例子来说明第一第二种功能
1 public class AServlet extends HttpServlet { 2 3 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 4 throws ServletException, IOException { 5 ServletContext application = this.getServletContext(); 6 application.setAttribute("name", "123"); 7 } 8 }
public class BServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext application=this.getServletContext(); String value=(String) application.getAttribute("name"); System.out.println("通过域属性名取得域属性值 :"+value); } }
启动服务器后,执行AServlet,再执行BServlet 后可以看到如下结果
4、 获取应用初始化参数
- Servlet 也可以获取初始化参数,但它是局部的参数;也就是说,一个Servlet只能获取自己的初始化参数,不能获取别人的初始化参数,即初始化参数只为一个Servlet准备
- 也可以配置公共的初始化参数,为所有的Srevlet 而用,这需要使用ServletContext才能使用!
如配置公共的初始化参数,首先要先配置web.xml文件
再来新建一个CServlet.java
package org.lxh.serletdemo; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; public class CServlet extends HttpServlet { public void init() throws ServletException { ServletContext application =this.getServletContext(); String value=(String)application.getInitParameter("context-param"); System.out.println(value); } }
启动服务器后,再执行CServlet可以看到如下结果
5、 获取相关资源方法
- 获取真实路径(******)
- 获取资源流
- 获取指定目录下的所有资源路径
1、可以使用ServletContext 对象来获取Web应用下的资源,例如在web目录下有个input.html文件,现在想在servlet中获取这个资源,就可以通过ServletContext来获取
1 package org.lxh.serletdemo; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class DServlet extends HttpServlet { 13 public void doGet(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 ServletContext application=this.getServletContext(); //获取ServletContext对象 16 String realPath=application.getRealPath("input.html"); //通过getRealPath得到真实路径 17 System.out.println(realPath); //打印路径 18 } 19 }
2. 获取资源流
1 package org.lxh.serletdemo; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.PrintWriter; 6 import java.util.Set; 7 8 import javax.servlet.ServletContext; 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 public class DServlet extends HttpServlet { 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 ServletContext application=this.getServletContext(); 18 InputStream input =application.getResourceAsStream("input/jsp");//获取资源路径后,再创建出输入流对象 19 20 } 21 }
3. 获取指定目录下的所有资源路径,如输出WEB-INF下 的所有路径
1 package org.lxh.serletdemo; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.PrintWriter; 6 import java.util.Set; 7 8 import javax.servlet.ServletContext; 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 public class DServlet extends HttpServlet { 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 ServletContext application=this.getServletContext(); //获取ServletContext对象 18 Set<String > paths=application.getResourcePaths("/WEB-INF"); 19 System.out.println(paths); 20 } 21 }
时间: 2024-11-03 03:43:22