• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

servlets and database

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it okay to have database connectivity and data retrieval code inside a servlet or should it always be in a model class i.e. a simple java class ?
 
Sheriff
Posts: 67530
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
In my opinon, database code has no place anywhere in the UI tier.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally we have it in a xml file like we do in Hibernate. This is the best practices today.
 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
In my opinon, database code has no place anywhere in the UI tier.



Does servlet come into UI tier??? :roll:
[ April 27, 2007: Message edited by: ankur rathi ]
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Normally we have it in a xml file like we do in Hibernate. This is the best practices today.



Jothi, XML file can have configuration parameters but you will need �code� to use those information and get data from database, I guess.
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Poonam Kadu:
Is it okay to have database connectivity and data retrieval code inside a servlet or should it always be in a model class i.e. a simple java class ?



IMO, Tomcat has some �connection pooling� thing else you can create connection pool yourselves, but definitely not in servlet.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlets should be used for delegating requests to processing classes and the business logic should be outside the servlets.You can configure database connection pool in the container and the business logic can make use of it whenever it needs database connections.


Originally posted by ankur rathi:


Does servlet come into UI tier??? :roll:



Though servlets are capable of rendering html but it should be used for controlling and delegating , not for generating views for users , thats the purpose of jsp.
 
Bear Bibeault
Sheriff
Posts: 67530
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

Originally posted by ankur rathi:

Does servlet come into UI tier??? :roll:



Yes, servlets are part of the UI tier. You are of the opinion that they are part of the model?
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:


Yes, servlets are part of the UI tier. You are of the opinion that they are part of the model?



No, my opinion is, (typically) servlet acts as a controller between views and action classes.
 
Bear Bibeault
Sheriff
Posts: 67530
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

Originally posted by ankur rathi:


No, my opinion is, (typically) servlet acts as a controller between views and action classes.



Which together comprise the UI tier.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:
Though servlets are capable of rendering html but it should be used for controlling and delegating , not for generating views for users , thats the purpose of jsp.



Following this discussion, I have couple of questions.

is it a good way to store database connection object in application context attribute, so connection object will be available for all?

I implements ServletContextListener to instantiate db connection, get db url and class name (for driver manager) from context-param in web.xml. again is it a correct way?

any advice welcome. thanks all.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anthony Karta:
is it a good way to store database connection object in application context attribute, so connection object will be available for all?
I implements ServletContextListener to instantiate db connection, get db url and class name (for driver manager) from context-param in web.xml. again is it a correct way?



Only one thread should be working with one connection object at a time.Binding a connection object to context is bad idea.
Why do not you make a connection pool at the server level and associate it with JNDI.So connection will be taken care by the pool and will be available for all.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic