前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >servlet中init-param与context-param的区别「建议收藏」

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

作者头像
全栈程序员站长
发布2022-09-13 10:14:30
4940
发布2022-09-13 10:14:30
举报
文章被收录于专栏:全栈程序员必看

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

init-param

web.xml中的写法
代码语言:javascript
复制
<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>
代码语言:javascript
复制
init-param配置在servlet标签中,用来初始化当前的Servlet的,属性存放在servletConfig中
因此可以通过获取servletConfig对象来获取servlet中init-param里配置的属性,作用域
限制在当前的Servlet中
获取方式一
代码语言:javascript
复制
1.声明一个ServletConfig当做成员变量
2.重写初始化方法init
  通过该方法的参数 自动获取ServletConfig对象
  ServletConfig对象中保存的是Servlet中的配置信息
代码语言:javascript
复制
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 {
    }
}
获取方式二–简单粗暴
代码语言:javascript
复制
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中的写法
代码语言:javascript
复制
<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>
获取方式
代码语言:javascript
复制
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的区别

区别一
代码语言:javascript
复制
在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
只有一种获取方式
这里写图片描述
这里写图片描述
区别二 作用域不同
代码语言:javascript
复制
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.cn/160405.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • init-param
    • web.xml中的写法
      • 获取方式一
        • 获取方式二–简单粗暴
        • context-param
          • web.xml中的写法
            • 获取方式
            • init-param与context-param的区别
              • 区别一
                • 区别二 作用域不同
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档