When reading post at Javaranch the MVC2 approach is recommended for devloping webapplication. Is hard to debug and I often get the advise to test POJO, beans outside the servlet container.
My problem is the following. If I develop a connection pool and use my own objects to get and set the data in database and in beans how can I later implement this in the servlet environment? Can I add my own connection pool to the servlet container. I'm using Jakarta DBCP right now but I'm not using it in my application.
How could I implements that? If I use DBCP in Tomcat and test my classes outside the container. You can I without recompiling my classes move classes to the container?
Joined: Nov 29, 2005
Originally posted by Mathias Nilsson: How could I implements that?
Implement what ?
Is this your question: You want to test calsses which needs connection to communicate with database.Now you want to test those classes without an servlet environment and you want those classes to be used within the web application using the servers connection.
Say you are using JUnit for this stuff.You can create a manual connection and pass it to the classes which needs a connection and the tear down that when the test completes and this delegation should happen independently so that connection from application servers pool should be used with the classes are used from within application.
To be honest I really don't know. I have google around and read several articles on MVC2 front controller and so forth but I have trouble in getting the whole picture because there is never any example on how to implement.
This is what I have so far. I have a plain java class that have a method that returns a connection
The datasource is registered in the context.xml so it works in the container.
I have some classes that gets the data from the database and returns Vectors. an example below
The brand class is just a setter and getter class.
I use a servlet to call the BrandHandler and then set the attribute.
this forwards the request to the view a jsp that use JSTL to loop the data.
Is this the MVC architecture? I have the BrandHandler, Brand to act as the model. The servlet as the controller and the JSP as the view. Can you please explain how to get the Front controller, Command , Beans pattern to work in this environment?
I see you have jsp (view)<-> servlet (controller) <-> Brandhandler, looks good (MVC)to me.
Some remarks: -ConnectionHandler does the same jndi-lookup everytime, why not store it in a class variable? -at least log your SQL-Exceptions, don't just return null, it will cause a NullPointerException in BrandHandler (conn.close() -BrandHandler swallows SQLExceptions, please never do that! (you will never know what happened) -move the closing of rs and stmt in the finally block (like conn)
-if you wanted to add some business-logic to your Vector of Brands (maybe filter out some of the Brands, merge together the results of 2 getBrands(..) calls, you would probably want an extra class between servlet and BrandHandler
Herman PS I agree with you that learning how to develop applications like this in Java is hard.
Joined: Aug 21, 2004
-ConnectionHandler does the same jndi-lookup everytime, why not store it in a class variable?
Yes.... this is still in test mode... I think i will put it in a properties file. You are altso right about all the exception methods.
I was reading about the Struts. Is it better to use the struts framework here?
Joined: Jul 17, 2006
I've made webapplications that looked at lot like your code, then I started using the Struts-framework, and after that I started using Spring MVC as well.
Both made my code shorter and easier to read (for somebody who knows the framework that is)
I really like Spring because: -it made my JDBC-related code a lot smaller, simpler and better. -it makes your code easier to unit-test -if you want to add Aspect Oriented stuff(maybe for logging the beginning of a every method of a class): very easy to get started.
The Tiles part of Struts makes it easy to define templates for your webapplication, this can be very handy if a lot of your pages have the same header and menu, but only a different 'content'- part.
I learned Struts and Spring by reading books and articles, and then rebuilding a webapplication that I already had build.
Joined: Aug 21, 2004
I just downloaded the Struts framework but I do not have time to use it in this project. It's short of time and I guess that to really get the advantages of Struts you need to know it pretty well.