• Post Reply Bookmark Topic Watch Topic
  • New Topic

Model-View-Controller questions and references?

 
Bryan Scarbrough
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if this question goes here, but I will give it a shot anyway!

I asked a question the other day (http://www.coderanch.com/t/360958/Servlets/java/init-method) and one of the replies pointed me to the model-view-controller method for creating Web Applications. I did some research on the Internet and the information for this as it relates to JAVA and JDBC in particular (as far as I could find) is pretty sparse, so I thought (at the prompting of Ben Souther) to ask the forum for help. I have a few questions relating to this scheme if you do not mind answering them...

1) Does the general flow of the app go from JSP to Servlet to JavaBean?
2) If so then what is the purpose of the Bean?
3) Do you know of any good resources to find more information on this subject (especially as it pertains to Java and in particular JDBC connectivity), because what is on the internet is pretty generic.

I appreciate your time and I look forward to learning more about this scheme from you all!!

Thanks,
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, in a Model 2 (aka MVC) web app, URLs "hit" the servlet first. This servlet is usually called the "controller".

This servlet performs (or more often, delegates) any processing for the request: be it database activity or otherwise. The servlet then forwards control to a JSP which is responsible for renderring the view. In "modern"
JSPs, the use of Java scriplets and sciplet expressions on the pages is minimized or eliminated in favor of the JSTL and EL.

If any data needs to be "passed" to the view (JSP) from the servlet -- data retrieved from the database, for example -- the data is aggregated in the form of JavaBeans and/or collections and placed onto the request scope as "scoped variables".

That help? Or just bring up more questions?
 
Bryan Scarbrough
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply! This actually helps AND brings up more questions, like:

How does the controller know to send the data to the Bean and then retrieve it and then send it to the JSP? (from examples I have seen, the controller code is really simple and does not seem to explicitly pass the data form one point to another)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take the scenario where a request is made for information on a particular person whose information is in the database.

A request is made with a request parameter that has the unique id of the person whose information is to be displayed. A controller servlet, let's name it FetchPersonInfoAction, is activated. It delegates a call to some model code that, given the id value, fetches the data. The data is returned, by the model code, in a JavaBean that abstracts the collection of information for that user. Such a bean is often called a 'value object'.

The servlet places this bean in request scope as a scoped variable (via the setAttribute method) named "person", and forwards to the JSP page.

The JSP page, using the JSTl and EL, references the "person" scoped variable to display the information on the page.
[ August 16, 2005: Message edited by: Bear Bibeault ]
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably checking out one of the many MVC frameworks and actually writing an app in one of them would help? I would suggest using Struts since it is easy to grasp and brings out the concept very well.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to disagree with Neeraj, but I highly recommend not adopting a framework (particularly Struts) until one understands the mechanics of a web application. Such frameworks insulate the user from the actual mechanics and retard the understanding of these mechanics, in my opinion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!