RSS

Stateful Bean based on Remote Interface

07 Mar

CartInterface.java

package com.xavier.ejb3.sessionbean;

import java.util.List;

 

public interface CartInterface {

public List<String> getCart();

public void addCart(String name);

}

 

CartBean.java

package com.xavier.ejb3.sessionbean;

import java.util.ArrayList;

import java.util.List;

import javax.ejb.Remote;

import javax.ejb.Stateful;

 

@Stateful

@Remote(CartInterface.class)

public class CartBean implements CartInterface {

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

    public void addCart(String name) {

        this.cardList.add(name);

    }

 

    public List<String> getCart() {

return this.cardList;

    }

 

}

SessionBean.jsp

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

pageEncoding=“UTF-8”%>

<%@ page

import=“javax.naming.*,com.xavier.ejb3.sessionbean.*,com.xavier.ejb3.entitybean.*,java.util.*”%>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” http://www.w3.org/TR/html4/loose.dtd>

<html>

<head>

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

<title>Session Bean</title>

</head>

<body>

<%

try {

final Properties jndiProperties = new Properties();

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

jndiProperties.put(Context.URL_PKG_PREFIXES,

“org.jboss.ejb.client.naming”);

Context ctx = new InitialContext(jndiProperties);

CartInterface cart = (CartInterface) session

.getAttribute(“cart”);

if (cart == null) {

cart = (CartInterface) ctx

.lookup(“ejb:ejb3/ejb3//CartBean!com.xavier.ejb3.sessionbean.CartInterface?stateful”);

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.”);

}

%>

</body>

</html>

Advertisements
 
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:

WordPress.com Logo

You are commenting using your WordPress.com 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

 
%d bloggers like this: