• Post Reply Bookmark Topic Watch Topic
  • New Topic

servlets and database

 
Poonam Kadu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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 ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinon, database code has no place anywhere in the UI tier.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally we have it in a xml file like we do in Hibernate. This is the best practices today.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • 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: 3830
  • Mark post as helpful
  • send pies
  • 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: 3830
  • Mark post as helpful
  • send pies
  • 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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 3830
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!