java_web之ServletContext应用

WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。

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类,提供增删改查;

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值