WEB访问EJB3.0 有状态bean 的问题
写了一个有状态的会话bean 在main函数中访问没有问题,当我放到JSP中访问就出现问题了
下面是我的相关代码
远程接口
import javax.ejb.Remote;@Remotepublic interface CountNumber { public int getnum();}
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 ; }}
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());
<%@ 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>
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)
org.apache.jasper.JasperException: An exception occurred processing JSP page /welcome.jsp at line 1512: Context cx=new InitialContext();13: CountNumber cn=(CountNumber)session.getAttribute("cn");14: if(cn==null){15: cn=(CountNumber)cx.lookup("CountNumberBean/remote");16: session.setAttribute("cn",cn);17: }18: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:415) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)root causejava.lang.ClassCastException: $Proxy222 org.apache.jsp.welcome_jsp._jspService(welcome_jsp.java:73) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)note The full stack trace of the root cause is available in the JBossWeb/2.0.1.GA logs.