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 exception java.lang.ClassCastException: $Proxy96 cannot be cast to com.tgb.ejb.UserManager at 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,造成混乱。
希望我的讲解能够帮助大家进步。