Question

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());

Was it helpful?

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:

OTHER TIPS

There is no scope associated with javabeans. The scope is only jsp/webapp related. In that sense, BeanUtils.populate(..) does not store the bean anywhere in the web contexts, and so it doesn't have a scope. If you choose to add the thus created bean to a given context, only then it would have scope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top