Solved

Can't implement MVC properly in JSP

Posted on 2004-10-14
22
175 Views
Last Modified: 2013-11-24
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
Comment
Question by:thomas908
  • 8
  • 5
  • 5
  • +1
22 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12316138
that all sounds fine, which particular part do you think isn't right?
0
 
LVL 8

Author Comment

by:thomas908
ID: 12316239
>> 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
 
LVL 92

Accepted Solution

by:
objects earned 230 total points
ID: 12316775
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
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 40 total points
ID: 12316781
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 12316789
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
 
LVL 35

Assisted Solution

by:girionis
girionis earned 230 total points
ID: 12316792
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
 
LVL 92

Expert Comment

by:objects
ID: 12316824
> 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
 
LVL 16

Expert Comment

by:suprapto45
ID: 12316836
For the tutorial,

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

I wish you all the best luck :).
0
 
LVL 35

Expert Comment

by:girionis
ID: 12316890
> 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
 
LVL 8

Author Comment

by:thomas908
ID: 12317101
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
 
LVL 92

Expert Comment

by:objects
ID: 12317110
sure, use jsp pages as your views, and servlets as your controllers.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 8

Author Comment

by:thomas908
ID: 12317130
>> 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
 
LVL 8

Author Comment

by:thomas908
ID: 12317136
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
 
LVL 92

Expert Comment

by:objects
ID: 12317175
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
 
LVL 8

Author Comment

by:thomas908
ID: 12317202
Are the names of view and model hard coded in the controller(servlet) ?
This is what I have done. Is this correct ?
0
 
LVL 35

Expert Comment

by:girionis
ID: 12317216
> >> 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
 
LVL 8

Author Comment

by:thomas908
ID: 12317405
Are the names of view and model hard coded in the controller(servlet) ?
0
 
LVL 35

Expert Comment

by:girionis
ID: 12317425
> 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
 
LVL 8

Author Comment

by:thomas908
ID: 12317444
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 12317461
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
 
LVL 8

Author Comment

by:thomas908
ID: 12354969
Thanks for the help.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12356419
Hi,

Glad to help. :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now