This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes Is this MVC? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is this MVC?" Watch "Is this MVC?" New topic

Is this MVC?

Dinesh Kumar
Ranch Hand

Joined: Jul 03, 2002
Posts: 54
I posted this in the design patterns forum a couple of days ago but got no reply. Perhaps put it in the wrong forum?

"I am developing a sample web application using the Model view controller pattern. I have a handlerServlet which I use to pass control to other servlets. Those servlets contain some of my business logic and in turn pass control to JSPs for display. Like depicted below

handlerServLet --> Task specific servlets --> JSPs (results / user inputs)

Now, based on certain user inputs I need to forward the user from the JSPs to some other servlets. I reckon this is going to get me into a bit of a mess.

Is it alright that I pass the control back to the handlerServlet and then go to the respective servlet from there? On the other hand, isn't it a bit of an extra overhead when the control can go direct from the JSP to another servlet?

This is the dilemma I am facing. Can someone advise?

Also should I make a servlet for each business logic task that I want to perform? Or should I just have a new method in my handlerServlet?

Thanks. "

Can someone pls also explain whether MVC requires that you have a single controller servlet. What if it becomes too unweildy? Can we have multiple controller servlets co-ordinating?

Thanks again.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
very nice explaination buddy. i liked the way you put your problem.

anyways. it is not necessary in MVC that you have to have one servlet only. we can use task/module specific servlets. in MVC the Model(your business logic) and your View(presentation tier) should not interact with each other directly. they should go through a controller. a Controller could be any servlet.

But here, in your previous message, i can not hear anything about the Model. how you are implementing your model. usually, if not using something like ejb or something, our simple java classes work as a Model.

JSP ---> servlet(could be any, if many) ----> java class (business logic)

and then

java class (business logic) ----> servlet(could be any, if many) ---> JSP

I hope it will give you some idea about how MVC works.
Dinesh Kumar
Ranch Hand

Joined: Jul 03, 2002
Posts: 54
Thanks Adeel.

I also have some simple classes (models) that are carrying out some tasks. I am calling the methods in these classes from the servlets based on certain conditions that I check in the servlets. So if a condition is tru the servlet calls method "a" in the class else it calls method "b" etc.

My only worry was I was ending up with quite a few servlets (about 10) and I thought this may not be correct.

Thanks for clearing this.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
why are you thinking like that. it is OK. sometimes we use only one servlet as a controller in the whole app.
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Hey, Adeel...

Can i get a model code for the MVC2 architecture. Jsut to get the full taste of it?

Any code, simple, like one for each component, in a model, View and controller.....

This will really be helpful.

Thanks man.....

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Yeah i have some code but its proprietry. Dont have something of my own. So it is impossible to send or disclose it anyway.

but you can get the simple example codes easily from any MVC2 tutorials. if we look at struts framework, it reflects MVC architecture + Command Pattern. And most of the Servlet and JSP books talk about design patterns, primarily MVC2.

jQuery in Action, 3rd edition
subject: Is this MVC?
It's not a secret anymore!