Servlet container initialization through code

In this section, you will learn about Servlet container initialization through code in Spring MVC.

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

Servlet container initialization through code

In this section, you will learn about Servlet container initialization through code in Spring MVC.

You can configure the Servlet container programmatically in a Servlet 3.0+ environment. You can use it as an alternative or in combination with a web.xml.

Given below sample code for registering a DispatcherServlet :

import org.springframework.web.WebApplicationInitializer;

public class ApplicationInitializer implements WebApplicationInitializer {
	@Override
	public void onStartup(ServletContext container) {
		XmlWebApplicationContext ctxt = new XmlWebApplicationContext();
		ctxt.setConfigLocation("/WEB-INF/spring-config/dispatcher-config.xml");
		
		ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(ctxt));
		registration.setLoadOnStartup(1);
		registration.addMapping("/");
	}
}

In Spring MVC, WebApplicationInitializer interface automatically initialize Servlet 3 container with your provided configuration.

Registering DispatcherServlet becomes much easier using AbstractDispatcherServletInitializer by overriding the functions which used to assign DispatcherServlet configuration location and the Servlet mapping.

Given below Java-based Spring configuration :

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
	return null;
	}
	
	@Override
	protected Class<?>[] getServletConfigClasses() {
	return new Class[] { AppConfig.class };
	}
	
	@Override
	protected String[] getServletMappings() {
	return new String[] { "/" };
	}

}

Using AbstractDispatcherServletInitializer, the above configuration can also be done by using  XML-based Spring configuration as shown below :

public class AppInitializer extends AbstractDispatcherServletInitializer {
	
	@Override
	protected WebApplicationContext createRootApplicationContext() {
		return null;
	}
	
	@Override
	protected WebApplicationContext createServletApplicationContext() {
		XmlWebApplicationContext appContext = new XmlWebApplicationContext();
		appContext.setConfigLocation("/WEB-INF/spring-config/dispatcher-config.xml");
		return appContext;
	}
	
	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}

}

AbstractDispatcherServletInitializer also provide suitable and appropriate way to add Filters, which automatically mapped to the DispatcherServlet :

public class AppInitializer extends AbstractDispatcherServletInitializer {

	// ...
	
	@Override
	protected Filter[] getServletFilters() {
	return new Filter[] { new HiddenHttpMethodFilter(), new CharacterEncodingFilter() };
	}

}

Advertisements

Ads
Share on Google+Share on Google+

Servlet container initialization through code

Posted on: January 31, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Servlet container initialization through code  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads