• Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB3- resource injection in JSP

 
Joe Khan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a resource like @EJB, be injected to a JSP page?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,
No. It can be injected into a servlet, not a JSP.

The reason is that good Model View Controller design says the JSP should only be for presentation/view. Having logic in a JSP is generally accepted to be poor practice. An EJB is certainly business logic.
 
Joe Khan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Jeanne.
I'm a little confused here. I think business logic is performed in the real EJB which runs in a EJB container. Servlets are not meant to perform business logic too.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,
You are correct that the business logic is in the EJB. I guess I meant the caller of the business logic.

Basically, a servlet handles control and sits between the view and the business logic. A JSP just handles presentation and is the view. It shouldn't care where the data comes from. It could come from an EJB, a web service, magic, etc.
 
Joe Khan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne,
I got your point. But I'm still wondering why resource injection is not allowed but 'InitialContext().lookup("xyz")' is allowed to find a bean, in a JSP page.
sorry If I'm bugging you or something.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Khan:
But I'm still wondering why resource injection is not allowed but 'InitialContext().lookup("xyz")' is allowed to find a bean, in a JSP page.

While discouraged, the JSP spec allows any code to be run in a scriplet. It can't change this now or JSPs won't be backward compatible. Someone could have a JSP 1 file around that has all sorts of stuff in it. When J2EE 5 was written, the practice of leaving such code out of the JSP was well established. Since they didn't need to worry about backward compatibility (nobody was using resource injection as it is new), they decided not to add it.


sorry If I'm bugging you or something.

You're not . It's good to ask follow ups as it helps clarify the answer. It also helps me learn to better formulate replies in the first place!
 
Joe Khan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That explains it. I was thinking as JSP is a a high-level abstraction of servlets they are basically same. Sure JSP Model 2 helps effectively separating presentation from content. In JSP Model 1 , the JSP page alone is responsible for processing the incoming request and replying back to the client; though this model is suitable for simple applications. The latest specifications still suggest us to use model 1 for simple appications, so I think Sun people should give us flexibility to use all those features to JSP pages which are available in Servlets. What do yu think?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Khan:
The latest specifications still suggest us to use model 1 for simple appications, so I think Sun people should give us flexibility to use all those features to JSP pages which are available in Servlets. What do yu think?

I think they should stop suggesting this to people!
 
Joe Khan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok..
Anyways, thanks Jeanne for all your help and have a nice long weekend.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!