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 7913533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:52:00+00:00 2026-06-03T13:52:00+00:00

Im trying out an example with EJB Session Beans. I wanna see the differences

  • 0

Im trying out an example with EJB Session Beans. I wanna see the differences them. My basic projects diagram is below ;

http://img109.imageshack.us/img109/8262/85220418.png

Project is like a shopping cart. When i invoke to the managed bean to get result of added Urun objects via injected EJB, result list must be last Urun object. Cause my EJB is @Stateless bean. But, when i run the application, it keeps the all new added Urun object. But it is Stateless, it must keep the last Urun object in every action. And then, when i open the application page in other web browser, it list the before added Urun object’s list. But managed bean and Ejb is Stateless in my model. But it acts a Singleton bean. Where is my problem?

@Stateless
public class AlisverisSepetiEJB {

    List<Urun> urunler=new ArrayList<>();

    public List<Urun> getUrunler() {
        return urunler;
    }

    public void setUrunler(List<Urun> urunler) {
        this.urunler = urunler;
    }

    public void urunEkle(Urun urun){
        urunler.add(urun);
    }

}

@ManagedBean(name="bean")
@RequestScoped
public class JSFYonetimliNesne {

    public JSFYonetimliNesne(){
        System.out.println("Yönetimli nesne çalıştı");
    }

   @EJB
   AlisverisSepetiEJB alisverisSepeti;

   Urun urun=new Urun();
   List<Urun> urunler;


    public List<Urun> getUrunler() {
        return alisverisSepeti.getUrunler();
    }

    public void setUrunler(List<Urun> urunler) {
        this.urunler = urunler;
    }

    public Urun getUrun() {
        return urun;
    }

    public void setUrun(Urun urun) {
        this.urun = urun;
    }

    public void sepeteKoy(){
        alisverisSepeti.urunEkle(urun);
        urun=new Urun();
    }

}

public class Urun {

   String urunAdi;
   Long fiyat;
   Long gramaj;

    public Long getFiyat() {
        return fiyat;
    }

    public void setFiyat(Long fiyat) {
        this.fiyat = fiyat;
    }

    public Long getGramaj() {
        return gramaj;
    }

    public void setGramaj(Long gramaj) {
        this.gramaj = gramaj;
    }

    public String getUrunAdi() {
        return urunAdi;
    }

    public void setUrunAdi(String urunAdi) {
        this.urunAdi = urunAdi;
    }

}

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Alışveriş Sepeti</title>
    </h:head>
    <h:body>
        Alışveriş Sepeti
        <h:form>

            <h:panelGrid columns="2">
            <h:outputLabel value="Ürün adı : "/>
            <h:inputText value="#{bean.urun.urunAdi}"/>
            <h:outputLabel value="Ürün fiyatı : "/>
            <h:inputText value="#{bean.urun.fiyat}"/>
            <h:outputLabel value="ÜRün gramajı : "/>
            <h:inputText value="#{bean.urun.gramaj}"/>
            </h:panelGrid>

            <h:commandButton action="#{bean.sepeteKoy}" value="Sepete Ekle"/>

            <br><h:outputLabel value="Sepetteki Ürünler"/></br>


            <h:dataTable value="#{bean.urunler}" var="item" border="1">

            <h:column >
                <f:facet name="header">
                    <h:outputLabel value="Ürün adı"/>
                </f:facet>
            <h:outputText value="#{item.urunAdi}"/>
            </h:column>

            <h:column >
                <f:facet name="header">
                    <h:outputLabel value="Ürün fiyatı"/>
                </f:facet>
            <h:outputText value="#{item.fiyat}"/>
            </h:column>

            <h:column >
                <f:facet name="header">
                    <h:outputLabel value="Ürün gramajı"/>
                </f:facet>
            <h:outputText value="#{item.gramaj}"/>
            </h:column>

            </h:dataTable>

        </h:form>

    </h:body>
</html>
  • 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-06-03T13:52:04+00:00Added an answer on June 3, 2026 at 1:52 pm

    It’s quite possible that the same instance of AlisverisSepetiEJB is being shared between invocations (the container can do this, it’s perfectly legal). Precisely because of the stateless nature of a @Stateless EJB, you should not declare instance attributes in it!

    Remove the List<Urun> urunler = new ArrayList<>(); declaration and all associated methods, think of a stateless bean as a class where all its methods are static – meaning, that they can’t use any instance attributes and must be completely independent of instance data; if any state needs to be maintained between calls, then it must be passed along as parameters from the point where the EJB is being invoked. The only instance variables allowed in a @Stateless EJB are attributes which are themselves stateless and/or immutable.

    If you need to maintain the state of the shopping cart between invocations, you’ll have to keep it elsewhere in a stateful context (say, the Session context of a web application) or mark the EJB as @Stateful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi while trying out jni example in this link http://wendro.blogspot.com/2010/03/jni-example-eclipse-dev-cpp.html?showComment=1309930446765#c5048550711511727724 with eclipse Helios windows
I am trying out this example http://thinkdiff.net/facebook/new-javascript-sdk-oauth-2-0-based-fbconnect-tutorial/ But I am getting xml error which
I am trying out the example given in http://docs.python.org/library/re.html for the group numbering and
I'm trying out the example Hello World from this link here: http://www.mulesoft.org/documentation/display/MULE2INTRO/Quick+Start I have
I am trying out the basic QSortFilterProxyModel example in qt assistant and have made
I’m trying out Meteor’s Leaderboard example and am running into a bug in trying
I'm trying to figure out how XSLT process namespace prefixes and have following example:
I'm trying to figure out why the control does not honor ZIndex. Example 1
I'm using Mirven's Twitter OAuth Sinatra example and trying to figure out how I
I'm new in android development and I am trying out the WebView example in

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.