ServletContext

本文详细介绍了在Java Web开发中ServletContext对象的应用方法,包括如何通过ServletContext对象进行数据共享、获取初始化参数及资源路径等。

一个web应用中的所有Servlet共享同一个ServletContext对象,通过该对象进行数据共享

ServletContext对象通常被称为域对象

this.getServletContext().setAttribute(String namne, Object object);//设置name属性的值

this.getServletContext().getAttribute(String name);//取出name属性的值

this.getServletContext().getInitParameter(String name);//获取web.xml中标签对<context-param>中配置的name参数的值

this.getServletContext().getInitParameterNames();//获取web.xml中标签对<context-param>中配置的所有参数名称

this.getServletContext().getRequestDispatcher("1.jsp").forward(httpServletRequest, httpServletResponse);//内部请求转发

this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");//将资源以流形式返回, 返回InputStream

this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");//返回资源的绝对路径




转载于:https://my.oschina.net/lhplj/blog/385735

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值