*
The moose likes Servlets and the fly likes MVC2 outside servlet for testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "MVC2 outside servlet for testing" Watch "MVC2 outside servlet for testing" New topic
Author

MVC2 outside servlet for testing

Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Hi!

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.

Is there any example on MVC2 for Servlets, JSP, POJO?

// Mathias


SCJP1.4
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
You can have your connection pool but servlet container has its own connection pool.Why do not you use that ? In fact that is the prefered way to get connection in a servelt environment.


Rahul Bhattacharjee
LinkedIn - Blog
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
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?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

If you have to re-compile your model classes to test them outside of the srvlet environmnet, that means that you have not succeeded in isolating your model from your controller.

Think of how you are using the connection pool. What belongs in the controller, and what belongs in the model?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
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?
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Mathias,

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.
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Thanks!

-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?
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
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.

Herman
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Thanks Herman!

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.

// Mathias
 
wood burning stoves
 
subject: MVC2 outside servlet for testing