9 servlet2

  1. servlet生命周期:实例化,初始化,服务,销毁
  2. servlet中的初始化方法有两个:init(),init(config),有参数的init后面也会调用无参init
    1. 其中带参数方法代码如下
      public void init(ServletConfig config){
      this.config = config;
      }
    2. 另一个无参的就是public void init(){}
    3. 如果我们想要在Servlet初始化中完成一下工作,那么我们可以重写init方法,在init方法通过如下步骤去获取初始化设置的的数据
      • 获取config对象:ServletConfig config = getServletConfig();
      • 编写配置文件或者使用注解@webservlet中的initParameter
        • 格式参照下面
        •   @WebInitParam(name="hello",value = "world"),  
            @WebInitParam(name = "uname",value = "jim")  
            })```
          
        • 如果是web中xml文件配置
        •   <servlet-name>AppServlet</servlet-name>  
            <servlet-class>Application</servlet-class>  
            <init-param>  
            <param-name>hello</param-name>  
            <param-value>world</param-value>  
            </init-param>  
          
        AppServlet /demo01 ```
      • 获取初始化参数值:config.getInitParameters(key);
  3. 学习Servlet中的ServletContext和<context-param>
    1. 获取ServletContext,有很多种方法
      1. 在init()中,通过 ServletContext context = config.getServletContext
      2. 在service()中,通过 request.getServletContext
      3. 在session中,通过 session.getServletContext
      4. 以上三个都会获取到同一个ServletContext,[[ServletContext]] 范围更大的session,和application同一个概念
    2. 获取初始化值 context.getInitParamete();
  4. [[context-param与init-param]]这两者之间是有区别的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值