Stateful Bean based on Remote Interface

07 Mar

package com.xavier.ejb3.sessionbean;

import java.util.List;


public interface CartInterface {

public List<String> getCart();

public void addCart(String name);


package com.xavier.ejb3.sessionbean;

import java.util.ArrayList;

import java.util.List;

import javax.ejb.Remote;

import javax.ejb.Stateful;




public class CartBean implements CartInterface {

private List<String> cardList = new ArrayList<String>();

    public void addCart(String name) {




    public List<String> getCart() {

return this.cardList;





<%@ page language=“java” contentType=“text/html; charset=UTF-8”


<%@ page


<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>



<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Session Bean</title>




try {

final Properties jndiProperties = new Properties();

jndiProperties.put(Context.PROVIDER_URL, “localhost:1099”);



Context ctx = new InitialContext(jndiProperties);

CartInterface cart = (CartInterface) session


if (cart == null) {

cart = (CartInterface) ctx


session.setAttribute(“cart”, cart);


cart.addCart(“Mastering EJB 3.0”);

cart.addCart(“Mastering Hibernate”);

List<String> items = cart.getCart();

for (String name : items) {

out.println(name + “<br>”);


} catch (Exception e) {

out.println(“<br>Invoke failed.”);





Leave a comment

Posted by on 03/07/2012 in EJB


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: