Forums Register Login

How to call a JPA-Facade from a JSP

+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
Are you aware of the MVC design pattern?? It's worth having a look at this pattern for having a solution to your problem.
+Pie Number of slices to send: Send
Why would you want to do this?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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??





+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
 

Terence Gronowski wrote:And JPA is pure MVC!



This is an utterly ridiculous thing to say.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.


+Pie Number of slices to send: Send
Moved to the JSF forum.
+Pie Number of slices to send: Send
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.
You can't expect to wield supreme executive power just because
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 9430 times.
Similar Threads
Need help with a servlet problem
Problem with sessions: Always getting old session id.
If-Else In JSTL
glassfish auto-generates unwanted session
How to return ResultSet from Bean to JSP?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:50:43.