• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Can't implement MVC properly in JSP

i am trying to use MVC in my JSP application. For every job to be done 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.
I somewhere read 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 and get a better OO in my application.
0
thomas908
Asked:
thomas908
  • 8
  • 5
  • 5
  • +1
3 Solutions
 
objectsCommented:
that all sounds fine, which particular part do you think isn't right?
0
 
thomas908Author Commented:
>> that all sounds fine, which particular part do you think isn't right?

Having a seperate controller for each job to be performed.
If i have say 10 forms to submit, I'll need 10 servlets, 10 models and 10 jsps and name of each model and jsp hardcoded into the servlet. Something like


SignUpModel ------- SignUpServlet --------- SignUpJSP

AcceptSignUpModel-------AcceptSignUpServlet----------AcceptSignUpJSP

InputProfileChangesModel  -----  InputProfileChangesServlet-------InputProfileChangesJSP

AcceptProfileChangesModel ---------AcceptProfileChangesServlet--------AcceptProfileChangesJSp

MainPageModel ---------MainPageServlet--------MainPageJSP

InputQModel -------- InputQServlet -------InputQJSP


DoQModel -------- DoQServlet -------DoQJSP

0
 
objectsCommented:
you don't need a seperate model/view for every job to be performed.
your model layer should provide a representation of the data you're app deals with, no need for a seperate one for each action. In fact you may just have a single model that your actions update.
Similiar for views, you need a seperate view for each page, not for each controller. Different controllers may well share views for example.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
suprapto45Commented:
Well,

Thomas, you could use DispatchAction in Struts. You will still have 10 HTML and 10 JSP but you will only have 1 Java model. You could find some information about DispatchAction.

Let me try to help you. Please wait for my message.
0
 
suprapto45Commented:
Hi,

One java model but many methods in it. Probably you will have your 10 methods (as the representation of your 10 Java model)
0
 
girionisCommented:
Just have one controller, one model and several views. Your controller should be smart enough to distinguish between different calls to the model. If you are using a web framework like Spring or Struts this should be done automatically for you since you define different methods that deal with different views and the model.
0
 
objectsCommented:
> Just have one controller, one model and several views. Your controller should be smart enough
> to distinguish between different calls to the model.

wouldn't that one controller being just checking the action, and calling the appropriate action code?
So you'd still *really* have multiple controller, just with a single entry point.


0
 
suprapto45Commented:
For the tutorial,

please refer to this URL.
http://doc.advisor.com/Articles.nsf/nl/13372

I wish you all the best luck :).
0
 
girionisCommented:
> wouldn't that one controller being just checking the action, and calling
> the appropriate action code?
> So you'd still *really* have multiple controller, just with a single entry point.

It would yes. I mean one controller in the sense of one class.
0
 
thomas908Author Commented:
Can all this be done without using  Struts by using only Servlets and JSP and no such framework like struts.
or is there no option other than using Struts or similar framework
0
 
objectsCommented:
sure, use jsp pages as your views, and servlets as your controllers.
0
 
thomas908Author Commented:
>> sure, use jsp pages as your views, and servlets as your controllers.

that i am already doing but still can't do what u mentioned in ur previous post.
0
 
thomas908Author Commented:
Take the example I gave earlier, how many models , views and controllers need to be made. i have made 1 for each but that obviously is not correct.

Is there some example available on net
0
 
objectsCommented:
really depends on the application.
typically you'd have a single model (representing the data your webapp works with) which could well be made up of lots of classes.
each required view would be implemented as a jsp page
and any action that is required to update your model you would have a servlet.
0
 
thomas908Author Commented:
Are the names of view and model hard coded in the controller(servlet) ?
This is what I have done. Is this correct ?
0
 
girionisCommented:
> >> sure, use jsp pages as your views, and servlets as your controllers.

> that i am already doing but still can't do what u mentioned in ur previous pos

Have a servlet that receives the requests from the views. Then idenityf each separate view in the servlet (you can do it by either reading the URL of the view or by passign a different parameter each time with the url, soemthing like http://www.mysite.com?view=1, http://www.mysite.com?view=2).

Then in your servlet do the processing based in each view, for example:

public void doPost(...)
{
   if (request.getParameter("view").equals("1"))
   {
      // process view 1
   }
   else ...

}
0
 
thomas908Author Commented:
Are the names of view and model hard coded in the controller(servlet) ?
0
 
girionisCommented:
> Are the names of view and model hard coded in the controller(servlet) ?

Not necessarily but you can do it if you want. Better have a configuration file and read them from there.
0
 
thomas908Author Commented:
>>Better have a configuration file and read them from there.

do u mean web.xml or some other configuration file. Any example on this ?
0
 
girionisCommented:
You can have both, either an XML or a simple txt file would do. But it will be simpler and faster to have a simple text file with key and values pairs. Then read it liek you do with property files: http://javaalmanac.com/egs/java.util/Props.html
0
 
thomas908Author Commented:
Thanks for the help.
0
 
suprapto45Commented:
Hi,

Glad to help. :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now