• 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

In MVC, JSP controller or Servlet controller, which one is better?

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
It is pros and cons to use JSP controller or Servlet controller.
If we use JSP controller, after receiving the client request,the compiled JSP servlet requests information from server components,which perform any necessary computation and encapsulate the business logic. Then, the compiled JSP servlet inserts the results of the computation into the Web page, which is then rendered and interpreted by the browser as usual. In this case, controller code and view code are mixed in the same component. This may make maintenance of the application more difficult.
If servlets are used as the controller, browsers invoke servlets which then invoke JavaServer Pages. The Java Server pages would access
only the information needed to display results. The Servlet interacts with the JavaBeans to perform any necessary computation and encapsulate the business logic, and may also create beans to store the results of the computation. The JSP then extracts whatever information it requires from the JavaBeans and merges them with the Web page. The browser then interprets and renders the Web page as usual. But using servlets as the controller adds another layer to the application
beyond JavaServer Pages.
What is the reality in the applications you designed and developed? Thanks.
Ren
 
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, it totally depends on your application. What you've described here are the "model 1" and "model 2" architectures.
Unless your application is very small (for example just a handful of JSPs), I would recommend adopting the 2nd approach, where servlets are used as the controller. You are right in saying that this does add an additional layer into your application, but in the end, it will be much more maintainable because of this structure.
If you're looking for more information about this, check out chapter 12 "Designing Web Applications and Servlet Patterns" from Professional Java Servlets 2.3, which is available to download from my website.
On an implementation level, there are many ways in which this can be tackled - from simple frameworks to fully fledged open source projects such as Struts. The project that I'm just finishing up uses an extremely simple framework consisting of around 4 classes. It all depends on the complexity of your project...
Hope that helps.
Simon
 
Ren Li
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Simon. The more I read your book, the more I like it. I am gonig to apply the model 2 architecture in new application.
One more quick question. In JSP, we may specify the scope in jsp:useBean tag. If we use servlet as controller, how could we specify the scope?
Regards,
Ren
 
Simon Brown
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The scopes REQUEST, SESSION and APPLICATION map on to the ServletRequest, HttpSession and ServletContext respectively. So, to add a session scoped attribute, simply grab the HttpSession and call setAttribute. Likewise for the ServletRequest and ServletContext.
Cheers again
Simon
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic