java_web之ServletContext应用

本文详细介绍了Servlet容器在启动时如何加载Web应用及创建ServletContext对象,解释了ServletContext对象的作用,包括存放共享数据的方法以及如何通过ServletConfig获取ServletContext对象。文章还探讨了ServletContext对象在实现Servlet之间的数据共享和转发请求资源的应用场景,并提供了实例代码演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的 对应的ServletContext对象. 它代表当前 web 应用,可以把ServletContext看成是一个Web应用的服务器端组件的共享内存.在ServletContext中可以存放共享数据,它提供了4个读取或设置共享数据的方法.其方法有:

  • setAttribute(String name,Objectobject):把一个对象和一个属性名绑定,将这个对象存储在ServletContext中
  • getAttribute(string name):根据给定的属性名返回所绑定的对象
  • removeAttribute(Stringname):根据给定的属性名从ServletContext中删除相应的属性
  • getattributeNames():返回一个Enumeration对象,它包含了存储在Servlet对象中的所有属性名

ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。

   也可以使用 this.getServletContext方法

由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。多个Servlet通过ServletContext对象实现数据共享。

ServletContext对象通常也被称之为context域对象。(requestsessionpage

  setAttribute(),getAttribute();

相关举例:


实现Servlet的转发。

 RequestDispatcher rd = getServletContext().getRequestDispatcher(“/1.jsp”);

rd.forward(request,response);

如何把数据传到 1.jsp ?(可以通过request域,不能用context域)

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

/* 返回一个RequestDispatcher对象,作为包装器为资源位于给定的路径。

 一个RequestDispatcher对象可用于转发请求资源或包括资源在一个响应。资源可以是动态的还是静态的。 

路径名必须开始以一个“/”和被解释为相对于当前上下文根。使用getContext获取对资源RequestDispatcher在外国上下文。

  该方法返回null如果ServletContext RequestDispatcher不能返回。*/

this.getServletContext().setAttribute("today","Yestoday");

RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/index.jsp");

/* 从一个servlet将请求转发到另一个资源(servlet、JSP文件,或HTML文件)在服务器。

 这种方法允许一个servlet来做初步的请求处理和另一个资源生成响应。*/

         rd.forward(request, response);

}

    public void doGet(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
    //获取ServletContext的两种方式  
    //第一种  
             ServletContext sc=this.getServletConfig().getServletContext();  
             //第二种  
            ServletContext sc2=this.getServletContext();  
              
    }  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
    //多个Servlet实现数据共享  
       String data="abcd";  
               this.getServletContext().setAttribute("lenovo", data);  
    }  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
    ////配合Context2使用,通过属性值lenovo调出data的值;  
           String value = (String)this.getServletContext().getAttribute("lenovo");  
           System.out.println(value);  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
         
    //在web.xml文件中查找data属性相应的值;    System.out.println(this.getServletContext().getInitParameter("data"));  
    }  

ServletContext应用

获取WEB应用的初始化参数。

web.xml文件中插入;

<context-param>

    <param-name> data</param-name>

   <param-value> xxxx</param-value>

</context-param>

ServletConfigServletContext的区别

整个Web应用只有一个ServletContext,在部署Web应用的时候,容器会建立这一个ServletContext对象,这个上下文对Web应用中的每个ServletJSP都可用。 

Web应用中的各个Servlet都有自己的ServletConfig,它只对当前Servlet有效。

还有一个是通过一般的java类读取配置文件的内容:

利用ServletContext对象读取资源文件。

得到文件路径

读取资源文件的三种方式

.properties文件(属性文件)

这个首先必须写配置文件:

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/test

username=root

password=root

public void test1() throws IOException {

InputStream in= this.getServletContext().getResourceAsStream(

"/WEB-INF/classes/db.properties");

Properties prop = new Properties();

prop.load(in);

String driver = prop.getProperty("driver");

String url = prop.getProperty("url");

String username = prop.getProperty("username");

String password = prop.getProperty("password");

System.out.println(url);

}

// 通过ServletContext读取配置文件 方法2,获取了文件的绝对路径

public void test2() throws IOException {

String path = this.getServletContext().getRealPath(

"/WEB-INF/classes/db.properties");

int i = path.lastIndexOf("\\");

System.out.println(path.substring(i + 1));

FileInputStream fis = new FileInputStream(path);

Properties prop = new Properties();

prop.load(fis);

String driver = prop.getProperty("driver");

System.out.println(driver);

}

也可以再写一个工具类:

InputStream in = StudentDao.class.getClassLoader().getResourceAsStream("db.properties");

Properties prop = new Properties();

try {

prop.load(in);

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 driver = prop.getProperty("driver");

String url = prop.getProperty("url");

String username = prop.getProperty("username");

String password = prop.getProperty("password");

然后再写一个dao类,提供增删改查;


转载自:http://blog.youkuaiyun.com/liuxiaogangqq/article/details/8099068

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值