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

  • SEARCH
  • Home
  • 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 6949099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:54:37+00:00 2026-05-27T13:54:37+00:00

I have a stateful EJB that I am using to keep current user information

  • 0

I have a stateful EJB that I am using to keep current user information within my application. This EJB is injected into the servlet controller and used to store the last user logged in. However, the session seems to be the same on every single client.

Sample EJB Code:

Stateful
@LocalBean
public class CurrentUserBean {

private string Username;

public void setUser(String un)
{
    Username = un;
}

....

Sample Servlet Code:

public class MainController extends HttpServlet {
       @EJB private CurrentUserBean userBean;

        protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                HttpSession session = request.getSession();
                String name = session.getAttribute("username");

                userBean.setUser(name);
                ......

Now, when the application is deployed on my server and I have many different people talking to the server from several different clients, the username is always set to the last user that logged in. In other words, it seems as if the stateful session bean is keeping the same state across all clients. This confused me greatly, as I read in the java 6 ee tutorial the following quote from page 247:

In a stateful session bean, the instance variables represent the state
of a unique client/bean session. Because the client interacts
(“talks”) with its bean, this state is often called the conversational
state. As its name suggests, a session bean is similar to an
interactive session. A session bean is not shared; it can have only
one client, in the same way that an interactive session can have only
one user. When the client terminates, its session bean appears to
terminate and is no longer associated with the client.

Can someone explain why this is occurring and also explain how to use stateful beans in a proper manner that doesn’t keep the same state across all clients?

Thank you.

  • 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-27T13:54:37+00:00Added an answer on May 27, 2026 at 1:54 pm

    Although a statefull session bean has a state per user (session actually), the servlet doesn’t. By injecting it as you do now a single bean is used by this servlet since it is injected when the servlet is created (probably the bean beloning to the first visitor).

    You shouldn’t inject the bean, but retrieve it from the context within the processRequest method.

    InitialContext ctx= new InitialContext();
    CurrentUserBean userBean = (CurrentUserBean)ctx.lookup("CurrentUserBean");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We currently have a Stateful bean that is injected into a Servlet. The problem
I have a ConfigParameter SLSB that reads configuration values from the ejb-jar.xml file. This
I have a Tapestry application ( WAR , no EJB) that ... ... I
I have a servlet code which calls a ejb stateful session bean code as
I have a @RequestScoped CDI bean that I want to turn into an EJB
So I have a Stateful .NET webservice (C#) that I would like Flex to
I have a model that is stateful. In each state there are a selection
I know that a Stateful EJB can be accessed concurrently by a particular client.
I have a Java EE application consisting of 1 web module and 1 EJB
I have: existing object oriented native code API (non GUI) GUI application that works

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.