• 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

JSP MVC JDBC

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shepujw, welcome to JavaRanch. Please check your private messages. You can see them by clicking My Private Messages.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic