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


相关推荐

  • 如何制作404页面

    如何制作404页面第一步:选取你喜欢的404页面,右键查看源代码,全选复制。第二步:粘贴刚才复制的代码到编辑器,更改对应的跳转链接,文字,以及页面的标题,404图片路径。查看修改编码方式,如不修改可能出现乱码,命名为404.html。第三步:制作404图片,将图片保存到桌面。第四步:打开404.html,没有问题后上传到网站根目录,图片上传到对应的图片路径第五步:在主机管理后台-基础环境设置…

    2022年7月27日
    7
  • 深入浅出URL编码

    深入浅出URL编码

    2021年5月10日
    311
  • 免费申请国外免费域名超详细教程

    免费申请国外免费域名超详细教程1.首先申请免费域名网站:https://my.freenom.com/domains.php2.填入域名,这里我们以xcflag为列(尽量选择复杂一点的或者五个字母以上的域名,因为简单的有些域名是需要收费的),点击检查可用性。3.可以看到很多免费的域名(用的谷歌翻译插件,翻译有时候不是很准确,free翻译过来应该是免费而不是自由,之后会写一些关于谷歌插件的笔记,详细讲解)4.我们选择xcflag.tk点击立即获取,稍等一会点击购物车查看绿色按钮5.默认三个月试用,这里下拉框我们选择十二个月

    2022年6月30日
    86
  • Nslookup命令_nslookup命令反解ip

    Nslookup命令_nslookup命令反解ip1、nslookup作用nslookup用于查询DNS的记录,查询域名解析是否正常,在网络故障时用来诊断网络问题2、查询a.直接查询nslookupdomain[dns-server]//如果没有指定dns服务器,就采用系统默认的dns服务器。b.查询其他记录nslookup-qt=typedomain[dns-server]type:…

    2022年10月19日
    4
  • PHP UEditor富文本编辑器 显示 后端配置项没有正常加载,上传插件不能正常使用…

    PHP UEditor富文本编辑器 显示 后端配置项没有正常加载,上传插件不能正常使用…

    2021年10月11日
    304
  • 深入Redis持久化

    深入Redis持久化

    2021年11月3日
    37

发表回复

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

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