Can I remove a bean from a session in JSP when the bean was set up using the <jsp:useBean ... scope="session"/> tag?

Ryan Breidenbach

Yes. <jsp:useBean id="bean" class="com.mypackage.MyBean" scope="session" /> basically translates into:

<%
  MyBean bean = (MyBean)session.getAttribute("bean");
  if (bean == null) {
    bean = new MyBean();
    session.setAttribute("bean",bean);   }
%>

So, you can remove it using the same sort of semantics:

<%
  session.removeAttribute("bean");
%>

Actually, you can remove an object stored in the session this way no matter how it was placed there.

Remember, the JSP XML actions have no "magic" about them. They are just a way to remove Java code from JSP pages and replace it with more user-friendly XML tags. This is the same priciple behind Custom Tags - it's another layer of abstraction. However, when all is said and done, the JSP will be translated in a Servlet and, therefore, Java code. So, anything that can be done with JSP XML action tags must be able to be "backed" by Java code.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact