I am trying to use MVC in my JSP application. For each page I have 3 pages, one for view , one for model and one for controller. Say i have an html form (form1.html) which the user fills and submits. This form goes to the controller (servlet1.java) which calls a java class, Model (model1.java) to perform database operations and business logic and the request is dispathed to the view (jsp1.jsp). Similarly, If user enters a query to search in form2.html, it goes to servlet2.java which calls model2.java and then dispatches request to jsp2.jsp to display the result to the user. In this way if I have say 10 forms, then i'll have 10 jsp's , 10 servlets and 10 models, names of each model and view hard coded into the servlet. I know this is not the right way to do it. So many controllers and hard coded names of servlets and jsps make it look weird. There must be a better way of doing it. In chapter 14 of HF Servlets & JSP it is mentioned that Struts can be used to solve this problem. But at the moment i can't learn Struts. Is there any way i can use just Servlets and JSP to solve this problem.
From page 55 HF, something like this I am doing in my app
You dont have to 10 servlets for that. For each page you have pass a hidden value and action can be one servlet. if the page name is "abc" do this in that servlet, that way one servlet will do for 10 servlets.
Joined: Aug 25, 2003
If the number of pages are less there is no need to go with structs.
Joined: Nov 07, 2003
Does that mean having 1 controller for each model and view is correct ? And will hard coding the name of view and model in the controller be correct ?
You dont need to. You can specify all the names of possible JSPs in the configuration file's <init-param>, where the param name is view name, while value is the target model name.
And as Mary said, for each JSP, you can submit the view name via hidden field, so that you check which model you are going to use (or create by reflection).
There are several trade-offs when deciding to use only one servlet controller or multiple servlet controllers. Here is a rough list:
one servlet for the whole webapp
one servlet for each use case in the webapp
one servlet for each action in each use case in the webapp
I have found that if you take the first and second approaches that your servlet(s) will be very complicated with lots of conditional or dispatching code (based on the hidden "action" field as suggested by Mary).
The alternative (third approach) is to use a different servlet for each action in your webapp. This design implies a proliferation of servlet classes that need to be built for your webapp. This approach has several positive aspects. First, each servlet is highly cohesive (which is good OO). Second, it is easy to add new actions to your webapp. Finally, if you do decide to refactor your webapp to use Struts, this approach makes it much easier as each servlet simply becomes a subclass of the Action class.
Joined: Nov 07, 2003
That's why Struts implements MVC in that way. If you dont use Struts, you may need to implement MVC, better refers to what Sturts did.