File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes JSP MVC JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JSP MVC JDBC" Watch "JSP MVC JDBC" New topic


Bob Smith

Joined: Sep 10, 2008
Posts: 12
I'm somewhat new to Java. I have been reading on Servlet's & JSP's for awhile, but can't seem to find answers to some questions.

When developing a Web app intended to be database driven, what is the best practice for handing connections to the DB? Should I put the DB Connection in the web.xml file? Should it go in a .properties file?

With MVC, where do I connect to the DB to do the work, is it in the model?

With MVC, how many controller files do you have? Is it one for everything? What is the best practice?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

shepujw, welcome to JavaRanch. Please check your private messages. You can see them by clicking My Private Messages.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vihung Marathe

Joined: Apr 24, 2006
Posts: 7
You have not mentioned which MVC framework and which data access mechanism you intend to use. The basic Servlet and JSP spec do not mandate an MVC pattern - in fact there really isn't an officially recommended MVC framework for Java. Servlets and JSP also say nothing about JDBC or data access.

There are many ways to write web applications using Java. They range from JSP-based applications where EVERYTHING - including JDBC code to access the database and SQL - can be written in JSPs as inline Java and using various tag libraries (you should never ever do this, but it is technically possible!) - all the way to enterprise (JEE) applications with business logic encapsulated in EJB (Enterprise JavaBeans), messaging, routing and transformation fronted by a component based UI framework like JavaServer Faces.

While there is no 'true' Java way to do things, the usual way is to use an MVC framework - such as Spring MVC, Struts or JSF for your UI (presentation and basic user interaction). The UI layer delegates to a business logic layer - either POJOs hosted in a container such as the Spring Framework, or EJBs or something similar. The business logic layer would usually use some sort of abstraction for accessing data from a database - something like an ORM (Object-Relational Mapping) framework like Hibernate, or something like Entity EJBs. The data abstraction layer would in turn use data sources defined and configured in the container - either a web container (e.g. Tomcat etc.) or an application container (eg. JBoss, WebLogic, WebSphere etc.)

While this may sound like a lot of overhead, it is often the most efficient (in terms of developer productivity). It is often the best way organise things for sclability also.

I often find that the quickest way to get started is to use Spring, Hibernate and Apache Commons DBCP all running as a web application (i.e. a WAR) in Tomcat.

First you define the Spring Application Context in the web.xml (using the Context Listener) pointing it to a Spring XML configuration file.

Then, for the Data Access

1. You instantiate (i.e. define in the XML configuration file) the Hibernate Session Factory (using Spring's implementation of LocalSessionFactoryBean).

2. You define an instance of the Apache Commons BasicDataSource in the Spring context - you can start with username, password, JDBC URL etc. all hardcoded in the Spring XML file, and then move it out later if you wish.

3. You point the Session Factory to the data source.

That is all your basic data access wiring done. Now you can use the Hibernate Session Factory in your business logic beans. Of course, the devil is in the details - you will need to make sure you have packaged your configuration files for both Spring and Hibernate in the right place. You will also have to make sure that you have all the third party library dependencies in your classpath

If you write it well, you should be able to scale this kind of application to any target platform. You could retool your business logic into EJBs to deploy on to an application server platform. You can easily change your data sources to be looked up via JNDI and retrieved from the application container.

You can use the same application context for your MVC - I won't go into the details, but there is plenty of documentation for using Spring MVC available
Vihung Marathe

Joined: Apr 24, 2006
Posts: 7
I did not answer your MVC questions

With MVC, where do I connect to the DB to do the work, is it in the model?

Yes and no. Remember, with true MVC, the M is not your data model (i.e. your database), but your UI model. The model holds state that is relevant to the view. The operations on a model reflect what is relevant to the UI, not neccesarily to the business flow. The model should delegate operations to a business logic layer and update its state accordingly. It is the business logic layer that should in turn access the data in the database.

Usually the model is stateful and the business logic is stateless.

If your application is quite light, and you are not neccesarily working towards a high level of reuse, and therefore do not need a high level of layering or abstraction, you can put all your business logic in the model. In this case, the model would access the DB layer.

With MVC, how many controller files do you have? Is it one for everything? What is the best practice?

What do you mean by controller files?
Bob Smith

Joined: Sep 10, 2008
Posts: 12
For the controller files, I am talking about the controller in MVC.

This is what I hate about Java. There is no simple answer to anything, or maybe I just don't know how to ask a the questions yet.

What if I am not using any framework (no Struts, no Spring, no Hibernate) and I just want a simple MVC web page. How many controller files do I have? One to control everything?

What is the best practice for connecting to a DB when making a web app such as this?
subject: JSP MVC JDBC
Similar Threads
Finished Studing HFJS ,require guidance
Question on DAO
Configuration GUI &
NX: Hardcode or using getHeaderNames?
Too Many Client Tasks