ServletContext介绍以及用法

一、ServletContext概述

  ServletContext也被称为servlet上下文。WEB容器再启动时,它会为每个WEB应用程序都创建一个ServletContext对象,他代表当前web应用,并且这个对象是全局唯一,多个Servlet都共享这个对象。
功能

  • 多个Servlet通过ServletContext对象实现数据共享
  • 获取WEB 应用的初始化参数
  • 使用ServletContext实现请求转发
  • 使用ServletContext对对象读取资源文件

我们可以使用this.getServletContext()来获得。可以实现数据的共享
ServletContext介绍以及用法_第1张图片从上面的图中可以知道

  • Servlet1和Servlet2之间实现交互,必须通过ServletContext来实现,它们本身之间是不可以实现交互的。
  • Servlet1,Servlet2,Servlet3共享一个ServletContext对象。

二、利用ServletContext对象实现数据共享。

第一步编写两个Servlet类
第一个Servlet

在这里插入代码片package org.westos.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        //通过Servlet设置一个公共属性
        ServletContext servletContext = this.getServletContext();
        servletContext.setAttribute("password","123456");

    }
}

第二个Servlet

package org.westos.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        ServletContext servletContext = this.getServletContext();
        String  password = (String) servletContext.getAttribute("password");
        resp.getWriter().println("密码为"+password);

    }
}

第二步配置web.xml


    <servlet>
        <servlet-name>Servlet1servlet-name>
        <servlet-class>org.westos.servlet.ServletDemo2servlet-class>
    servlet>
    <servlet>
        <servlet-name>Servlet2servlet-name>
        <servlet-class>org.westos.servlet.ServletDemo3servlet-class>
    servlet>
    

    <servlet-mapping>
        <servlet-name>Servlet1servlet-name>
        <url-pattern>/q2url-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>Servlet2servlet-name>
        <url-pattern>/q3url-pattern>
    servlet-mapping>

第三部,先运行ServletDemo,然后再运行ServletDemo2
结果为:
ServletContext介绍以及用法_第2张图片

三、利用ServletContext对象读取网站配置文件。

第一步
首先再src目录下建立一个resources包,里面写入配置文件

driver=com.mysql.jdbc.Driver
username=root
password=123456
url=jdbc:mysql://localhost:3306/school

第二步
编写Servlet类

package org.westos.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取properties配置文件
        //获取配置文件路径
        String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/resources/database.properties");
        Properties properties = new Properties();
        //获取文件输入流
        FileInputStream is= new FileInputStream(realPath);
        //加载文件流
        properties.load(is);
        String driver = properties.getProperty("driver");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        //响应到网页
        resp.getWriter().println(driver);
        resp.getWriter().println(username);
        resp.getWriter().println(password);
        resp.getWriter().println(url);
        System.out.println("控制它输出为:");
        System.out.println(driver);
        System.out.println(username);
        System.out.println(password);
        System.out.println(url);

    }
}

第三步
配置web.xml

<servlet>
        <servlet-name>Servletservlet-name>
        <servlet-class>org.westos.servlet.ServletDemo1servlet-class>
    servlet>
    
    <servlet-mapping>
        <servlet-name>Servletservlet-name>
        <url-pattern>/q1url-pattern>
    servlet-mapping>

第四步
在网页中输入:http://localhost:8080/web1/q1,获得的内容为:
ServletContext介绍以及用法_第3张图片

你可能感兴趣的:(前端)