aspose file tools*
The moose likes JSF and the fly likes How to call a JPA-Facade from a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to call a JPA-Facade from a JSP" Watch "How to call a JPA-Facade from a JSP" New topic
Author

How to call a JPA-Facade from a JSP

Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
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?


Docendo discimus (we learn by teaching)
SCJP 5 Zertifizierung, Vorbereitungsbuch; SCJP 6 Zertifizierung, Vorbereitungsbuch

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9390
    
    2

Are you aware of the MVC design pattern?? It's worth having a look at this pattern for having a solution to your problem.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why would you want to do this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
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

Joined: Jan 10, 2002
Posts: 61241
    
  66

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

Joined: Dec 19, 2007
Posts: 64
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

Joined: Dec 19, 2007
Posts: 64
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

Joined: Dec 19, 2007
Posts: 64
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

Terence Gronowski wrote:And JPA is pure MVC!


This is an utterly ridiculous thing to say.


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 19, 2007
Posts: 64
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

Joined: Jan 10, 2002
Posts: 61241
    
  66

Moved to the JSF forum.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16070
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to call a JPA-Facade from a JSP