• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

MVC implementation (JSP or Class)

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ben, I'm glad to hear I'm on the right track.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might be interested in this article.
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic