• 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 ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

How retrieve session value inside servlet

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

 
Saloon Keeper
Posts: 27488
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Suresh Khant
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27488
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27488
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27488
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic