Search code examples
jspservletsapache-commons-beanutils

What is the Scope of Bean object with BeanUtils.populate() Method?


i previously used <jsp:useBean> tags that have facility to decide the scope of bean for page or request or session or application but now i moved to MVC architechture, where i am using BeanUtils class to set the property of bean in Servlets. Please tell me what is the default SCOPE is provided by this method? and what i have to do if i want to change the scope?? I am using syntax:

BeanUtils.populate(bean, request.getParameterMap());


Solution

  • The BeanUtils#popuate() doesn't store the bean anywhere. It just populates the bean. The changes are reflected in the bean instance which you've there. You know, Java is Object Oriented and pass-by-value. After that line is called, the bean is populated. You just need to store the bean in the desired scope yourself.

    Request scope:

    request.setAttribute("bean", bean);
    

    Session scope:

    request.getSession().setAttribute("bean", bean);
    

    Application scope:

    getServletContext().setAttribute("bean", bean);
    

    Either way, it's available in JSP by ${bean}. There's no page scope, but that's not relevant anyway.

    See also: