0

So i have a sort of design question: I have a jsp, and a controller that fetched the data for that jsp. Some of that data come from service classes. I know that mvc pattern tells me to use the controller to call the service class and pass that info to my view (jsp). Why can't I call the service class from my jsp directly?

solar apricot
  • 353
  • 2
  • 5
  • 24

2 Answers2

0

You can. You can even put everything in one class and maybe it will work. But why? Doing like that ruin all flexibility. You think only about little example, but you should think what advantages it gives to big applications. Read this.

Community
  • 1
  • 1
mchern1kov
  • 450
  • 3
  • 10
  • yes, i guess instead of "can" i should have used "should". I knew i could, and i knew i shouldn't. I just didn't know how to explain why. Thank you! – solar apricot Nov 08 '16 at 20:03
0

You can, and that's what developers sometimes do. But you shouldn't.

MVC is about interchangeability and separation of concerns. If you call your service from JSP, you create a tight coupling, to parameters and return types, for example.

Moreover, usually, systems are not developed singlehandedly. Let's say you have getAllAdmins() method in your service, which you use for internal logic. Do you really want another developer to use it directly in JSP, and by mistake display all your admins? Probably not.

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40