0

Background

I'm new to spring MVC but I can clearly see the benefit of using a class annotated with @Controller as opposed to a child class of HttpServlet.

Question

Since spring allows you to have multiple servlet-context files, I will assume some people mix both annotated controller classes and standard HttpServlets. My question is why would you want to? Wouldn't you then have to go through the trouble of connecting that servlet to the appropriate view and lose out on the reason you chose Spring MVC in the first place?

2 Answers 2

2

One of the main ideas of Spring is to make it non-obtrusive - you should not be forced to rewrite all your code if you decide to use Spring in your application.

From that point of view use of Servlets other than DispatcherServlet in Spring application can be easily justified: you may have Servlets containing legacy code, or third-party libraries implemented as Servlets (web services, RPC, other web frameworks, etc). You don't want to rewrite these legacy components at once, but Spring allows you to leverage its advantages in these components by moving core services of your application to the root application context, so that you can use them from your Spring MVC controllers as well as from other Servlets.

Sign up to request clarification or add additional context in comments.

Comments

1

Spring annotated controllers are used in conjunction with the Spring DispatcherServlet. This is a Spring implementation of HttpServlet that provides all of the functionality described in the documentation.

The DispatcherServlet is configured in web.xml and the location of the Spring configuration is provided. This can one or more xml files, or one of more classes annotated as @Configuration (from Spring 3.1).

This configuration should contain the element (or @EnableWebMvc). This will trigger spring to scan the classpath for @Controller classes with the application is started.

2 Comments

Thanks but my question wasn't about how the Spring DispatcherServlet works. My question was why would you use additional servlets and not just the DispatcherServlet alone.
Oh sorry. You might want to have an additional servlet that doesn't use Spring MVC at all. JAX-WS might be one reason.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.