• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Problem in setting bean scope to request

Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am trying to implement a very simple example, where I am invoking one jsp from another using jsf and spring framework. given below are the code snippets -

web.xml contents -

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">




<!-- Faces Servlet -->
<servlet-name>Faces Servlet</servlet-name>

<!-- Faces Servlet Mapping -->
<servlet-name>Faces Servlet</servlet-name>


faces-config contents -

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"



Backing bean that contains transaction information.

<to-view-id>/pages/two.jsp </to-view-id>


application-context contents -

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="firstBean" class="com.rnd.web.faces.backinbean.FirstBean" scope="request">

I am calling FirstBean.firstToSecondMethod from one.jsp, setting some values in the method and calling two.jsp -

public String firstToSecMethod(){
System.out.println("inside java method.. ");
firstProp = "prop set in FirstBean java";
secondProp = 10;
return "second";

but two.jsp does not display the values

<td><h:outputText value="#{firstBean.firstProp}"/></td>

after changing the bean scope to session values are getting displayed properly.

I am not sure why this is happening.

I think request scope should work fine. Please advise.

Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's similar to http request and session scope.

For example, if on 1 page you set a value on the request, and forward to another page and try to read that value off the request object, you won't get it. A new request object has been created for that page.

If you want to get values from a previous page you have to store those values on the session.

JSF has 4 scopes, None, Request, Session and Application.

So let's say you had a LoginBean, that only did some kind of verification, nothing else. You would put that into the request scope, and call it when the user clicked login, for example. but now let's say after logon, you want to say hello to that user, so he logs on as billybob, and upon successfully logon, you want to say hello billybob, And you want to read the username from the bean, you will have to put that bean into the session scope, so that it is not discarded when you forward to the welcome page.

Does this make sense?
Abhijeet Vaidya
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

yeah, that's what it looks like. But won't it cause too much load on the server? if practically, backing beans (with domain objects) are same for data input and display and all are stored in session?

Also, if we do request.setAttribute() in java method, we can access those values in normal jsp, but it seems like that is not happening in case of JSF.

Correct me if I am wrong.

[ June 02, 2008: Message edited by: Abhijeet Vaidya ]
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic