代码之家  ›  专栏  ›  技术社区  ›  Chintan

从servlet中删除上下文属性

  •  0
  • Chintan  · 技术社区  · 11 年前

    我有许多对象在上下文中存储对象 在使用存储其值的变量之后。 经过一些处理后,我想从上下文中删除该对象。

    例如:

    ServletContext context = getServletContext();
    boolean chk;
    chk=true;
    
    // save that value in context attribute
    context.setAttribute("myArray",chk);
    
    // after it used no need to require that object 
    //  so how can I remove that object from the context?
    if(somecondition){
    // I want to remove myArray attribute from context
    }
    

    那么,如何执行该任务?

    2 回复  |  直到 9 年前
        1
  •  1
  •   Aniket Kulkarni    11 年前

    使用 ServletContext#removeAttribute 方法删除属性。

    安全检查属性已设置或未使用 getAttribute() 方法

    if(somecondition){
       //i wnat to remove myArray attribute form contect
       if(context.getAttribute("myArray") != null) {
         context.removeAttribute("myArray");
       }
    }
    

    void removeAttribute(java.lang.String名称)

    • 从中删除具有给定名称的属性 ServletContext .
    • 删除后,后续调用 getAttribute(java.lang.String) 检索属性的值将返回 null .
    • 如果在 ServletContext 容器相应地通知它们。

    参数:

    • name-指定要删除的属性名称的字符串
        2
  •  0
  •   Karibasappa G C    11 年前

    公共抽象void removeAttribute(字符串名称)

    从绑定到特定名称的上下文中删除属性。

    所以做下面这样的事情

    context.removeAttribute("myArray");