【04 ServletConfig&ServletContext 学习】

一、ServletConfig

在这里插入图片描述

1. ServletConfig配置方式

在这里插入图片描述

2. ServletConfig常用方法

在这里插入图片描述


import javax.servlet.ServletConfig;
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;
import java.util.Enumeration;

/**
 * ServletConfig 演示
 */

public class TestServlet extends HttpServlet {

    //1.声明ServletConfig
    private  ServletConfig config;
    //2.通过init方法,对ServletConfig对象赋值
    @Override
    public void init(ServletConfig config) throws ServletException {
        this.config=config;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //3.演示ServletConfig常用方法
        //根究key获取相应的value值
        String encodingValue = config.getInitParameter("encoding");
        System.out.println("encoding对应的value值:"+encodingValue);

        //获取所有的key
        Enumeration<String> keys = config.getInitParameterNames();
        while (keys.hasMoreElements()){
            //获取每一个key
            String key = keys.nextElement();
            //根究key获取value
            String value = config.getInitParameter(key);
            System.out.println(key+":"+value);
        }
        //获取Servlet名称
        String servletName = config.getServletName();
        System.out.println("servletName:" + servletName);
        //获取ServletContext对象
        ServletContext servletContext = config.getServletContext();
        System.out.println("servletContext:"+servletContext);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

在这里插入图片描述

二、ServletContext

在这里插入图片描述
什么是域对象:
在这里插入图片描述

1. ServletContext的配置方式

在这里插入图片描述
在这里插入图片描述

2. ServletContext常用方法

在这里插入图片描述


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;

/**
 * ServletContext 演示
 */

public class ServletContextDemo extends HttpServlet {



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取ServletContext对象
        ServletContext context = getServletContext();
        //2.常用方法演示
        //获取全局配置参数 getInitParameter(String key) 根据key获取value

        String value = context.getInitParameter("globalDesc");
        System.out.println("globalDesc:"+value);
        //获取应用虚拟目录 context.getContextPath()
        String contextPath = context.getContextPath();
        System.out.println("contextPath:"+contextPath);

        //根据虚拟目录获取绝对路径
        String realPath = context.getRealPath("/");
        System.out.println("realPath:"+realPath);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

在这里插入图片描述
这里的/demo来源于项目部署配置的地方:
在这里插入图片描述


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;

/**
 * ServletContext 演示
 */

public class ServletContextDemo extends HttpServlet {



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取ServletContext对象
        ServletContext context = getServletContext();
        //2.常用方法演示
        //获取全局配置参数 getInitParameter(String key) 根据key获取value

        String value = context.getInitParameter("globalDesc");
        System.out.println("globalDesc:"+value);
        //获取应用虚拟目录 context.getContextPath()
        String contextPath = context.getContextPath();
        System.out.println("contextPath:"+contextPath);

        //根据虚拟目录获取绝对路径
        String realPath = context.getRealPath("/");
        System.out.println("realPath:"+realPath);

        String a = context.getRealPath("/WEB-INF/classes/a.txt");
        System.out.println("a.txt:"+a);
        String b = context.getRealPath("/b.txt");
        System.out.println("b.txt:"+b);
        String c = context.getRealPath("/WEB-INF/c.txt");
        System.out.println("c.txt:"+c);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

在这里插入图片描述

2. ServletContext常用方法(数据共享)

在这里插入图片描述
在不同的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;

/**
 * ServletContext 演示
 */

public class ServletContextDemo extends HttpServlet {



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取ServletContext对象
        ServletContext context = getServletContext();
        //2.常用方法演示
        //获取全局配置参数 getInitParameter(String key) 根据key获取value

        String value = context.getInitParameter("globalDesc");
        System.out.println("globalDesc:"+value);
        //获取应用虚拟目录 context.getContextPath()
        String contextPath = context.getContextPath();
        System.out.println("contextPath:"+contextPath);

        //根据虚拟目录获取绝对路径
        String realPath = context.getRealPath("/");
        System.out.println("realPath:"+realPath);

        //设置共享数据
        context.setAttribute("username","zhangsan");

        //删除共享数据
        context.removeAttribute("username");
        //删除后再另一个Servlet中就获取不到了

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱喝阔落的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值