• 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
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

making sense of MVC

 
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on web application and trying to follow MVC design pattern. Now, I have almost all parts ready: Model and View. Right now in my View (JPS page) I decide how program flow should go. So, according to MVC I should move decision part to a Controller. Now, should a controller be a Servlet or just a Bean that talks between View and Model. If so, should the Servlet (Controller) be called from inside a JSP? If yes, how? I mean if I am going to use <jsp:forward="/servlet/controlServlet"> then how do I come back with data from Servlet into JSP page (View). I think I am confused...

thanks,
Alex
 
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
Following the standard Model 2 pattern (a rough approximation of MVC for web apps), your URLs should map to the servlet controllers, and not JSPs. The servlets do their thing, and then forward on to the JSP pages to render the view.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right now I have my jsp page getting input from what button was pressed by user, so if I have Servlet Controller do that and Controller forwards output to JSP, after that step how can a servlet proccess next request if output had been forwarded to JSP and all controls have been left with a Servlet? Or should JSP have representations of controls but forward the actual values back to servlet for proccessing such as <jsp:forward page="/servlet/controlServlet?action=next">?
[ July 20, 2004: Message edited by: Alex Kravets ]
 
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

after that step how can a servlet proccess next request if output had been forwarded to JSP and all controls have been left with a Servlet?



I have no idea what you are asking here.

The sequence is actually pretty simple:

1) When a URL is entered, the servlet gains control and performs whatever processing it needs to do. The servlet produces no response output.

2) It forwards to a JSP page which renders the HTML/Javascript to be sent to the browser, including any form controls, links and so on. Any info that the servlet needs to pass onto the JSP are stored (by the servlet) as request attributes which the JSP can easily retrieve.

In best case, the JSP contains no Java. JSTL tags and custom actions should be used in place of scriplets.

3) Once the page is rendered by the browser, the user initiates any action by selecting links or submitting forms, which brings us back to step 1.
[ July 20, 2004: Message edited by: Bear Bibeault ]
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, so the entry point is a Servlet, it proccess data and forwards to a JSP page, JSP page displays output from Servlet and as the second step and all steps after that submits to the Servlet, right?


Any info that the servlet needs to pass onto the JSP are stored (by the servlet) as request attributes which the JSP can easily retrieve.


Isn't this a lot of requests?
[ July 20, 2004: Message edited by: Alex Kravets ]
 
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

Isn't this a lot of requests?



Request attributes do not cause new requests. Attributes are simply stored in a container (usually a Map) maintained by the request instance.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a an example of this view-controller communication? A link perhaps?
 
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
Here's a quickie example. Let's say I want to send a String to the page.

In the servlet:



In the page



Of course, the attribute can be any object, and is usually a Java Bean with properties that are easily accessed via the EL.
[ July 20, 2004: Message edited by: Bear Bibeault ]
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, got it. Will try it out, if I'll have any question I'll bug you some more...if you don't mind.

thanks,
Alex
 
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
That's why we're here!
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic