aspose file tools*
The moose likes Servlets and the fly likes getServletConfig() in a model class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getServletConfig() in a model class?" Watch "getServletConfig() in a model class?" New topic
Author

getServletConfig() in a model class?

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Hi,

Is is possible to get parameters from web.xml (init-param's) from a model class? I am trying "getServletConfig().getInitParameter("paramName") - but the model class isn't liking that (even if I import javax.servlet.Servlet where the 'getServletConfig' is based.

For example, currently I have all of the DB connection details in each of the model classes that connects to the DB - but this isn't a good solution as need to have one place (web.xml) that stores all of this data for re-use in all of the model classes.

Is this possible?

Or will I have to upgrade all the model classes to accept DB connection details parameters which can be pulled in from the Servlet calling the model class?

Thanks
Michael

edit:
Doing a bit more reading at the moment - and it seems that I need, "getServletContext" instead so that the data is available to all servlets and not just a specific one - not figured out how to get that at the moment via a model class...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Accessing interface-specfic artifacts such as anything to do with servlets or JSP in the model is a severe and drastic violation of the Separation of Concerns upon which MVC is based.

Your Model should know nothing -- nothing -- of the technology used to implement the user interface. Your Model, for example, should also be able to serve as a backend to a Swing application. It can't do that if you mangle it all up with servlet-specific stuff.

If the Model needs information from the UI, pass it as parameters that are free from UI entanglements.

If you need app-wide settings, property files can be used which are agnostic to where they are located in the MVC stack.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

I would also ask, why does your UI know anything about the DB connections? That's the Model's concern, not the UI's.
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Haven't quite got the full understanding of MVC just yet - just a mini-MVC at the moment I guess :-)

Currently I have got the DB connections & details in each model class that needs data from the DB. What I understand from your comments is that to make the project more scalable (and correct for MVC) I need to.....
1) Leave the DB connections in the model classes (ie, try and connect to db, pull data back, and return data etc)
2) Move all of the DB details into the web.xml file (as context params)
3) Within each Servlet, I need to get the Context Params and pass these to each model class (that requires them) as parameters which are used in the DB connection.

Could I then take this a step further by separating all of the "connect to DB" parts of the model classes into a separate model class? So I would end up with 3 model classes per DB call...
1) TryConnectToDB(dbDetails, asParams, etc) <-- model class
2) DoThing <-- model class which could select data, update data or whatever was required
3) CloseDBConnections

Have I understood that correctly?

Thanks
Michael
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

No.

You've still got your UI involved in dealing with the database. The whole idea of Separation of Concerns is that things that don't need to know something, don't have to know about it!

  • Your UI doesn't need to know anything about the database. Why get it involded?
  • Your Model needs to know all about the database -- why should need a UI to tell it about the database?


  • Put the information where it's needed. That way you don't have to worry about passing it around, and you avoid the strong coupling that results.

    The web.xml is great place to put information that the UI needs. It's a horrible place for other things, such as DB info.

    I'd recommend putting the DB info into a properties file that can be dropped anywhere on the classpath, and that the Model uses to get its DB info.

    Your UI shouldn't even know that a DB is in use. It should just ask for and send data, and not care where it comes from, or what happens to it.
    Michael Cropper
    Ranch Hand

    Joined: Sep 30, 2009
    Posts: 137
    Thanks for the clarification BB, that has made much more sense now.

    So the best way to make my project MVC correct is to leave all the DB connection information etc in the model class so that when a Servlet 'asks' the model for 'information' after passing 'some_parameter' then the UI has no idea where/how that information was generated - as far as the Servlet/UI is concerned it has the data that it asked for.

    And for ease of changes, I could put all of the DB connection details into a .properties file.

    So as an example, the Servlet would ask the model "give me the 'name' of this person from the DB whose personal 'ID' is 123"
    Then the model would accept "123" as a parameter and select the "name" from the DB accordingly
    Then the model would pass back "bob" to the Servlet that called it
    So the Servlet is happy that it got what it wanted
    And the model is happy as it wasn't dependent on the Servlet/UI passing in the correct DB details

    ... The dots are all starting to join together

    Michael

    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61761
        
      67



    I think you are beginning to see how severing the dependencies between the layers can simplify everything.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61761
        
      67

    By the same token, DB-specific stuff like resultsets should never make their way out of the Model.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: getServletConfig() in a model class?