{"id":16223,"date":"2013-08-09T15:03:16","date_gmt":"2013-08-09T12:03:16","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=16223"},"modified":"2018-09-26T15:09:21","modified_gmt":"2018-09-26T12:09:21","slug":"java-ee-stateful-session-bean-ejb-example","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html","title":{"rendered":"Java EE Stateful Session Bean (EJB) example"},"content":{"rendered":"<p>In this article we are going to see how you can use Stateful Session Beans to keep track of state across a client Session in a simple Web Application.<\/p>\n<h2>1. Introduction<\/h2>\n<p>Stateful Session Beans usually hold information about a specific client&#8217;s session, and holds that information throughout the whole session (opposed to Stateless Session Beans). A Stateful EJB instance is coupled with only one client. One client can of course have many EJB instances is his session.<\/p>\n<p>In this example we are going to create a simple Shopping Cart application. The session bean will hold a list of products. As the client adds more products to his cart, this list will grow accordingly. Finally the client will be able to checkout his order and the products on the aforementioned list will be persisted in a MySQL database.<\/p>\n<p>To implement the above functionality,\u00a0we are going to create an EAR Project and an EJB Project that will host our Session Bean and a Dynamic Web Application that will host a Servlet, testing the aforementioned behavior. We are going to use Eclipse Java EE IDE 4,3 Kepler and Glassfish 4.0 as our container. Additionally we are going to use standard JPA 2.o to persist our products in a MySQL\u00a05.6.14 database running on localhost. Here is a guide on how to<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/connector-j-usagenotes-glassfish-config.html\"> Integrate MySQL with Glassfish<\/a>.<\/p>\n<h2>2. Create a new Enterprise Application Project<\/h2>\n<p>Create a new Enterprise Application Project named\u00a0<code>StatefulEJBEAR<\/code>\u00a0. In Eclipse IDE select File -&gt; New -&gt; Enterprise Application Project, fill in the form and click Finish:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-ear-project1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20399\" alt=\"new-ear-project\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-ear-project1.png\" width=\"519\" height=\"630\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-ear-project1.png 519w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-ear-project1-247x300.png 247w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><\/a><\/p>\n<h2>3. Create a new EJB Projet<\/h2>\n<p>Create a new EJB Project called <code>StatefulSessionBeansEJB<\/code>. We are going to create our session bean on this. Go to File -&gt; New -&gt; EJB Project and fill out the form. Be careful to select \u201cAdd EAR Project\u201d and Select \u201c<code>StatefulEJBEAR<\/code>\u201d as EAR project name:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-ejb-project1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20400\" alt=\"new-ejb-project\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-ejb-project1.png\" width=\"519\" height=\"709\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-ejb-project1.png 519w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-ejb-project1-219x300.png 219w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><\/a><\/p>\n<h2>4. Create a Stateful Session Bean<\/h2>\n<p>Open <code>StatefulSessionBeansEJB<\/code> Project in the Project Explorer and in the folder\u00a0<code>ejbModule<\/code>\u00a0create a new source package named <code>com.javacodegeeks.enterprise.ejb<\/code>. In that package create a new <code>Interface<\/code> that will be a local view of the EJB:<\/p>\n<p><em><span style=\"text-decoration: underline;\">Cart.java:<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.enterprise.ejb;\r\n\r\nimport javax.ejb.Local;\r\n\r\nimport com.javacodegeeks.enterprise.product.Product;\r\n\r\n@Local\r\npublic interface Cart {\r\n\r\n\t  void addProductToCart(Product product);\r\n\r\n\t  void checkOut();\r\n\r\n}<\/pre>\n<p>As you can see, we declare two methods, one to add a product to the cart and another to checkout the order.<\/p>\n<p>And here is the Session Bean:<\/p>\n<p><em><span style=\"text-decoration: underline;\">CartBean.java:<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.enterprise.ejb;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\nimport java.util.concurrent.TimeUnit;\r\n\r\nimport javax.annotation.PostConstruct;\r\nimport javax.ejb.Stateful;\r\nimport javax.ejb.StatefulTimeout;\r\nimport javax.ejb.TransactionAttribute;\r\nimport javax.ejb.TransactionAttributeType;\r\nimport javax.persistence.EntityManager;\r\nimport javax.persistence.PersistenceContext;\r\n\r\nimport com.javacodegeeks.enterprise.product.Product;\r\n\r\n@Stateful\r\n@StatefulTimeout(unit = TimeUnit.MINUTES, value = 20)\r\npublic class CartBean implements Cart {\r\n\r\n\t@PersistenceContext(unitName = \"pu\", type = PersistenceContextType.EXTENDED)\r\n\tprivate EntityManager entityManager;\r\n\r\n\tprivate List products;\r\n\r\n\t@PostConstruct\r\n\tprivate void initializeBean(){\r\n\t   products = new ArrayList&lt;&gt;();\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void addProductToCart(Product product) {\r\n\t\t products.add(product);\r\n\r\n\t}\r\n\r\n\t@Override\r\n\t@TransactionAttribute(TransactionAttributeType.REQUIRED)\r\n\tpublic void checkOut() {\r\n\t\tfor(Product product : products){\r\n\t\t\tentityManager.persist(product);\r\n\t\t}\r\n\t\tproducts.clear();\r\n\r\n\t}\r\n}<\/pre>\n<p>As you can see, our Session bean implements our <code>Cart<\/code> interface and simply holds a List of products (more on class <code>Product<\/code> later).<\/p>\n<p>In the above code notice:<\/p>\n<ul>\n<li>We use <code><a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/ejb\/Stateful.html\">@Stateful<\/a><\/code>to annotate the class as a Stateful Session Bean.<\/li>\n<li>We declare a timeout with <code>@StatefulTimeout(unit = TimeUnit.MINUTES, value = 20)<\/code> annotation. This timeout denotes the amount of time that the bean should exist, and thus be valid for the session. It should correspond with the HTTP Session timeout value.<\/li>\n<li>We use <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/persistence\/PersistenceContext.html\">@PersistenceContext<\/a> to inject an <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/persistence\/EntityManager.html\">EntityManager<\/a> that will handle the persistence of our products.<\/li>\n<li>We use <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/annotation\/PostConstruct.html\"><code>@PostConstruct<\/code><\/a> annotation on <code>private void initializeBean()<\/code> method. This will denote to the EJB container to execute that method on bean initialization. You can view it as a constructor.<\/li>\n<li>We use <code><a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/ejb\/TransactionAttribute.html\">@TransactionAttribute(TransactionAttributeType.REQUIRED)<\/a><\/code> annotation on <code>public void checkOut()<\/code> method. This annotation is required to denote that the container is about to invoke a business method within a transaction context. As you can, see in that method the products on the list are persisted in the database.<\/li>\n<\/ul>\n<h2>5. The Product Entity class<\/h2>\n<p>This is the object representing a simple product in our cart application. It consists of an id and a type. As we said, when the order is checked out we want the product on the cart to be persisted in a database. We used JPA 2.0 annotations to map <code>Product<\/code> class to a MySQL table. For this example, I&#8217;ve created a simple database named <code>shop<\/code> and table named <code>product<\/code> created with the script:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><span style=\"text-decoration: underline;\"><em>MySQL product table creation script:<\/em><\/span><\/p>\n<pre class=\"brush:bash\">CREATE TABLE `product` (\r\n  `ID` int(11) NOT NULL AUTO_INCREMENT,\r\n  `TYPE` varchar(256) COLLATE utf8_unicode_ci NOT NULL,\r\n  PRIMARY KEY (`ID`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8<\/pre>\n<p>So this is the table:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/product-table.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20406\" alt=\"product-table\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/product-table.png\" width=\"266\" height=\"225\" \/><\/a><\/p>\n<p>Let&#8217;s see the code of <code>Product<\/code> class:<\/p>\n<p><em><span style=\"text-decoration: underline;\">Product.java:<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.enterprise.product;\r\n\r\nimport java.io.Serializable;\r\n\r\nimport javax.persistence.Column;\r\nimport javax.persistence.Entity;\r\nimport javax.persistence.GeneratedValue;\r\nimport javax.persistence.GenerationType;\r\nimport javax.persistence.Id;\r\nimport javax.persistence.Table;\r\n\r\n@Entity\r\n@Table(name = \"PRODUCT\", catalog = \"shop\")\r\npublic class Product implements Serializable {\r\n\r\n  private static final long serialVersionUID = 1L;\r\n\r\n  @Id\r\n  @GeneratedValue(strategy = GenerationType.IDENTITY)\r\n  @Column(name = \"ID\", nullable = false)\r\n  private int id;\r\n\r\n  @Column(name = \"TYPE\", nullable = false)\r\n  private String type;\r\n\r\n  public int getId() {\r\n    return id;\r\n  }\r\n\r\n  public String getType() {\r\n    return type;\r\n  }\r\n\r\n  public void setType(String description) {\r\n    this.type = description;\r\n  }\r\n\r\n}<\/pre>\n<p>The above annotations are self explanatory. Very briefly, we use:<\/p>\n<ul>\n<li><a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/persistence\/Entity.html\">@Entity<\/a> to declare the class as an Entity.<\/li>\n<li><code>@Table(name = \"PRODUCT\", catalog = \"shop\")<\/code> to show that the class will be mapped to a table named <code>product<\/code> in a database named <code>shop<\/code>.<\/li>\n<li><code>@Id<\/code>, <code>@GeneratedValue(strategy = GenerationType.IDENTITY)<\/code> to declare that the field <code>id<\/code> of the class <code>Product<\/code> will be the primary key of the corresponding database table.<\/li>\n<li><code>@Column<\/code> to map a class field to a database column of table <code>product<\/code>.<\/li>\n<\/ul>\n<p>Finally, for the persistence to work we need to create a <code>persistence.xml<\/code> file inside <code>ejbModule\/META-INF<\/code> folder. The file looks like this:<\/p>\n<p><em><span style=\"text-decoration: underline;\">application.xml:<\/span><\/em><\/p>\n<pre class=\"brush:xml\">&lt;persistence xmlns=\"http:\/\/java.sun.com\/xml\/ns\/persistence\"\r\n             xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n             xsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/persistence http:\/\/java.sun.com\/xml\/ns\/persistence\/persistence_2_0.xsd\"\r\n             version=\"2.0\"&gt;\r\n\r\n    &lt;persistence-unit name=\"pu\" transaction-type=\"JTA\"&gt;\r\n\r\n        &lt;jta-data-source&gt;jdbc\/MySQLDataSource&lt;\/jta-data-source&gt; \r\n        &lt;class&gt;com.javacodegeeks.enterprise.product.Product&lt;\/class&gt;\r\n    &lt;\/persistence-unit&gt;\r\n\r\n&lt;\/persistence&gt;<\/pre>\n<p>Make sure you&#8217;ve integrated MySQL with Glassfish correctly using this <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/connector-j-usagenotes-glassfish-config.html\">quide<\/a>. For more information on <code>persistence.xml<\/code> file you can see this <a href=\"http:\/\/docs.oracle.com\/cd\/E16439_01\/doc.1013\/e13981\/cfgdepds005.htm\">Oracle guide<\/a>.<\/p>\n<p>So the final project structure of <code>StatefulBeansEJB<\/code> is :<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/ejb-project-structure.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20407\" alt=\"ejb-project-structure\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/ejb-project-structure.png\" width=\"392\" height=\"506\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/ejb-project-structure.png 392w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/ejb-project-structure-232x300.png 232w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/a><\/p>\n<h2>6. Create a new Dynamic Web Project<\/h2>\n<p>Go to File -&gt; New -&gt; Dynamic Web Project. Fill out the form and make sure you check \u201cAdd project to an EAR\u201d and put StatefulEJBEAR as the \u201cEAR project name\u201d:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-dynamic-web-project1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20409\" alt=\"new-dynamic-web-project\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-dynamic-web-project1.png\" width=\"506\" height=\"621\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-dynamic-web-project1.png 506w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-dynamic-web-project1-244x300.png 244w\" sizes=\"(max-width: 506px) 100vw, 506px\" \/><\/a><\/p>\n<p>After clicking \u201cFinish\u201d, go to the project Explorer and Right click on the Project\u00a0<code>StatefulSessionBeansTest<\/code>\u00a0and go to Properties-&gt; Deployment Assembly -&gt; Add -&gt; Porject -&gt; StatefulEJB :<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/deplyment-assembply.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20408\" alt=\"deplyment-assembply\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/deplyment-assembply.png\" width=\"770\" height=\"397\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/deplyment-assembply.png 770w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/deplyment-assembply-300x154.png 300w\" sizes=\"(max-width: 770px) 100vw, 770px\" \/><\/a><\/p>\n<h2>7. Create a new Servlet<\/h2>\n<p>Go to\u00a0<code>StatefulSessionBeansTest<\/code>\u00a0Web project and create a new Servlet named\u00a0<code>ShoppingCartServlet<\/code>:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-servlet1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20410\" alt=\"new-servlet\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-servlet1.png\" width=\"505\" height=\"433\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-servlet1.png 505w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/new-servlet1-300x257.png 300w\" sizes=\"(max-width: 505px) 100vw, 505px\" \/><\/a><\/p>\n<p>So this would be the final structure of the Web Project :<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/dynamic-web-project-structure.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20411\" alt=\"dynamic-web-project-structure\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/dynamic-web-project-structure.png\" width=\"392\" height=\"462\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/dynamic-web-project-structure.png 392w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/dynamic-web-project-structure-254x300.png 254w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/a><\/p>\n<p>Here is the code of the Servlet:<\/p>\n<p><em><span style=\"text-decoration: underline;\">ShoppingCartServlet.java:<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.enterprise.servlet;\r\n\r\nimport java.io.IOException;\r\n\r\nimport javax.naming.InitialContext;\r\nimport javax.naming.NamingException;\r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.annotation.WebServlet;\r\nimport javax.servlet.http.HttpServlet;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\nimport com.javacodegeeks.enterprise.ejb.Cart;\r\nimport com.javacodegeeks.enterprise.product.Product;\r\n\r\n@WebServlet(\"\/ShoppingCartServlet\")\r\npublic class ShoppingCartServlet extends HttpServlet {\r\n\r\n\tprivate static final long serialVersionUID = 1L;\r\n\tprivate static final String CART_SESSION_KEY = \"shoppingCart\";\r\n\r\n    public ShoppingCartServlet() {\r\n        super();\r\n\r\n    }\r\n\r\n\tprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n\r\n\t\tSystem.out.println(\"Hello from servlet\");\r\n\r\n\t\tCart cartBean = (Cart) request.getSession().getAttribute(CART_SESSION_KEY);\r\n\r\n\t\t if(cartBean == null){\r\n\t\t      \/\/ EJB is not yet in the HTTP session\r\n\t\t      \/\/ This means that the client just sent his first request\r\n\t\t\t  \/\/ We obtain a CartBean instance and add it to the session object.\r\n\t\t      try {\r\n\t\t        InitialContext ic = new InitialContext();\r\n\t\t        cartBean = (Cart) \r\n\t\t         ic.lookup(\"java:global\/StatefulEJBEAR\/StatefulSessionBeansEJB\/CartBean!\"\r\n\t\t         \t\t+ \"com.javacodegeeks.enterprise.ejb.Cart\");\r\n\r\n\t\t        request.getSession().setAttribute(CART_SESSION_KEY, cartBean);\r\n\r\n\t\t        System.out.println(\"shoppingCart created\");\r\n\r\n\t\t      } catch (NamingException e) {\r\n\t\t        throw new ServletException(e);\r\n\t\t      }\r\n\t\t }\r\n\r\n\t\t String productName = request.getParameter(\"product\");\r\n\t\t if(productName != null &amp;&amp; productName.length() &gt; 0){\r\n\r\n\t\t      Product product = new Product();\r\n\t\t      product.setType(productName);\r\n\t\t      cartBean.addProductToCart(product);\r\n\r\n\t\t      System.out.println(\"product \"+productName+\" added\");\r\n\t\t }\r\n\r\n\t\t String checkout = request.getParameter(\"checkout\");\r\n\t\t if(checkout != null &amp;&amp; checkout.equalsIgnoreCase(\"yes\")){\r\n\t\t      \/\/ Request instructs to complete the purchase\r\n\t\t      cartBean.checkOut();\r\n\t\t      System.out.println(\"Shopping cart checked out \");\r\n\t\t }\r\n\r\n\t}\r\n\r\n}<\/pre>\n<p>In the above Serlvet, when the user sents a GET request for the fist time, a new <code>CartBean<\/code> instance will be obtained from the container and added to the session. Then the <code>product<\/code> query parameter is parsed, and if it&#8217;s not null a new <code>Product<\/code> with <code>type<\/code> <code>productName<\/code>, and will e added to the list of products in the session bean.<\/p>\n<p>Then the <code>checkout<\/code> query parameter is parsed and if it&#8217;s evaluated to <code>'yes'<\/code> the products in the session bean will be persisted.<\/p>\n<p><strong>Tip:<\/strong><em>\u00a0If you are having trouble figuring out the\u00a0Portable JNDI names for EJB PassivationObject look at the logs or output of Glassfish when deploying the project and you will find a line like this :<\/em><em>2013-12-13T18:22:28.598+0200|INFO: EJB5181:Portable JNDI names for EJB PassivationObject: (java:global\/StatefulBeans\/StatefulEJB\/PassivationObject, java:global\/StatefulBeans\/StatefulEJB\/PassivationObject!com.javacodegeeks.enterprise.ejb.Passivation)<\/em><\/p>\n<h2>8. Test<\/h2>\n<p>Now we are simply going to deploy the Dynamic Web Application to Glassfish, and add some products on the cart. Then we will request to checkout the order.<\/p>\n<p>Let&#8217;s say we want to add some products, we can issue the following requests :<\/p>\n<pre class=\"brush:bash\">http:\/\/localhost:8080\/StatefulSessionBeansTest\/ShoppingCartServlet?product=ram\r\nhttp:\/\/localhost:8080\/StatefulSessionBeansTest\/ShoppingCartServlet?product=mouse\r\nhttp:\/\/localhost:8080\/StatefulSessionBeansTest\/ShoppingCartServlet?product=ssd<\/pre>\n<p>While posting these requests this is the output from the console:<\/p>\n<pre style=\"background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;\"><code style=\"color: black; word-wrap: normal;\">2014-01-07T22:02:07.622+0200|INFO: Hello from servlet\r\n2014-01-07T22:02:07.684+0200|INFO: shoppingCart created\r\n2014-01-07T22:02:07.687+0200|INFO: product ram added\r\n2014-01-07T22:02:12.236+0200|INFO: Hello from servlet\r\n2014-01-07T22:02:12.237+0200|INFO: product mouse added\r\n2014-01-07T22:02:24.851+0200|INFO: Hello from servlet\r\n2014-01-07T22:02:24.851+0200|INFO: product ssd added\r\n<\/code><\/pre>\n<p>Now to checkout the order you can issue:<\/p>\n<pre class=\"brush:bash\">http:\/\/localhost:8080\/StatefulSessionBeansTest\/ShoppingCartServlet?checkout=yes<\/pre>\n<p>This is the output from the console:<\/p>\n<pre style=\"background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;\"><code style=\"color: black; word-wrap: normal;\">2014-01-07T22:19:46.444+0200|INFO: Hello from servlet\r\n2014-01-07T22:19:46.537+0200|INFO: Shopping cart checked out\r\n<\/code><\/pre>\n<p>Here you can see the products on the database :<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/mysql-terminal.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20412\" alt=\"mysql-terminal\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/mysql-terminal.png\" width=\"497\" height=\"142\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/mysql-terminal.png 497w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/mysql-terminal-300x85.png 300w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><\/a><\/p>\n<h2>Dowload Eclipse Project<\/h2>\n<p>This was an example on\u00a0Java EE Stateful Session Bean (EJB). Here are the Eclipse Projects of this example :\u00a0<a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/StatefulEJBS.zip\">StatefulEJBS.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we are going to see how you can use Stateful Session Beans to keep track of state across a client Session in a simple Web Application. 1. Introduction Stateful Session Beans usually hold information about a specific client&#8217;s session, and holds that information throughout the whole session (opposed to Stateless Session Beans). &hellip;<\/p>\n","protected":false},"author":7,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[90],"class_list":["post-16223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-ejb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java EE Stateful Session Bean (EJB) example<\/title>\n<meta name=\"description\" content=\"In this article we are going to see how you can use Stateful Session Beans to keep track of state across a client Session in a simple Web Application. 1.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java EE Stateful Session Bean (EJB) example\" \/>\n<meta property=\"og:description\" content=\"In this article we are going to see how you can use Stateful Session Beans to keep track of state across a client Session in a simple Web Application. 1.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2013-08-09T12:03:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-09-26T12:09:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Nikos Maravitsas\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nikos Maravitsas\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html\"},\"author\":{\"name\":\"Nikos Maravitsas\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/aca65ecdda8de63ce629d2754da61f11\"},\"headline\":\"Java EE Stateful Session Bean (EJB) example\",\"datePublished\":\"2013-08-09T12:03:16+00:00\",\"dateModified\":\"2018-09-26T12:09:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html\"},\"wordCount\":1083,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"EJB\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html\",\"name\":\"Java EE Stateful Session Bean (EJB) example\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2013-08-09T12:03:16+00:00\",\"dateModified\":\"2018-09-26T12:09:21+00:00\",\"description\":\"In this article we are going to see how you can use Stateful Session Beans to keep track of state across a client Session in a simple Web Application. 1.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/java-ee-stateful-session-bean-ejb-example.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Java EE Stateful Session Bean (EJB) example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/aca65ecdda8de63ce629d2754da61f11\",\"name\":\"Nikos Maravitsas\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g\",\"caption\":\"Nikos Maravitsas\"},\"description\":\"Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens.\",\"sameAs\":[\"http:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/nikos-maravitsas\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java EE Stateful Session Bean (EJB) example","description":"In this article we are going to see how you can use Stateful Session Beans to keep track of state across a client Session in a simple Web Application. 1.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html","og_locale":"en_US","og_type":"article","og_title":"Java EE Stateful Session Bean (EJB) example","og_description":"In this article we are going to see how you can use Stateful Session Beans to keep track of state across a client Session in a simple Web Application. 1.","og_url":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-08-09T12:03:16+00:00","article_modified_time":"2018-09-26T12:09:21+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Nikos Maravitsas","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Nikos Maravitsas","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html"},"author":{"name":"Nikos Maravitsas","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/aca65ecdda8de63ce629d2754da61f11"},"headline":"Java EE Stateful Session Bean (EJB) example","datePublished":"2013-08-09T12:03:16+00:00","dateModified":"2018-09-26T12:09:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html"},"wordCount":1083,"commentCount":9,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["EJB"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html","url":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html","name":"Java EE Stateful Session Bean (EJB) example","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2013-08-09T12:03:16+00:00","dateModified":"2018-09-26T12:09:21+00:00","description":"In this article we are going to see how you can use Stateful Session Beans to keep track of state across a client Session in a simple Web Application. 1.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/java-ee-stateful-session-bean-ejb-example.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Java EE Stateful Session Bean (EJB) example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/aca65ecdda8de63ce629d2754da61f11","name":"Nikos Maravitsas","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g","caption":"Nikos Maravitsas"},"description":"Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/nikos-maravitsas"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/16223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=16223"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/16223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=16223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=16223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=16223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}