標籤:
EJB本地調用方式:把Web用戶端和EJB服務端部署到同一個JBoss,用戶端和伺服器通過一個JVM進行通訊。
Web用戶端本地調用時,需引用EJB服務端打包的jar,不需引用JBoss/client下的jar和src下的jndi.properties檔案,調用方式如下:
<%@ page language="java"import="java.util.*,javax.naming.*,com.tgb.ejb.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title></head><body><%InitialContext ctx = new InitialContext();UserManager usermanager = (UserManager) ctx.lookup("UserManagerBean/local");User user = new User();user.setUsername("quwenzhe");user.setPassword("quwenzhe123");usermanager.addUser(user);out.println("userId=" + user.getId());%></body></html>
在瀏覽器輸入用戶端發布的URL地址,JBoss會報如下異常:
13:02:30,924 ERROR [[jsp]] Servlet.service() for servlet jsp threw exceptionjava.lang.ClassCastException: $Proxy96 cannot be cast to com.tgb.ejb.UserManagerat org.apache.jsp.index_jsp._jspService(index_jsp.java:79)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:182)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:619)
解決方案為:在JBoss伺服器的jboss-4.2.3.GA\server\default\deploy\jboss-web.deployer\META-INF路徑下找到jboss-service.xml檔案,把<attribute name="UseJBossWebLoader">false</attribute>的false改為true,重啟JBoss,問題解決。
UseJBossWebLoader屬性設定為true:表示Tomcat使用JBoss的統一類載入器作為Web應用的類載入器,這意味著WAR包中WEB-INF/classes和WEB-INF/lib目錄裡的類將被載入到預設的共用類載入器倉庫中。通過它,可以實作類別和資源在Web應用之間共用。但是,如果這不是你想要的情況,可以通過將該屬性設定為false來避免這種行為。
通常情況下應該避免使用這種情況,因為當不同的應用中擁有同樣的類和資源的時候,JBoss不同應用之間會產生衝突;舉例如下:兩個war應用 WarA,WarB下面均有com.tgb.User.class檔案;現在期望每個War包下面分別用各自的User.class檔案,當修改一個WarA的應用時候,tomcat會重新部署以下應用。這個時候,就會出現WarA應用使用WarB下面的User.class,造成混亂。
希望我的講解能夠協助大家進步。
解決EJB本地調用報“java.lang.ClassCastException: $Proxy96 cannot be cast to com.tgb.ejb.UserManager”異常問題