• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MVC without using struts

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



I want to solve this prob without using Struts.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since your pages are not going to be reused by different flows, you may simply specify the target Servlet that your form wanna to be submitted to.

And that Servlet can determine which page will be forwarded to after the process.

This can then by pass the usage of Struts.

But still, seems using Struts gives you more flexibility, isnt it?

Nick
 
K Anshul
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
Since your pages are not going to be reused by different flows, you may simply specify the target Servlet that your form wanna to be submitted to.

And that Servlet can determine which page will be forwarded to after the process.

This can then by pass the usage of Struts.

But still, seems using Struts gives you more flexibility, isnt it?

Nick



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 ?
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anushal,

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.
 
Mary Wallace
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the number of pages are less there is no need to go with structs.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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).

Nick
 
author
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

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.

    HTH,
    Bryan
     
    Nicholas Cheung
    Ranch Hand
    Posts: 4982
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.

    Nick
     
    Ranch Hand
    Posts: 884
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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.



    Was Sturts the father of Struts?
     
    Nicholas Cheung
    Ranch Hand
    Posts: 4982
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry for the typos.

    Nick
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic