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


相关推荐

  • Ribbon整合Avalondock 2.0实现多文档界面设计(一)

    Ribbon整合Avalondock 2.0实现多文档界面设计(一)前些时间研究了WPF的一些框架,感觉基于Prism框架的MVVM模式对系统的UI与逻辑分离很好,所以就按照之前Winform的框架设计,用WPF做了一套,感觉比Winform要强很多。MVVM模式和MVC模式一样,主要目的是分离视图(View)和模型(Model),有几大优点1.低耦合。视图(View)可以独立于Model变化和修改,一个ViewModel可以绑定到不同的”View”上,…

    2022年7月20日
    23
  • 【机器学习】彻底理解神经网络中的反向传播算法(BP)

    【机器学习】彻底理解神经网络中的反向传播算法(BP)目录1,前言2,例程Step1前向传播Step2反向传播3,代码实现1,前言最近在看机器学习神经网络方面的知识,也看了很多关于反向传播算法原理的介绍,有一篇文章写的很好,在这里记录下,并且加入自己的理解。反向传播法其实是神经网络的基础了,但是很多人在学的时候总是会遇到一些问题,或者看到大篇的公式觉得好像很难就退缩了,其实不难,就是一个链式求导法则反复用。如果不想…

    2022年5月3日
    56
  • shell:修改变更值[通俗易懂]

    shell:修改变更值[通俗易懂]#catconfd-general-config.confETCD_SERVER_HOSTIP=192.168.3.103ETCD_SERVER_PORT=2379innernetworksegement=192.168.3pgconn=10.47.245.110:13306,10.47.245.110:23306kafkaconn=10.47.223.223:9090,10…

    2022年7月11日
    26
  • Android中的应用——谷歌官方Json分析工具Gson使用

    Android中的应用——谷歌官方Json分析工具Gson使用

    2022年1月6日
    148
  • 编程路上必定要知道的数据库语言SPL

    编程路上必定要知道的数据库语言SPL要说清这个目标,先要理解数据库是做什么的。数据库这个软件,名字中有个“库”字,会让人觉得它主要是为了存储的。其实不然,数据库实现的重要功能有两条:计算、事务!也就是我们常说的OLAP和OLTP,数据库的存储都是为这两件事服务的,单纯的存储并不是数据库的目标。我们知道,SQL是目前数据库的主流语言。那么,用SQL做这两件事是不是很方便呢?事务类功能主要解决数据在写入和读出时要保持的一致性,实现这件事的难度并不小,但对于应用程序的接口却非常简单,用于操纵数据库读写的代码也很简单。如果假定目前关系数据库的逻辑存储

    2022年10月6日
    3
  • python3、sqlmap下载与安装教程

    一、前提需要安装python3,可以参考其他教程二、下载官网下载http://sqlmap.org/三、安装将下载的sqlmap.zip解压到文件夹sqlmap中,并拷贝到Python安装路径下四、配置在桌面上创建一个cmd进入python的快捷方式(这步可以不做,只是比较方便启动),右键新建快捷方式输入cmd快捷方式命名可随意,不作要求,这里用sqlmap右键–属性修改起始位置为sqlmap文件夹路径测试启动sqlmap,双击刚才创建的快捷方式,

    2022年4月7日
    239

发表回复

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

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