English 中文(简体)
Acessing远程/本地会话Bean
原标题:Acessing Remote/Local Session Beans

我正在尝试连接到会话bean。我尝试了许多与谷歌不同的方法,但我总是得到相同的错误,因为我的JNDI查找总是返回NULL。

错误如下:

java.lang.NullPointerException
at org.apache.jsp.index_jsp._jspService(index_jsp.java:85)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

所以,我可能缺少一些配置,但我是这方面的新手。帮个忙?

这是我的实体Bean:

 package beans;
 import java.util.HashSet;
 import java.util.Set;
 import javax.persistence.*;
 import org.hibernate.annotations.GenericGenerator;

@Entity
public class Album {

@Id
@Column(name="idalbum")
@GeneratedValue(generator="album_idalbum_seq")
@GenericGenerator(name="album_idalbum_seq", strategy = "increment")
private int id;

@Column(name="nomealbum")
private String nome;

@ManyToMany
  @JoinTable(name = "albumfaixas",
          joinColumns = { 
          @JoinColumn(name = "idalbum") 
          },
          inverseJoinColumns = {
          @JoinColumn(name="idfaixas")
          }
  )
private Set<Faixas> faixas = new HashSet<Faixas>();

public Album() {}
//gets and sets
}

我的会话Bean:

 package DAO;
 import org.hibernate.*;
 import sFactory.*;
 import java.util.List;
 import javax.ejb.Stateless;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 import beans.*;


 @Stateless 

 public class AlbumDAO  implements AlbumRemote { 

 @PersistenceContext 
  private EntityManager entityManager;

public List<Album> listaAlbuns() { ... code ... }
public void createAlbum(Album f) { ... code ... }
public void updateAlbum(Album f) { ... code ... }
public void deleteAlbum(Album f) { ... code ... }
 }

我的远程接口:

 package DAO;
 import javax.ejb.Remote;
 import java.util.List;
 import beans.Album;

 @Remote 

 public interface AlbumRemote {

public List<Album> listaAlbuns();
public void createAlbum(Album f);
public void updateAlbum(Album f);       
public void deleteAlbum(Album f);
 }

我的本地接口:

 package DAO;
 import javax.ejb.Local;
 import java.util.List;
 import beans.Album;

 @Local

 public interface AlbumLocal {

public List<Album> listaAlbuns();
public void createAlbum(Album f);
public void updateAlbum(Album f);   
public void deleteAlbum(Album f);
 }

web.xml中的引用:

   <ejb-ref>
     <ejb-ref-name>ejb/AlbumRemote</ejb-ref-name>
     <ejb-ref-type>Session</ejb-ref-type>
     <home>DAO.AlbumLocal</home>
     <remote>DAO.AlbumRemote</remote>
   </ejb-ref>

我的JSP客户端代码:

Context initCtx = new InitialContext();

Context ctx = (Context) initCtx.lookup("java:comp/env"); 

Object x = ctx.lookup("ejb/AlbumRemote");

AlbumRemote fr = (AlbumRemote)PortableRemoteObject.narrow(x, AlbumRemote.class);

fr.createAlbum(fx1);    

我使用myeclpse进行开发,使用Tomcat进行服务器

问题回答

你真的很接近。放入打开jb.war文件,此代码应该可以工作。

Tomcat本机不支持EJB,但使用OpenEJB可以很容易地做到这一点。事实上,我们现在正在调用此组合Apache TomEE,并且正在对Java EE 6 Web Profile进行认证。因此,它将跻身Glassfish、JBoss、Geronimo等行列。

如果您还没有,请确保您的应用程序中有一个WEB-INF/classes/META-INF/persistence.xml文件来定义您的持久性单元。





相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...

热门标签