Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MVC implementation (JSP or Class)

 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben, I'm glad to hear I'm on the right track.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in this article.
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's almost nothing about Struts in that article--not necessary to understand much about it.
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er, a Front Controller is one part of MVC. It's the C.
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic