servlet中init-param与context-param的区别「建议收藏」

servlet中init-param与context-param的区别「建议收藏」init-paramweb.xml中的写法<servlet><servlet-name>demo01</servlet-name><servlet-class>com.lanou3g.Demo01</servlet-class><init-param><pa…

大家好,又见面了,我是你们的朋友全栈君。

init-param

web.xml中的写法

<servlet>
    <servlet-name>demo01</servlet-name>
    <servlet-class>com.lanou3g.Demo01</servlet-class>
    <init-param>
        <param-name>username</param-name>
        <param-value>张飞</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>demo01</servlet-name>
    <url-pattern>/demo01</url-pattern>
</servlet-mapping>
init-param配置在servlet标签中,用来初始化当前的Servlet的,属性存放在servletConfig中
因此可以通过获取servletConfig对象来获取servlet中init-param里配置的属性,作用域
限制在当前的Servlet中

获取方式一

1.声明一个ServletConfig当做成员变量
2.重写初始化方法init
  通过该方法的参数 自动获取ServletConfig对象
  ServletConfig对象中保存的是Servlet中的配置信息
public class Demo01 extends HttpServlet{ 
   
    private ServletConfig config;
    @Override
    public void init(ServletConfig config) throws ServletException {    
        this.config = config;
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        传入参数 配置时的 username(相当于key)
        用key获取对应的value
        String value = this.config.getInitParameter("username");
        System.out.println(value);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}

获取方式二–简单粗暴

public class Demo01 extends HttpServlet { 
   
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String value = this.getServletConfig().getInitParameter("username");
        System.out.println(value);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

context-param

web.xml中的写法

<context-param>
    <param-name>username</param-name>
    <param-value>关羽</param-value>
</context-param>
<servlet>
    <servlet-name>demo01</servlet-name>
    <servlet-class>com.lanou3g.Demo01</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>demo01</servlet-name>
    <url-pattern>/demo01</url-pattern>
</servlet-mapping>

获取方式

public class Demo01 extends HttpServlet { 
   
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String string = context.getInitParameter("username");
        System.out.println(string);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

init-param与context-param的区别

区别一

在context-param中不存在这种获取context-param属性的方法
public class Demo01 extends HttpServlet { 
   
    ServletContext config;
    public void init(ServletContext config) throws ServletException {
        // TODO Auto-generated method stub
        config = config;
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(config.getInitParameter("username"));
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
这种获取的方式是错误的,因为系统的内部实现没有init(ServletContext config)这种方法
然而有init(ServletConfig config)这种方法,所有init-param有两种获取方式,而context-param
只有一种获取方式

这里写图片描述

区别二 作用域不同

init-param写在servlet中,web.xml中可以写多个servlet,而每个servlet中都可以设置一个
init-param,即init-param作用域仅对自己的servlet起作用
context-param写在servlet之外,web.xml中只能有一个context-param,作用域属于整个程序的
而不限制于某一个servlet,context-param更多用来交互比如获取form表单中的内容
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160405.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java中 List 和 JSON字符串之间的相互转换

    Java中 List 和 JSON字符串之间的相互转换List转JSON字符串Listlist=newArrayList();Map&lt;String,String&gt;map=newHashMap&lt;&gt;();map.put("name","szy");map.put("num",2);list.add(map);StringinventoryResultStr=JSON.t…

    2022年10月17日
    1
  • Kotlin学习之路(3)控制语句

    Kotlin学习之路(3)控制语句

    2021年3月12日
    152
  • 寄存器与锁存器的区别

    寄存器与锁存器的区别

    2022年3月13日
    32
  • 课程设计—飞机订票系统

    课程设计—飞机订票系统1. 题目 本课程设计的题目为:飞机订票系统。2. 项目描述 基于目前人们外出远行频繁,为方便乘客提前买票及优化飞机航空订票服务,需要开发一个飞机订票系统,此程序就是要实现航班情况的录入,查询,订票,退票以及航班的查询和修改等基本功能。 3. 数据及其逻辑结构分析 (1)航班的信息:航班的情况存储结构采用单链表,每个元素表示一个航班的情况,包括航班号、起飞时间、降落时间、起

    2022年6月15日
    30
  • 常见电子元器件的常用品牌汇总

    ​抽时间汇总了一些常见电子元器件常用品牌,大家在元器件选型时可以参考。电阻:Yageo国巨、Uniohm厚声、Walsin华新科、Fenghua风华、Ralec旺诠、KOA兴亚、Panasonic松下、AVX、Rohm罗姆、Samsung三星、TDK、TMTEC泰铭、Kyocera京瓷、PHYCOM飞元。电容:Yageo国巨、Samsung三星、Eyang宇阳、Murata村田、Taiyo太诱、Fenghua风华、Kyocera京瓷、HEC禾伸堂、Kemet基美、ISND华信安、AVX、TDK、Nich

    2022年4月4日
    81
  • SSRF漏洞原理解析[通俗易懂]

    SSRF漏洞原理解析[通俗易懂]文章目录0x01基础知识1、SSRF漏洞简介:2、主要攻击方式:3、漏洞形成原理:4、漏洞的危害:0x02漏洞检测1、漏洞验证:2、漏洞的可能出现点:0x03绕过方法:1、绕过限制为某种域名:2、绕过限制请求IP不为内网地址:3、限制请求只为http协议:0x04漏洞利用1、产生漏洞的函数:2、漏洞靶场:0x05如何防御SSRF0x01基础知识1、SSRF漏洞简介:SSRF全称:Server-SideRequestForgery,即服务器端请求伪造,是一个由攻击者构造请求在目标服务

    2022年6月25日
    28

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号