This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JSP view MySQL  RSS feed

 
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?

 
Marshal
Posts: 67226
169
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
Marshal
Posts: 67226
169
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.
 
Master Rancher
Posts: 3887
45
  • 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
Marshal
Posts: 67226
169
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
Marshal
Posts: 67226
169
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}
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!