-1

I'm coming from .NET world. Know for, pretty sure, that in Web applications (MVC),

.NET: A new controller instance is created for each & every incoming request to the webserver and the result is rendered.

See SO Question & Answer for more details.

But, I'm about to develop a Java web application and was shocked to hear this:

Java: One controller instance is used to serve each & every incoming request to the web-server. (Singletons)

I also googled to learn that the above statement is true. Here

As for as I know, MVC is a framework and that is independent of implementing languages.

Why is this discrepancy ? Or am I missing something out of here ?

Community
  • 1
  • 1
  • @downvoters: Please feel free to leave your comment with your vote. – now he who must not be named. Mar 27 '17 at 10:59
  • In java, you can use 1 controller to serve many request, or make individual controller for each request. It's entirely upto you. – Raman Sahasi Mar 27 '17 at 10:59
  • You are right, it is independent. It's your choice to implement as many controllers as you need. I think you are coming from the Spring side. There the framework is already implemented and you have to use it's structures. – marc3l Mar 27 '17 at 10:59
  • @MarcelHöll There's nothing Spring specific about this. A basic `Servlet` is reused instead of creating a new instance every time. This means that you can't have state in servlets, although sounds like you can't have state on .NET side either (but for a different reason). – Kayaman Mar 27 '17 at 11:01
  • 1
    Not sure how useful this question is. You are trying to compare 2 distinct frameworks that have completely different implementation details. You also seem to suggest (though I may be wrong) that MVC as a concept seems to favour one way or the other, that wouldn't be accurate. (still, not my downvote) – DavidG Mar 27 '17 at 11:02

1 Answers1

1

I don't know of any specific reason why this is the case, but I can think of some pros and cons.

Java's way pros

  • Avoids object creation which could affect things when you're getting a lot of requests
  • Allows you to keep state inside controller

Java's way cons

  • Allows you to keep state inside controller. As the objects are multithreaded, you must make sure that any state you keep inside the controller (which is strongly discouraged) is used in a thread-safe way. This is not always obvious and can cause bugs.

.NET's way pros

  • Simple
  • State can be kept in the object for the duration of the request (sort of "semi-global variables") without any specific thread-safe code. No chance of the next request seeing state from a previous one (one reason why Java discourages the keeping of state in controller).

.NET's way cons

  • May be a (minor) issue if handling a lot of requests at once

Probably others too, but these I can think of off the top of my head. The difference is most likely due to a design decision, which may be impossible to track down. However, as explained in the comments, neither way is "correct". MVC doesn't require one or the other.

Kayaman
  • 72,141
  • 5
  • 83
  • 121