Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

EJB and JSP

  Date: Nov 24    Category: Java    Views: 483
  

I have a question about using EJBs in JSP. Orion App Server has a sample
called atm. In this sample application, there is an EJB (a session bean)
called accountManager. In each jsp file where this EJB is needed,
"<jsp:useBean id="accountManager" type="com.acme.atm.ejb.AccountManager"
scope="session"/>" is used. Isn't this strange? I mean I thought that
jsp:useBean is for Java Beans, not EJBs.

The only place that the above-mentioned EJB is located and created normally
is in an error page called errorAccountManager.jsp that is executed in
response to the exception "java.lang.InstantiationException". In this jsp,
the bean is created using JNDI and its home object with the help of the
ejb:useBean custom tag from the program's tag library.

I'd appreciate if someone can tell me:

1- How jsp:useBean can be used for EJBs.

2- What's the relation of that error page with other pages? I mean perhaps
the first time jsp:useBean is called for that session bean, an exception is
thrown and then the error page will actually create the bean using its
custom tag(ejb:useBean) and somehow pretend that it is a normal Java Bean.

Share: 

 

1 Answer Found

 
Answer #1    Answered On: Nov 24    

Strange, isn’t it? But it’s quite a creative and unusual case :o)

Ok, let me explain what’s going on:

First I want to note that "<jsp:useBean id="accountManager" type="com.acme.atm.ejb.AccountManager"
scope="session"/>" doesn’t mean “lookup for accountManager in scope session and if not found then instantiate one” but means “lookup for accountManager in scope session”. This is because “type” is used not “class”. So if you lookup something that doesn’t exist in session an InstantiationException will be thrown.

So what comes to your mind? “This accountManager thing should be create()-ed somewhere and put into the session”. Right? Well, a bit different but the same result. When you want to use atm, you first should login to it; there in login.jsp also a useBean as above exists. Because no accountManager exists yet, an InstantiationException is thrown. If you look at web.xml you see that when this exception occurs in a page, request is forwarded to errors/errorAccountManager.jsp. So the first time in login page you’re actually redirected to this error page and if you look at it, it’s there that the accountManager is create()-ed and put into session! Then you’re forwarded to index.jsp page, and because you’re not logged in you’re forwarded to login.jsp but this time an accountManager exists in session!

I hope it’s now clear for you how accoutnManager is created. Regarding ejb vs javabean question, ejb is a javabean, all java classes are beans, just put a getBlabla() in your class and the blabla attribute is accessible from outside world as an attribute of a javabean.

 
Didn't find what you were looking for? Find more on EJB and JSP Or get search suggestion and latest updates.




Tagged: