tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steven.S...@savillemail.com
Subject useBean tag
Date Fri, 04 Aug 2000 02:27:37 GMT


The useBean tag seems to be less than useful when accessing session attributes.
Say I have a bean called "com.foo.beans.CustomerBean" i.e. that is it's
attribute-name set from a servlet. I then try to access the bean from a JSP
using the useBean tag.

     <jsp:useBean id="customerBean" .... />

How can I do this? It seems I need something like:

     <jsp:useBean id="customerBean" scope="session"
name="com.foo.beans.CustomerBean" />

Currently I am working around this with some code like this:

     <% customerBean = CustomerBean.get(request); %>

The CustomerBean.get is a static method which lazily instantiates the bean:

public class CustomerBean {
    private static BEAN_NAME = "com.foo.beans.CustomerBean";

    public static CustomerBean get(HttpRequest request) {
        HttpSession session = request.getSession();
        CustomerBean bean = (CustomerBean) session.getAttribute(BEAN_NAME);;
        if (bean == null) {
            synchronized (session) {
                bean = (CustomerBean) session.getAttribute(BEAN_NAME);;
                if (bean == null) {
                    bean = new CustomerBean();
                    session.setAttribute(BEAN_NAME, bean);
                }
            }
        }
        return careSession;
    }
.....

Any comments. Am I missing something?

cheers,

Steve.



Mime
View raw message