I have built a web application using servlets,jsp and static html pages. Now I want to implement mvc.
I've heard that :
1)The first page that's displayed is index.jsp
2)All user requests are handled by a controller servlet. (If true then does index.jsp pass this info to the controller servlet? If all the pages pass their request to the servlet controller then controller must have a big 'if else' structure. If so, is the entire application logic written within these if's n else's? )
3)What do we acheive in making all the pages forward their requests to controller? Can't these pages themselves not make database connections and display the results to the user?
4)If the controller extracts data from the db and puts them in java beans and these beans are then bound to the session or response to be sent to the VIEW to display the info, how does one put an entire table having say 10's of columns and hundreds of rows in javabeans simply to be passed to the VIEW?
Please tell me the exact way of acheiving mvc using servlets and jsp's. Tell me the entire series of events that take place to serve 1 request from the user.Please Tell me exactly how servlets ,jsp's and java beans work together to acheive mvc.
OR SIMPLY TELL ME WHAT YOU WANT TO TELL ME ABOUT MVC USING servlets, jsp and java beans.
I'd be really gratefull to you all. Thank you in advance.
First, you probably don't want to try to print something with tens of columns and hundreds of rows to a JSP. It would take a long time to load, possibly crash the user's browser, and at best, be a horrible experience for your end users.
This exercise might help you get the concept.
Build a small set of plain old java objects (POJOs) that read data from your database and load it into simple beans.
To test, write a main method that instanciates one of these objects, calls the method that connects to the database and returns the bean, then use System.out.println calls to print the data to the screen.
You now have your model.
Now, build a servlet that, when called instanciates one of these objects, calls the method to make the database call.
Take the returned bean and bind it to request scope.
You now have your controller.
Then, forward to a JSP.
In that JSP use JSTL and EL to loop through the data in the request scoped bean and build an HTML table with that data.
This JSP is your view.
1) you can define a welcome page who is mapped to the controller, and without action set, it displays the "index" view. Don't forget that we speak of "Views" and not pages in MVC.
2) it's better to use a config file where you map the "action" to the class that will handle the action.
3) You have one point to control how do you handle the request
4) request->front controller->(common application logic) ->dispatch to action -> (application action logic) ->dispatch to view -> (view logic).
If it's to learn how mvc works, why not. If it's for production use, don't re-invent the wheel, forget your custom servlet's for this task.
Learn a web framework, Struts 1.3.x should be very fast to learn to put the MVC Pattern in you application, you can still use your "Servlet" like coding for web forms until you learn how to use the framework for that.
Introduce the DataAccessObject pattern to access data from DB and build domain objects. Reuse code logic between controllers (Helper classes).
Put a paging system to fetch only the data that you will print to the browser.
put it in the bean with session scope for "edit" purpose (avoid database hit for nothing) or request scope if there is nothing else to do than seeing the data table.
Not necessarily. Annotations are also useful, or even "configuration by convention". Config files are not a necessity, and can be unwieldily.
I'am not a fan of Annotations, configuration by convention is a better way to reduce code and configuration files but you must have a bypass to override easily the 5% of cases where it can't be applied (and where you spend the most of your time). Ruby On Rails is a cool design, I m must try Grails to see if it's the same thing. :-)
I'm not a fan of Struts
I'am not a fan too, but he's very well documented and it can be a good way to learn an MVC architecture, and it's very easy to refactor Servlet's to Struts action's. I'am using JSF 2.0, and for a guy how have started this Servlet's / Jsp's, it could be an nightmare because the "approach" is totally different.
And why start developing a custom web framework..? :S I think it's better to spend time to implement use cases, than loosing time for basic plumbing that you could do even worst than the existing web frameworks. We have the choice with JEE. For a basic CRUD application, using a web framework is a 100% match.
You don't create JSTL tags. You perhaps mean custom tags?
Of courses ;-)
Joined: Jul 08, 2010
Thank you all for your helpful advice . Please tell me a link to the code for a sample application using servlets, jsp, and java beans. Thanks in advance.