Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call a JPA-Facade from a JSP

 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In theory it should be possible, to access a database over JPA from a JSP.

I created an entity (or better a JPA-Bean) and the corresponding Facade with Netbeans 6.8.

Now, as I want to call some methods of the facade there is only a null pointer exception.

For test reasons, I declared a member variable in the Facade-Bean. It is no problem to read it!




Can it be, that it is not possible to call an EJB from a JSP?

Somebody has an idea of combining jsp with jpa?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you aware of the MVC design pattern?? It's worth having a look at this pattern for having a solution to your problem.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to do this?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm, yeah, I agree with David. Not a good way to approach a web application.

Maybe you should read this article which addresses modern web application structure.
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey , this approach is MVC, so I think at least!

Why I do that? Just Curiosity!

Some clarifications: The program is made with Netbeans 6.8 and JPA 2.0, Glassfish v3. The thing is, if I use a Servlet or a Facelet the whole story works perfect, but not with a JSP (I will show how it works with a Facelet/Servlet in the next post).

As far as I see its perfect MVC, in any case it is a many tier architecture:

1. Database-Table (CUSTOMER of the demo database of Glassfish) and corresponding JPA-Bean (Customer.java) => Model
2. Facade-Bean (CustomerFacade.java) => Controller
3. JSP (index.jsp) => View

If I run it with the call to count() a NullPointerExceptin results.

I suppose that the call of a bean by a JSP is somewhat different from a call by a Servlet or Facelet. Or JSP is too old to work together with JPA? Something is funny, I can nowhere find the working togehter of a JSP with JPA, neither in the recent literature nor in the web. JSP is always with JDBC.

Of course I would solve this problem with a Facelet or a Servlet. But it should work with a JSP, too. I just wonder why it does not work. The funny thing is, that I can read the own member variable I declared in the controller bean (CustomerFacade.java). But when I call the method count() it does not. Count() should return the count of rows in the table Customer.

By the way such comments like "no good idea" are useless! I do not expect a solution, but a guess would be good as well.

I'll just try with a Servlet and report.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terence Gronowski wrote:Hey , this approach is MVC, so I think at least!

You think incorrectly. Performing database access in a JSP is as far from MVC as you can get.

But hey, it's your rope.
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried with a servlet, the call to the CustomerFacade bean works well!


Here the code with the call (See lines 3/4 and 25, bold marking does not work):




There must be a fundamental difference between the call of a bean in a Servlet



and the call in a JSP




The Annotation @EJB lacks in the second call.

Question: Is there a possibility to call an enterprise Bean in a JSP? Can I somehow include the annotation?

What is the difference of a "normal" bean to an "enterprise" bean? Or is there any difference??





 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, I disagree, it is not database access in JSP, it is database access in JPA with a JSP view!

And JPA is pure MVC!

again:

1. Database-Table (CUSTOMER of the demo database of Glassfish) and corresponding JPA-Bean (Customer.java) => Model
2. Facade-Bean (CustomerFacade.java) => Controller
3. Servlet/Facelet/JSP (does not work, but it is a view!!!) => View

The problem ist not MVC or not, but the call of an enterprise java bean in a JSP. It might be, that a call to an enerprise java bean is impossible in a JSP.
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I found the answer

http://www.esus.com/javaindex/j2ee/javajsp/jspejb.html

The call is rather lengthy, may be that's wy you do not find this call hardly anywhere!
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terence Gronowski wrote:And JPA is pure MVC!


This is an utterly ridiculous thing to say.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terence Gronowski wrote:It might be, that a call to an enerprise java bean is impossible in a JSP.


I wish it were impossible. It would have saved you a ton of time you just wasted learning how to do something incorrectly.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terence Gronowski wrote:The call is rather lengthy, may be that's wy you do not find this call hardly anywhere!

Nah, I think it's because it's just a really bad idea.
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling a EJB with JSF is possible but clumbsy, as there should not be too much code in a View it should be avoided.

To do it correctly one has to introduce another bean like the JSF-Managed Bean in Java ServerFaces.

I forgot to mention the JSF-Managed Bean which is used using JSF (2a):

1. Database-Table (CUSTOMER of the demo database of Glassfish) and corresponding JPA-Bean (Customer.java) => Model
2. Facade-Bean (CustomerFacade.java) => Controller or Model?
2a. JSF-Managed bean (backed bean) => Controller
3. Facelet => View

The sequence in JSF

1. DatabaseTable (Model)
2. JPA-Bean (Model)
3. FacadeBean (Model? or Controller?)
4. JSF-Managed Bean (Controller)
5. Facelet (View)

is MVC. If you replace the Facelet by a JSP is still MVC.

If a good idea or not, it is completely legal to use a JSP instead of a Facelet. To call the EJB directly is not a good idea as the View is mixed with the Controller.


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JSF forum.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18214
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP is not JSF. Keep that in mind, since if you allow yourself to blur the distinctions, it will cause trouble. Just because a JSF view can be a ".jsp" file doesn't mean it's the same thing.

In fact, I prefer to avoid both terms except when referring to concrete objects and just use the term "View". As in Model/View/Controller.

Nowhere in MVC is it ever stated that the Model has to the the actual target object. In many cases, the model serves as a fa├žade, decorator, or even aggregator to the actual data objects (the Data Model, when using JPA). This permits decoupling of the UI from the datamode and allows the insertion of extra capabilities to the control process.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic