instantiate model object in jsp web project
Im writing an开发者_JS百科 web application using the MVC design pattern.. the application should connect to a RMI server providing the business part (Model)
my problem is, I dont know where i should instantiate the model class and connect to the RMI, providing access to all servlets.. i added a ServletContextListener and shared the reference on the servlet context, but i dont think that is the right way to do it
thanks in advance
You can instantiate your Model/Business classes from your Controller. Better still make a single instance (Just Create One) of each in the start of the application. You can also go lazy-loading about it.
Instantiating Model/Business classes from your JSPs means, instantiating them from View. That, of course, violates the MVC pattern.
Either you can make your Model/Business classes as Singleton, or keep the reference in Servlet Context. Both are fine and the latter is more test friendly. The former can be made test friendly too, if you don't specify the constructor as private
but just have a common understanding that it's not there for instantiating it everywhere.
精彩评论