aspose file tools*
The moose likes Servlets and the fly likes MVC implementation (JSP or Class) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "MVC implementation (JSP or Class)" Watch "MVC implementation (JSP or Class)" New topic
Author

MVC implementation (JSP or Class)

Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
Hi there,
I'm studying for SCWCD and trying to get an understanding of the practical side. I've written a little web app, and wanted to check that the following makes sense and is good practice.

The user clicks a link and this is mapped in web.xml to a servlet.

So my Controller here is a java class extending HttpServlet.
This will do some logic and talk to other java components (the Model).
The Model returns a bean which the Controller attaches to the request and forwards to a JSP (the View).

Does that sound correct? And are Controllers typically java classes or is there a reason for implementing them as JSPs themselves?

Many thanks,
Doug


SCJP, SCWCD
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What you're doing sounds correct.

No, you wouldn't want to use a JSP as the controller.
A servlet is the best tool for that task.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
Thanks Ben, I'm glad to hear I'm on the right track.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

You might be interested in this article.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
Thanks Bear, that was an interesting article and tied a few things together in my head. Although I'm still a step or two away from trying to understand Struts properly!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's almost nothing about Struts in that article--not necessary to understand much about it.
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
Sorry, what I was trying to say was that it is interesting to understand the evolution of the Patterns. Seeing it in this context helps me understand why we would use them.

From my practice, I'm still at the MVC level and have not yet moved to trying to implement a Front Controller (Struts or otherwise). I think it'll be a little while before I try that.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Er, a Front Controller is one part of MVC. It's the C.
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
David, is Front Controller not a pattern which simplifies the Controller part of MVC by reducing it from multiple Controller servlets to a single servlet?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

That's typically how I've used the terms.

Controller is more generic.
You could have a controller for every component in your application.

Front controller is MVC with the "Command Pattern" mixed in allowing you to have a single controller for an entire application (like what Struts and Frontman have).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Yes, it's not necessary to use a Front Controller to follow MVC -- but as the article points out, any other strategy is madness.
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
Thanks guys, I'm just trying to make sure that I've got the basics well understood before I add the next complexity.

At the moment the web-apps I'm building as I learn only have a couple of Controller servlets so Front Controller is probably overkill.

Once I've got SCWCD what do you think is most sensible to learn next? I thought maybe Struts and Hibernate?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Doug Braidwood wrote:At the moment the web-apps I'm building as I learn only have a couple of Controller servlets so Front Controller is probably overkill.


Not really.
Once I've got SCWCD what do you think is most sensible to learn next? I thought maybe Struts and Hibernate?

Don't learn Struts 1 unless you really, really need to, or want to learn it for historical purposes. There are an arbitrary number of web frameworks (of which Hibernate is not one) you could learn; Struts 2, Spring MVC, FrontMan, Wicket, Tapestry, JSF, ... Which to choose depends on your goals and ambition.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Doug, Just a note, to keep in mind that the concept of a Controller is a "design" concept. And the Model-View-Controller object-oriented design pattern is also a "design" concept. There can be many, many different ways to implement the design, with different programming languages or with different implementation designs using the same programming language.

The Struts Controller implementation consists of many more files than a single Java servlet class, for example. There are parts of the Controller that are open-source and then there are parts that a developer codes and then there is one or more XML-based configuration files which are created by the developer. The open-source parts of the Struts Controller are integrated with the proprietary parts that are created by an organization. This is why Struts is called a "framework." The open-source parts do nothing by themselves, they just give you something to build upon.

Hope this helps. Also Struts 1.X is still used in many, many organizations. So there is a significant value to learning it . Struts2 is a different take on MVC concept and can be considered a different framework altogether (which just cannibalized the popular "Struts" brand-name.)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Frank Bennett wrote:Hope this helps. Also Struts 1.X is still used in many, many organizations. So there is a significant value to learning it.

This is debatable.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Doug Braidwood wrote:
At the moment the web-apps I'm building as I learn only have a couple of Controller servlets so Front Controller is probably overkill.



Yea, I said that once.
Then one more component, then another, ......
With each one, it becomes more and more work to make the transition.
Do yourself a favor and start it right from the beginning.

We have a very simple example of the command pattern with MVC in the Servlets section of our CodeBarn.
I wouldn't use it for a large production app but it is a quick way to have a small working example on your desktop in under 5 seconds.
Once you get the gist, take a look at some of the ones that David mentioned earlier. They'll save you a lot of work when your project gets bigger.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

If you don't want to get involved in a big framework at this point, I'll plug my own FrontMan (link in my sig).

It's just a Front Controller without all the bells and whistles of the big frameworks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MVC implementation (JSP or Class)