aspose file tools*
The moose likes JSF and the fly likes How retrieve session value inside servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How retrieve session value inside servlet " Watch "How retrieve session value inside servlet " New topic
Author

How retrieve session value inside servlet

Suresh Khant
Ranch Hand

Joined: Feb 27, 2010
Posts: 118
Hi All,

I have set a session variable inside one of my beans as the following



I can retrieve the value of the session in other beans using the following


but the above code fails if i want to retrieve the value inside servlet (doGet method)

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16136
    
  21

Rule #1 of JSF: the more JSF-specific code you use, the more likely you're doing it wrong.

Forget the FacesContext.

Create the bean named "userId" as a Managed Bean and inject into whatever bean you had been using to do the getSessionMap().put(). Manipulate the userId bean if/when needed using POJO code.

In the servlet, do an HttpServletRequest().getSession().getAttribute('userId") to obtain the bean for servlet code to use.

Customer surveys are for companies who didn't pay proper attention to begin with.
Suresh Khant
Ranch Hand

Joined: Feb 27, 2010
Posts: 118
Thanks Tim Holloway for your reply ,

I have wrote the following code , but it does not








setting the session


retreiving the session from servlet
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16136
    
  21


Forget the FacesContext


You are attempting to "pull" userBean. JSF is based on Inversion of Control, which is "push" based.





Note that there is no FacesContext - or any other javax.faces import in the above. It accesses userBean in JSF using only POJO techniques.
Suresh Khant
Ranch Hand

Joined: Feb 27, 2010
Posts: 118
Thanks Tim Holloway for your reply ,


Create the bean named "userId" as a Managed Bean and inject into whatever bean you had been using to do the getSessionMap().put(). Manipulate the userId bean if/when needed using POJO code.

In the servlet, do an HttpServletRequest().getSession().getAttribute('userId") to obtain the bean for servlet code to use.


You have not used




in your above examples ,


how i can set the session in any other beans and retrieve the session from a servlet or inside a servlet
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16136
    
  21

I don't need to get the session map in JSF code. JSF does that automatically.

JSF session objects are HTTPSession objects. The only difference between session Managed Beans and bog-standard J2EE session beans is that JSF automatically constructs and initializes them and automatically stores them in the HTTPSession. After that, there's absolutely no way to tell the difference.
Suresh Khant
Ranch Hand

Joined: Feb 27, 2010
Posts: 118
Thanks Tim Holloway for your reply and your effort ,

I apologize you as I am a very slow learner

but the question is that How I can access the session value from servlet ? I have tried below code but it fails.










Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16136
    
  21


public class UsersListBean {
private UserBean userBean;

/**
* POJO accessor for userBean injected by faces-config.
* @param userBean The UserBean instance being injected.
*/
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}

public void doSomething(ActionEvent ae) {
...........
UserBean userBean = new UserBean();
this.userBean.setUserInfo(userInfo);

FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getSessionMap().put("userBean",userBean);
}
}

I removed the ActionEvent parameter because I strongly discourage frivolous use of Listeners. Use a simple POJO action method instead.

You don't need a constructor for userBean because it's a Managed Bean and JSF will construct the UserBean instance for you and store it as a session object in accordance with the faces-config directives supplied.

You don't need FacesContext or the getSessionMap.put operation to store the UserBean as a session object because JSF did that already as part of the process of creating the managed bean, as mentioned above.

All you need to do is use the bean itself. Everything else was done for you automatically by JSF. It's what JSF is all about. Simplification.

Here's the faces-config info that goes with all of the above:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How retrieve session value inside servlet