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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 记念刘和珍君是什么文体_纪念原版

    记念刘和珍君是什么文体_纪念原版转载自Tony2009年12月15日19:48阅读(6)评论(0)分类:休闲搞笑权限:公开刚和蘑菇头在安中楼楼下聊天,忽闻VeryCD被封的消息,网上骂声一片,满腔愤慨无处发泄,遂成此文。鲁迅先生先知,如今中国的种种奇景,先生几十年前便已洞悉一切。———————————————…

    2022年8月10日
  • input事件的获取

    input事件的获取loop线程已经运行起来了,如果不出意外,它是不会终止的;不妨以此为起点,再开始一段新的旅程,我要去探索input事件的获取。一EventHub构造函数EventHub是所有输入事件的中央处理站,凡是与输入事件有关的事它都管。上帝创造万事万物都是有原因的,看看构造它是出于什么目的。EventHub::EventHub(void):mBuiltInKeybo

    2022年6月10日
  • ov7725 stm32_如何给实验培养皿拍照

    ov7725 stm32_如何给实验培养皿拍照平台:STM32ZET6(核心板)+ST-LINK/V2+SD卡+USB串口线+鹰眼OV7725摄像头(注意,为了减少摄像头连线的麻烦,建议初学者选取单片机时选用带有摄像头接口的板子)工程介绍:需要移植FatFs文件系统,同时需要了解BMP位图的存储数据结构,从而实现将摄像头输出的RGB565像素数据直接输出到sd卡上,保存为*.bmp文件。1.BMP位图的存储1.1 数据结构介绍//BMP头文…

    2022年9月24日
  • 在Windows Server 2012 中安装 .NET 3.5 Framework「建议收藏」

    在Windows Server 2012 中安装 .NET 3.5 Framework「建议收藏」现在仍然有许多程序和应用需要依靠.NET3.5framework来运行。比如sqlserver在WindowsServer2012中,微软提供了.NET3.5和.NET4.5的安装选项以为你的应用程序提供支持。但在安装.NET3.5组件时提示如下信息!当使用服务器管理器来安装.NET3.5时你可能会遇到下面的提示信息,指出源文件无法找到安装其他的功能在添加时并不需要我们提供源…

    2022年6月6日
  • 区块链:P2P技术是什么

    区块链:P2P技术是什么摘要:包括比特币、以太坊等在内的去中心化的区块链平台,其底层网络都是采用的P2P技术实现,每个节点都是对等的。而本文,则先通过介绍P2P技术的特点和发展历史,让大家对P2P这个技术的来龙去脉有一个初步的认识和了解。然后在下一篇文章中,我会详细介绍第三代P2P技术(DHT)—Kademlia算法的实现原理。1.p2p的定义P2P是peer-to-peer的简称,又称为点对点技…

    2022年6月19日
  • PostMan使用教程。

    PostMan使用教程。原地址:https://blog.csdn.net/haibo0668/article/details/83828184 Postman教程——发送第一个请求 Postman教程——创建第一个集合 Postman教程——界面功能导航 Postman教程——设置 Pos…

    2022年5月7日

发表回复

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

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