• 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

Servlet, JSP, MySQL webb app

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am studying Servlet and JSP from Head First Servlet & JSP.
I completed the 6th chapter, and now I would like to write a simple application that uses the concepts I have been studying until now.

My idea is to create something that uses sessions, servlet context attributes, MVC with JSP etc...I need to see all this stuff in 'action'.

I understood the tutorial about MVC, I also read that I could use a servlet context attribute to store a database connection available to every servlet in the web app, I am thinking of using the advice in the book and use a session listener to syncronize the data in the database. Is this what you do in "real life" ?

Any suggestion will be really appreciated.

Thank you
Giovanni
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben gave me a nice reference:
http://simple.souther.us
Look for SimpleMVC.
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
I actually would like to know if setting a database connection as a context attribute is the "right" way to provide database access to the web app, or should I do it with JSP?

Giovanni
[ May 09, 2005: Message edited by: Giovanni De Stefano ]
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not? Unless you want to use something heavier like obtaining datasource using JNDI.
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been reading a lot of things until now and it seems to be a better idea to have a Java class that acesses the database (this java code can be stored as a class attribute in the context), a servlet that uses this class attribute, and JSP only to show the info (no access whatsoever!).

I hope I got it right!
Thank you...

Giovanni
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Giovanni De Stefano:
Thanks,
I actually would like to know if setting a database connection as a context attribute is the "right" way to provide database access to the web app, or should I do it with JSP?

Giovanni

[ May 09, 2005: Message edited by: Giovanni De Stefano ]



For real world apps this is not the norm. Having all requests queue up for a single connection, would create a bottleneck for anything but the most trivial of apps.

Most containers come with some kind of built in connection pooling capablity. Tomcat uses the Jakarta Commons DBCP
http://jakarta.apache.org/commons/dbcp
vi JNDI http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-resources-howto.html

If you don't want to configure pooling at the container level, you can also embed DBCP in your own webapp.
[ May 09, 2005: Message edited by: Ben Souther ]
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Accordingly last research(?) connection pooling is quite obsolete since creation connection takes much less time than it was before. From other side smart JDBC driver can orginize internal connection pooling. Actually as only you choose connection pooling you get a lot of parameters to configure and you do not really may have a clue how. For a not very complex web app I'd store database connection parameters in a static variable. As we just figured it's quite reasonable since context data do not replicate accross clusters, but very unlikely a servlet container will use a separate class loader for every instance of the same servlet, and even in this case you should be safe.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by D Rog:
Accordingly last research(?) connection pooling is quite obsolete since creation connection takes much less time than it was before. From other side smart JDBC driver can orginize internal connection pooling. Actually as only you choose connection pooling you get a lot of parameters to configure and you do not really may have a clue how. For a not very complex web app I'd store database connection parameters in a static variable. As we just figured it's quite reasonable since context data do not replicate accross clusters, but very unlikely a servlet container will use a separate class loader for every instance of the same servlet, and even in this case you should be safe.



I'd like to read the results of this research.
If you have any links, I would grateful, if you would post them.
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all guys!

I understand your different point of views, and I invite your to keep talking about this further.

I just completed my little app to connect to MySQL Server in order to retrieve, update or whatever user info.

From Head First Servlet & JSP, page 54: the model is the only part of the system that talks to the database...

I am going to follow this suggestion an implement my little MVC web app with the model the only component that has access to the database...

Thank you
Giovanni
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. Ok, when I did a little research asked by Mr. Ben I found some link for MySQL you can find interesting:
http://dev.mysql.com/tech-resources/articles/connection_pooling_with_connectorj.html
Ok, let's return to our discussion. Unfortunately I didn't find an original article I refered to, anyway I found some other interesting study link:
http://csajsp-chapters.corewebprogramming.com/CSAJSP-Chapter18.pdf
You can notice that then faster network you have then less difference of pooled and not pooled implementation. Notice that this study was done for Oracle. If you consider Derby, when you have DB running on the same VM in most cases, then you can not notice any performance degradation. So, a short conclusion can be that before making decision of value of using pool, you may look in particular JDBC implementation you are going to use. Of course for main stream develpoers there is only one way - connection pool.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
J Rog,

Both of these links seem to encourage connection pooling.
It's funny the second one is from "Core Web Programming". The chapter is exactly the same as the JDBC chapters in "Core Servlets and Java Server Pages" (great book). I guess the author really believes in reuse.

In particular, look at Section 18.8. In it the author builds a webpage with 20 frames all pointing to the same servlet. He first runs this test without any pooling (new connection for each request). He then tries with a connection pool containing only 1 connection (a condition similar to the example in the Head First book) and sees an improvement. Finally, he increases the pool to a max of 50 connections and gets the best performance of the 3 cases. The difference between the 3 cases was more drastic as the connection between the webserver and the database server got slower.

I still maintain that connection pooling is the way to go for anything other than really low traffic apps.

Thank you for the links though,
-Ben
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes you really cant make with just one connection. Because some transactions needs change in the properties of connection object. And in the real world apps, most of the time, you need to serve all requests simultaneously.
[ May 10, 2005: Message edited by: Adeel Ansari ]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Giovanni De Stefano:

From Head First Servlet & JSP, page 54: the model is the only part of the system that talks to the database...

/QUOTE]

Mmmm, well, here I do not agree. I've seen web applications where the JSP takes care of the database connection, the servlet, or the DAO or Data Access Object. But never the model, as I see it. The model is just the collection of javabeans which reflects the concepts known to the user like Client, Product, Order, Orderline and so on.
In my opinion, the best way to follow the MVC model 2 is to provide DAO's or Data Acces Objects which connect to the database and execute the SQL statements.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gert Cuppens:

Mmmm, well, here I do not agree. I've seen web applications where the JSP takes care of the database connection, the servlet, or the DAO or Data Access Object.


I've seen apps with everything written in the JSPs that doesn't make it a good design and it certainly isn't MVC.


But never the model, as I see it. The model is just the collection of javabeans which reflects the concepts known to the user like Client, Product, Order, Orderline and so on.

Those are part of the model. The DAOs would also be part of the model or
if you've built a whole data access tier, you would communicate with that tier via your model's command or helper beans -- returning the transportObjects that you've already mentioned.

In my opinion, the best way to follow the MVC model 2 is to provide DAO's or Data Acces Objects which connect to the database and execute the SQL statements.

I agree completely.
But I consider those objects to be part of the model.

A rule of thumb that I follow is:
Never have to import java.sql from a servlet or JSP
and
Never have to import javax.servlet from a bean.
(There is an exception to this.. Some people create beans for processing and validating their HTTP forms. They just pass the whole request object to them. In this case my rule would be not to import both javax.servlet.* and java.sql in the same bean.).

If I need to make those imports, then I know it's time to revisit my design.

Also, all of my helper beans in the model have main methods so they can be tested and debugged from the command line. Once I have these working and my tests pass, then I worry about writing servlets and JSPs.
[ May 15, 2005: Message edited by: Ben Souther ]
 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic