1
votes

I have a servlet that will navigate to a JSF page upon some conditions using ExternalContext.redirect. I need to initialise a Managed Bean in this Servlet and set it in request scope so that my JSF page can directly access the Managed Bean's property and display them on the page load.

I had seen posts that sets the bean using getServletContext()

Like,

getServletContext().setAttribute("beanName",new Bean())

it works.But this approach will be setting the bean in application scope instead of request scope.

Also i tried the following:

request.setAttribute("beanName",new Bean()) 

it doesnt work

So pls let me know if there is any way to set /initialise a managed bean in a request scope

1
This is a strange requirement. Why don't you simply use a bean with @RequestScope and access it from your page?LaurentG
Create a managed bean with @ViewScoped annotation. The bean will be created since user access to the view (this will be in the redirection) and will live until the user leaves the view (change to another view, closes the tab or closes the browser).Luiggi Mendoza
Are you in a servlet or a backing bean?Sazzadur Rahaman
My requirement is i am navigating from a Non-JSF resource like a servlet to a JSF page.Doing so i need to have my managed bean preloaded with some values IN REQUEST SCOPE so that the page i am rendering can pull these values and display. @LaurentG I am using JSF 1.2 -No Annotations.My requirement does'nt bothers about how it is configured in JSF, as i am navigating from Servlet (A non JSF-Resource) to a JSF Page ( I am using XHTML: FYI)Sai Prasad Sabeson
Why don't you just do servlet's job in constructor of the backing bean and then link directly to the JSF page?BalusC

1 Answers

0
votes

If it's a request scoped bean, use HttpServletRequest#setAttribute()

BeanName beanName = new BeanName();

request.setAttribute("beanName", beanName);

If its a session scoped bean,

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