How to catch Spring errors when launching a Tomcat web application?

I have a webapp running on Tomcat that uses Spring to inject dependencies. (This is a GWT application, but I don’t think it matters a lot for the solution I'm looking for.)

My web.xml file has the following format:

<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Servlets --> <servlet> <servlet-name>dispatch</servlet-name> <servlet-class>com.example.my.gwt.dispatch.DispatchServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatch</servlet-name> <url-pattern>/my_gwt/dispatch</url-pattern> </servlet-mapping> ... more servlets ... </web-app> 

One of my Spring configurations is connecting to the database through Hibernate:

 <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <bean id="databaseSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="datasource" /> <property name="packagesToScan"> <array> <value>com.example.my.gwt.model</value> </array> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> 

If the database is unavailable, this throws an org.h2.jdbc.JdbcSQLException, so Spring initialization will not continue, so the rest of the webapp cannot be used. Going to the webapp URL results in an HTTP 503 Unavailable Service error.

I want to do this in order to catch this error and display the page to the user (when they first go to the application), explaining what the problem is probably and suggested corrections. How can i do this?

I tried using the custom class ContextLoaderListener, which delegates the one specified in the XML above, but catches any exceptions. This allows me to catch an exception, but not so much what I can do - web.xml still points to a user request for a servlet that does not start after Spring failed to initialize. Is there a way to change the webapp configuration when I catch this exception so that it does not try to load servlets from web.xml and possibly modify the welcome file to point to the error page? Or is there another way I can get webapp to gracefully handle this exception?

thanks

+4
source share
2 answers

Basically you ask if you can have a valid web application after the web application does not start.

You can try to customize the 503 handler page and / or have a Spring-independent welcome page that checks something in the context of the application, which installs only when the spread is good. If it does not unfold, the exception that you have already caught can be placed in the application context.

Not sure if anything is in the application, even resources are only for web.xml if Spring is not spinning.

0
source

You can add a servlet filter to your web application that intercepts all Spring servlet requests and goes to your custom error page if Spring initialization failed.

0
source

All Articles