Mostly correct: beware saying it "redirects" to the other view - you're generally not doing an HTTP redirect, but doing a RequestDispatcher.forward(...) within the servlet. Unlike a redirect, the forward is handled completely server-side, which means your View can access anything in the original Request objectHarish Moolchandani wrote:1. The flow of MVC application:
I've a little confusion here correct me if I'm wrong. The user interacts with the view. The view calls a controller which interacts with the javabean and redirects to other view. Right?
The controller can determine the most correct View to use, and can deal with error-handling before determining what View to serve. In a JSP & Servlet environment, the Controller Servlet interacts with any other libraries required, leaving the JSP template to focus solely on the HTML (or XML) View.Harish Moolchandani wrote:2. Why to use a controller?
I know controller act as a bridge between model and view. It creates a javabean and redirect to a view. But why do we need it? I can create a javabean object using <jsp:useBean> which will be easy to use.
You probably want a Data Access Object (DAO) - http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html - rather than a simple bean. Again, a Controller Servlet is better able to deal with a DAO than a basic JSP template.Harish Moolchandani wrote:3. Is it good to add update, delete and insert functionality in a javabean.?
I've seen some MVC examples and javabean has a getJavaBean method to select and initialize the bean. But if we want to insert, update or delete in database can we use similar methods in that bean?
It's up to you, of course. My preference would be to make the Controller responsible for validation, since it could then forward me to the appropriate view - be it a login screen, an error page, or the data I am trying to view. You could probably do it with a tag as well, but I am not as familiar with this technique.Harish Moolchandani wrote:4. Validation in an MVC application.
I will create a tag for login using a Tag file. User will press the submit button and a login controller will be called. Controller will check whether user exists, if exists it redirects to its appropriate profile. But if user doesn't exist then it shows the error in that Login tag (Just like today's web applications). Is validation to be done in Login tag or in the controller.
OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower