日期:2014-05-20  浏览次数:20975 次

WEB访问EJB3.0 Stateful 的问题
写了一个有状态的会话bean 在main函数中访问没有问题,当我放到JSP中访问就出现问题了
下面是我的相关代码

远程接口
Java code

import javax.ejb.Remote;

@Remote
public interface CountNumber {
    public int getnum();
}



有状态Bean实现类
Java code

import javax.ejb.Stateful;
import com.thr.stateful.CountNumber;

public @Stateful class CountNumberBean implements CountNumber {
    private int k=1;
    public int getnum() {
        k++;
        return k ;
    }
}



如果在main函数中这样直接访问 

Java code


        Context cx=new InitialContext();
        CountNumber cn=(CountNumber)cx.lookup("CountNumberBean/remote");
        System.out.println(cn.getnum());
        Thread.sleep(2000);
        CountNumber cn2=(CountNumber)cx.lookup("CountNumberBean/remote");
        System.out.println(cn2.getnum());



就可以获得两个值,都是 2

但我想要在WEB应用中访问,就出错,这里是我的JSP页面
Java code

<%@ page language="java"  pageEncoding="ISO-8859-1"%>
<%@page import="com.thr.stateful.CountNumber"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.naming.InitialContext"%>
<html>
  <head>
    <title>My JSP</title>
  </head>
  <body>
    <%
        Context cx=new InitialContext();
        CountNumber cn=(CountNumber)session.getAttribute("cn");
        if(cn==null){
            cn=(CountNumber)cx.lookup("CountNumberBean/remote");
            session.setAttribute("cn",cn);
        }
      out.print(cn.getnum());
     %>
  </body>
</html>



下面是错误信息
控制台输出的错误信息

Java code

11:18:48,453 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: $Proxy222
    at org.apache.jsp.welcome_jsp._jspService(welcome_jsp.java:73)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
    at java.lang.Thread.run(Thread.java:595)