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

making sense of MVC

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
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


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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

Joined: Jan 24, 2001
Posts: 476
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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

Joined: Jan 24, 2001
Posts: 476
Is there a an example of this view-controller communication? A link perhaps?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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

Joined: Jan 24, 2001
Posts: 476
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

That's why we're here!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: making sense of MVC