Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP view MySQL  RSS feed

 
Ivan Antonovic
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First and foremost hello everyone, I am a new member of your community here.

I am learning java and I've problem connecting dots between servlet, JSP and JSTL. I do not know how to display data from database (MySQL) in JSP. I'll try to be succinct and unambiguous as I can.

I've DAO, beans, and controller packages. DAO has database logic (select, create, delete, remove), beans contains POJO User and Product and controller contains servlets.

Problem with this is that I do not know how to display products from database over servlet (using requestDispatcher) to JSP. I don't want to use JSTL and EL to write again "SELECT * FROM products" which I have already done in DAO.
I don't want this :



If I do it this way isn't then DAO redundant? I know that DAO must be a part of MVC so redundancy is not an option. I would like for a servlet to do everything and JSP only to display products using EL.
So my question is: how to display products from database without using jstl/sql? I am assuming I should use bean properties and session.setAttribute somehow but I am at loss how to do it.

Could someone clarify it for me please?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps this article will help.
 
Ivan Antonovic
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read it before posting my enquiry. It's a good article but it doesn't give me what I'm looking for.

It's like I'm missing some important piece of a puzzle to have a complete picture.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to let us know what that piece is.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With regards to the sql tags, from the docs (this javaee 5, but still applies):
"
The JSTL SQL tags for accessing databases ... are designed for quick prototyping and simple applications.
"

If you are learning how all this fits together then the sql tags are not something you should be looking into.
 
Ivan Antonovic
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From DAO package:


JSP form:


Servlet:



showProduct.jsp


I figured out how to display all product using a arraylist and a setAttribute but with this I have no idea what to do.
I get a null pointer exception when I run this. I am at a loss what to put in my servlet so JSP can display data.
Can someone help me with this?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see where in your controller you are using setAttribute to create a scoped variable in request scope.

Let's say that you end up with a List in variable productList. You would create a scoped variable as as such:
Then, in the JSP, you would access the list via ${products}, and use the <c:forEach> JSTL tag to iterate over the list.
 
Ivan Antonovic
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my DAO class returns product not a list. Can't I display a product without using a list?
I tried it with a list but still got null pointer exception. Maybe my form isn't good?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to display one product or many?

If one, the pattern is the same. This isn't as hard as you are making it out to be.

If you have the product in a variable named product, you'd create the scoped variable as
and access it in the JSP as ${product}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!