Results 1 to 6 of 6

Thread: Custom initialization of the servlet context e.g with data from a database table

  1. #1

    Default Custom initialization of the servlet context e.g with data from a database table

    Hello All,

    I am new to the Spring framework but I have been using the Spring MVC to some extent in the application I am working on. I have read through all the documentation related to the ApplicationContext and the MVC chapters and I am not able to come to a conclusion on how to solve my concern.

    I know that DispatcherServlet should be used as the controller servlet in the web.xml but if I want to load data from a table in the database into the servlet context when the application starts up, I wanted to know the best approach to do that.

    Please advise.

    Thanks,
    Neeraj.

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,844

    Default

    Can you clarify if you are trying to load data into the "javax.servlet.ServletContext" or if you are referring to the ApplicationContext being used by DispatcherServlet?

  3. #3

    Default

    Quote Originally Posted by neerajn
    I am new to the Spring framework but I have been using the Spring MVC to some extent in the application I am working on. I have read through all the documentation related to the ApplicationContext and the MVC chapters and I am not able to come to a conclusion on how to solve my concern.

    I know that DispatcherServlet should be used as the controller servlet in the web.xml but if I want to load data from a table in the database into the servlet context when the application starts up, I wanted to know the best approach to do that.
    Provided you've configured ContextLoaderListener in web.xml you can let one of your classes implement the org.springframework.web.context.ServletContextAwar e interface and configure it in the Spring Application Context. On startup the Spring Container will call the setServletContext() method and pass the ServletContext object.

    You should be aware however (no pun intended) that you bind your code to the API of the Spring Framework by doing so. You can use org.springframework.mock.web.MockServletContext in your test cases.

  4. #4

    Default

    Quote Originally Posted by Mark Fisher
    Can you clarify if you are trying to load data into the "javax.servlet.ServletContext" or if you are referring to the ApplicationContext being used by DispatcherServlet?
    Mark,

    I am trying to use the javax.servlet.ServletContext which is available from the WebApplicationContext inside the Controller classes to be able to access this data once it is loaded.

  5. #5

    Default

    Quote Originally Posted by devijvers
    Provided you've configured ContextLoaderListener in web.xml you can let one of your classes implement the org.springframework.web.context.ServletContextAwar e interface and configure it in the Spring Application Context. On startup the Spring Container will call the setServletContext() method and pass the ServletContext object.

    You should be aware however (no pun intended) that you bind your code to the API of the Spring Framework by doing so. You can use org.springframework.mock.web.MockServletContext in your test cases.
    I will try this approach too as you mentioned.

  6. #6

    Default

    Thanks for the suggestion. I was able to make it work. I have a different problem now which I cannot figure out and I will open a new thread for it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •