4

I'm looking now for a couple of days for the answer to the following question:

I have a Spring 2.5 webapplication and I want to show a certain setup screen if the initialization of the spring context has failed. In this setup screen they can look why the server doesn't startup and maybe make some changes (upload new config.properties file)

But how can I implemented this on a smart way? Has Spring already something like this or do I need to extend the ContextLoader for example?

I tried something in the web.xml like this: but this doesn't seems to work:

   <error-page>
        <error-code>404</error-code>
        <location>/public/setup.jsp</location>
    </error-page>

Solution:

I start with a default web.xml and after the setup is done I replace the web.xml with the right 'application' web.xml. Because the web.xml is replaced the servers restarts. This works great. Thanks again for your answers.

Michel
  • 9,220
  • 13
  • 44
  • 59

1 Answers1

3

Here are three ideas:

  • Modify the context loader to catch the exception, and add a servlet/mapping to the container the that redirects all the relevant mappings to the dynamically loaded servlet. Check out this stack overflow thread of instructions on how to create a dynamic servlet: Dynamically add a servlet to the servletConfig
  • Alternatively you could have a standard servlet defined that handles all requests and forwards them to the config page. You can then have a spring bean that remove thats servlet and mapping from the context when it's finished initalizing (you might want to put that code in the postInitalize hook of a spring bean.)
  • You could also try creating a listener that checks to see if a valid application context exists and removes a "default" mapping /servlet exits.

I don't think there are standard mechanisms for adding/removing servlets and mapping from the container. But it looks like most containers have some APIs that do this.

There is a third way, which you were hinting. Which is to assume that if a 404 error occured then servlet failed to start. If you go down that route I think you will run into the issue that 404 errors can occurs just because the user fat fingered the url.

Community
  • 1
  • 1
Karthik Ramachandran
  • 11,925
  • 10
  • 45
  • 53
  • Great post.. I will let you know what I going to implement and how it works – Michel May 02 '11 at 06:55
  • 1
    I want to let you know that is implemenents an whole other solution. I start with a default web.xml and after the setup is done I replace the web.xml with the application web.xml. Because the web.xml is replaced the servers restarts. This works great. Thanks again for your answer. – Michel May 11 '11 at 07:05
  • @michel that's a good idea. I'll keep it in mind. thanks for the update – Karthik Ramachandran May 11 '11 at 14:45