• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database output in JSP

 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I'm studying JSP on my own and I'm getting into lots of trouble!

Here is what my small application does.
index.jsp -> servlet -> result.jsp

1. I get values (say, no: of products) from index.jsp page.
2. The servlet creates a connection with a DB table to update the no: of products. Database is updated.
3. In result.jsp I would like to print the products, their quantity and the cost (no: of products selected * cost of each product)

My problem is with 3rd task.

I was successful in creating Connection object as a session attribute in servlet. Then I used scriptlets to obtain the connection object in jsp page.
This model is no more used. What is the appropriate way to use DB connections in JSP?

My friend says you could pull all data from DB in servlet and use that data in a attribute to be accessed in a JSP. But when I google, i found an example where they create a whole new connection within the JSP page itself and print values from table using JSTL.

Please suggest me the appropriate JDBC model to be used.

Regards,
John
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't an appropriate way of updating a database from a JSP, not in 2011 anyway. Sure, you can find examples of anything on the internet, but it just isn't a good practice any more.

And neither is using a database connection per session, which is what you did when you put a connection into your session.

So here's what you should do. You want your JSP to output a list of product data, where product data comprises something like description, quantity, and cost? Then first create a Product bean which has those three things as properties. Second, your JDBC processing should create a List of Product objects; an ArrayList would be suitable. Put that List into request scope and then forward to the JSP.

Then your JSP would use <c:forEach> to iterate through the List and it would use the EL to extract the properties from each of the beans and output them. Rough example:
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Listen to Paul, for he is wise. Scriptlets in a JSP, in 2011? Completely irresponsible!

Your DB access should't even be in a servelt -- it should be in UI-agnostic model classes.

Please read this article for information on properly structuring web applications.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Paul
Thank you. That was greatly helpful.

@ Bear
Awesome article.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic