As7 HelloWorld Problem lookup Exception
leafjungle Jan 5, 2012 11:13 PMHello, I am a new commer to EJB 3.0 and Jboss.
I write the following code:
Header 1 | Header 2 |
---|---|
Cart.java ------------------------------------------------------- package com.lijun.ejb.cart;
import java.io.Serializable; import java.util.List;
public interface Cart extends Serializable { public void addItem(String productName);
public List<String> getALlItems(); } | CartBean.java ------------------------------------------------------------------------------------------------------------------------------------- package com.lijun.ejb.cart;
import java.util.ArrayList; import java.util.List;
import javax.ejb.Remote; import javax.ejb.Stateful;
@Stateful @Remote({Cart.class}) public class CartBean implements Cart { private static final long serialVersionUID = 3813488272549276156L; private List<String> itemList=new ArrayList<String>();
@Override public void addItem(String productName) { this.itemList.add(productName); }
@Override public List<String> getALlItems() { return this.itemList; }
} |
test.jsp ----------------------------------------------------------------------------------------------------------------------------------------------- <%@ page language="java" pageEncoding="UTF-8"%> <%@ page import="com.lijun.ejb.cart.*,javax.naming.*,java.util.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Hello World</title> </head> <body> <% out.println("welcome to beijing<br/>"); try{ out.println("no exception....<br/>"); InitialContext ctx=new InitialContext(); Cart cart=(Cart)session.getAttribute("cart"); if(cart==null){ out.println("cart is null, now create....<br/>"); cart=(CartBean)ctx.lookup("CartBean/remote"); session.setAttribute("cart",cart); } out.println("add to cart.....<br/>"); long x=Math.round(Math.random()*100); cart.addItem("C++ Primer "+x);
out.println("all item list......<br/>"); List<String> allItems=cart.getALlItems(); out.println("item list:<br/>"); int index=1; for(String name:allItems){ out.println(index+" "+name+"<br/>"); } }catch(Exception e){ out.println("exception...."); out.println(e.getMessage()); out.println(e.getStackTrace().toString()); } %> </body> </html> |
And I run JBosss 7:
http://localhost:8080/EjbProject3/test.jsp
The result is:
welcome to beijing
no exception....
cart is null, now create....
exception.... CartBean/remote -- service jboss.naming.context.java.CartBean.remote [Ljava.lang.StackTraceElement;@13b8479
It means that when lookup, exception occurs.
What's the problem?