Hi,
Today we will discuss about different
scopes of managed bean in JSF(Java server faces).
A brief introduction about JSF and
managed bean.
Java Server Faces:- A component based
Java web development frame work which supports MVC model.
MVC model:-
- Model :- Backing beans,where web page components are mapped to an instance variables of a bean
- View- presentation layer
- Controller:- which will manage the navigation of web pages based on a particular output.
There are four different session
scopes(Ascending) for a manged bean.
- View/Page:- valid only for a particular page
- Request:- valid for a particular request
- Session:- valid for a session
- Application:- valid for entire web application at the container(server) level.
We will explain them in more detail by
below examples
JSP pages:-
- Login.jsp:- will have userName,password,login button and log out button
- userWelcome.jsp :- If userName is google the user name parameter is displayed here.This page also has links to page1.jsp and page2.jsp
- page1.jsp:- The username is forwarded to this page from login page
- page2.jsp:-The username is forwarded to this page from login page
snapshots or code for jsp pages..
Login.jsp
page1.jsp/page2.jsp
ManagedBean/Backing bean
- SessionMaintanence :- where session invalidation,defining scopes etc. are done
faces-config.xml :- is an xml
where manged bean are registered,navigation is defined.
Control flow of application:-
Login.jsp will
have user name and password,when user clicks on Login button and user
name entered is google the page is forwarded to userWelcome.jsp
displaying logged in user name.
In userWelcome.jsp
we have liks to pag1.jsp and page2.jsp
Every page will
have a logout button. The logged in userName is used in all the
pages.
Code for
SessionMaintanence bean:-
@ViewScoped
//scope of the bean
@ManagedBean
//Registering the bean as manged bean
public
class
SessionMaintanence implements
Serializable {
private
String userName;
//username mapping
private
String passWord;
//getter
and setter methods
public
String getUserName() {
return
userName;
}
public
void
setUserName(String userName) {
this.userName
= userName;
}
public
String getPassWord() {
return
passWord;
}
public
void
setPassWord(String passWord) {
this.passWord
= passWord;
}
public
String validate()
{
if(getUserName().equals("google"))
{
return
"success";
}else
{
return
"false";
}
}
//Method
for session invalidatio
public
String inValidate()
{
FacesContext
fc=FacesContext.getCurrentInstance();
ExternalContext
ec=fc.getExternalContext();
ec.invalidateSession();
return
"";
}
}
- Page/View scope:- We will enter google as username and click on login,the page is navigated to userWelcome.jsp
In the above code we have scope as
viewScoped...
Now we will enter userName as google
enter password and click on Login,the page is directed to
userWelcome.jsp, as we can see as the bean is viewScoped the userName
will not be printed in this page the out put will be
welcome
Conclusion:- As
the bean is view/page scoped userName is not forwarded to
userWelcome.jsp the value is limited to Login.jsp page only.
2.Request scope
:- Now we will change the scope to @RequestScope
in SessionMaintanence
and will execute the same by entering google
as
user name and password
The
output from the userWelcome.jsp will be like
welcome
google
Now
we will click on page1.The output will be like
welcome
to page1
[Actually
here userName also should be printed]
Though
the scope is request the username did not get printed,because when
we click on page1 hyperlink we are sending a new request so the
values stored in the earlier request will be lost
3.Session
scope
We will change the scope to
@SessionScoped
,will
execute the same flow the output from userWelcome.jsp will be
welcome
google
Now
we will click on page1 ,we are sending a new request as the bean is
in session scoped the output from pag1.jsp will be
welcome
google to
page1
Observe
the differences between request and session scoped from the output
from page1.jsp
welcome
to page1 and welcome
google to
page1
4.Application
scope
This
the most wider range scope at the application level for entire
container/server
Lets
take a scenario where we want to count the number of page hits for
our page.How to implement this scenario?Which session scope we will
use?
Here
comes the application scope.
No comments:
Post a Comment