2014-04-03 60 views
-1

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

例如:

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 
} 

那么,怎样才能执行该任务?

回答

1

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

用于安全检查属性设置或不使用getAttribute()方法。

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

的removeAttribute空隙(java.lang.String中名称)

  • 移除此ServletContext具有给定名称的属性。
  • 删除后,后续调用getAttribute(java.lang.String)以检索属性的值将返回null
  • 如果监听器配置在ServletContext上,容器会相应地通知他们。

参数:

  • 名称 - 一个String指定属性的名称被移除
0

公共抽象无效的removeAttribute(字符串名称)

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

所以做一些事情像下面

context.removeAttribute("myArray");