Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6998891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:28:10+00:00 2026-05-27T20:28:10+00:00

I need to modify a user session object (SessionScoped bean – CDI) in a

  • 0

I need to modify a user session object (SessionScoped bean – CDI) in a Servlet, so I have to obtain that bean somehow. I used injection in the following way:

@Inject
private UserSession user;

where UserSession is the SessionScoped CDI bean. user methods are called from either doPost or doGet servlet methods.
This works perfectly; every time the @Inject annotation injects the appropriate UserSession bean, but I don’t understand how this behavior is achieved.

I assumed that the beans, annotated with @Inject, are injected only once (when the object – Servlet instance in this case – is created), but it is obviously a wrong presumption.

So, when are these beans injected into the servlet? Per request? And how does this approach avoids conflicts (one servlet instance – multiple threads to deal with it) when there are multiple UserSession objects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:28:10+00:00Added an answer on May 27, 2026 at 8:28 pm

    The CDI uses the proxy pattern. The injected instance is actually not the real instance, but a proxy which locates the real instance depending on the current context and delegates all methods to it (like as how EJBs work). The autogenerated class of your UserSession bean looks roughly like this:

    public UserSessionCDIProxy extends UserSession implements Serializable {
    
        public String getSomeProperty() {
            UserSession instance = CDI.resolveItSomehow();
            return instance.getSomeProperty();
        }
    
        public void setSomeProperty(String someProperty) {
            UserSession instance = CDI.resolveItSomehow();
            instance.setSomeProperty(someProperty);
        }
    
    }
    

    This mechanism allows you to inject instances of a narrower scope in instances of a broader scope and allows you to still get the expected instance in the current context. The standard JSF @ManagedProperty annotation doesn’t support it, simply because it does not use a proxy, but injects the desired instance directly. That’s why it’s not possible to inject something of a narrower scope by @ManagedProperty.

    See also:

    • Backing beans (@ManagedBean) or CDI Beans (@Named)?
    • Get JSF managed bean by name in any Servlet related class
    • When using @EJB, does each managed bean get its own @EJB instance?
    • How to choose the right bean scope?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an issue where we need to modify keys for users that are
I have a row of data that I need to modify in a database,
I have User object with a list of groups that it belongs to: public
I need to modify a php search script so that it can handle multiple
I need to modify a column in a SQLite database but I have to
I need to store some session related data for a user. This data does
I have the following script which I need to modify a little. Here is
I'm building a system that need to collect some user sensitive data via secured
I'm trying to modify an object on a click. Here's what I have. <form>
hi i need to modify my code a little bit. i have a page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.