• Post Reply Bookmark Topic Watch Topic
  • New Topic

Connexion database in jsp page  RSS feed

 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your opinion, Is it possible to connect to the postgresql database in a jsp page?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possible - yes. Advisable - no. Any kind of I/O handling (including DB connections) should not happen in the view layer - which is what JSPs are.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, i must use a servlet for connexion or a simple class java ?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a question of "must", it's a question of whether or not you want to implement a good design. While you could establish the connection in a JSP backing bean class, the generally accepted practice these days is to use the MVC pattern for web apps. DB connections would be part of the "C", so yes - all the DB stuff would be done in a servlet.

See http://www.coderanch.com/how-to/java/Model1Model2MVC and http://www.javaranch.com/journal/200603/frontman.html for more detail on that.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i want to use the MVC pattern.
I will see these link,
thank you
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, you shouldn't even be connecting to a database in a servlet. Servlets and JSP are part of the presentation layer. DB activity should all happen in the model.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to do those below:
 firstly connected to the database,
and then retrieve the list of categories found in the database,
and display the list of categories in a jsp page in a table.
How can i do it?
thank you in advance
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a model class that knows nothing of JSP or servlets or anything else to do with presentation. This class should yield a list of using normal collection classes, not a result set.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear Bibeault :Sorry, I have not understood what you say.
In my opinion, the connection should be done at the servlet and the output done at a jsp page is not it?

 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do this:
code servlet:

And there's a part of my jsp page:

But an error appears:

Thank you in advance
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarra Sakka wrote:@Bear Bibeault :Sorry, I have not understood what you say.
In my opinion, the connection should be done at the servlet and the output done at a jsp page is not it?

No, it is not. Servlets and JSP are part of the presentation layer. Database access should not be done in this layer. The model layer is the proper place for database access.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean the data base should be placed in a model like JavaBean ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, just a class. It doesn't need to be a bean. They key concept is that it should not depend on anything to do with the UI, and vice versa.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, no. If it's going to be used in a web app, there would be no main.

Let's say that you want to get a list of Wazoos from the DB. Whatever pattern you use (DAO, Rich Entity, etc) you'd like need a method such as:

You might want to do some research on concepts such as DAO (though I'm not a fan) and other model concepts.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to thank you for your reply.
Then,i want to see that i found a solution but not the one you mentioned.
I use Servlet for database connexion:

and here's a part of jsp code :

With this servlet and jsp i get what i want, do you think that I chose the wrong method ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have chosen some very very bad approaches.

Grabbing a connection in a servlet is bad idea.
Grabbing a connection when the servlet initializes and holding onto it until the servlet is destroyed is a monstrously bad idea.
Using Java code in a JSP is a bad idea.

If you are not going to follow the advice given to you, we cannot offer much help.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, i want to follow your advice :)
So, i should begin with the database connexion in a simple class Java is not it?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!