Spring Boot Servlet Mapping
July 08, 2018
This page will walk through Spring Boot Servlet mapping example. Servlet mapping can be achieved either by using ServletRegistrationBean
or by using @ServletComponentScan
annotation in Spring Boot. ServletRegistrationBean
registers Servlet as Spring bean. @ServletComponentScan
scans Servlet annotated with @WebServlet
. The annotation @ServletComponentScan
works only using embedded server in Spring Boot. Here on this page we will create two Servlets and one Spring Controller class. We will provide examples to use Servlets using ServletRegistrationBean
as well as @ServletComponentScan
step by step.
Contents
Technologies Used
Find the technologies being used in our example.1. Java 9
2. Spring 5.0.7.RELEASE
3. Spring Boot 2.0.3.RELEASE
4. Maven 3.5.2
5. Eclipse Oxygen
Project Structure
Find the project structure of our demo application.
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.concretepage</groupId> <artifactId>spring-boot-app</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-app</name> <description>Spring Boot Application</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> </parent> <properties> <java.version>9</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Registering Servlets as Spring Beans using ServletRegistrationBean
ServletRegistrationBean
is used to register Servlets in Servlet 3.0 + container. We need to create a bean of ServletRegistrationBean
in our JavaConfig. Find some methods of ServletRegistrationBean
used to configure a Servlet.
setServlet(): Sets the servlet to be registered.
addUrlMappings(): Add URL mappings for the Servlet.
setLoadOnStartup: Sets priority to load Servlet on startup.
Suppose we have two Servlets as
HelloCountryServlet
and HelloStateServlet
then we will register them with Spring Boot using ServletRegistrationBean
as following.
WebConfig.java
package com.concretepage; import javax.servlet.http.HttpServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.concretepage.servlets.HelloCountryServlet; import com.concretepage.servlets.HelloStateServlet; @Configuration public class WebConfig { @Bean public ServletRegistrationBean<HttpServlet> countryServlet() { ServletRegistrationBean<HttpServlet> servRegBean = new ServletRegistrationBean<>(); servRegBean.setServlet(new HelloCountryServlet()); servRegBean.addUrlMappings("/country/*"); servRegBean.setLoadOnStartup(1); return servRegBean; } @Bean public ServletRegistrationBean<HttpServlet> stateServlet() { ServletRegistrationBean<HttpServlet> servRegBean = new ServletRegistrationBean<>(); servRegBean.setServlet(new HelloStateServlet()); servRegBean.addUrlMappings("/state/*"); servRegBean.setLoadOnStartup(1); return servRegBean; } }
ServletRegistrationBean
bean. The Servlets HelloCountryServlet
and HelloStateServlet
can be accessed using URL /country and /state respectively.
Find the Servlets used in our example.
HelloCountryServlet.java
package com.concretepage.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloCountryServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{ doGet(request,response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h3>Hello India!</h3>"); } }
package com.concretepage.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloStateServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{ doGet(request,response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h3>Hello Uttar Pradesh!</h3>"); } }
HelloWorldController.java
package com.concretepage.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("/world") public String helloMsg() { String msg = "Hello World!"; return msg; } }
SpringBootAppStarter.java
package com.concretepage; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootAppStarter { public static void main(String[] args) { SpringApplication.run(SpringBootAppStarter.class, args); } }
Scanning for Servlets using @ServletComponentScan
@ServletComponentScan
in Spring Boot will scan Servlets annotated with @WebServlet
, Filters annotated with @WebFilter
and Listeners annotated with @WebListener
. The annotation @ServletComponentScan
is used on JavaConfig at class level. @ServletComponentScan
scans Servlets, Filters and Listeners only using an embedded web server. Find the Servlets annotated with @WebServlet
.
HelloCountryServlet.java
package com.concretepage.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns = "/country/*", loadOnStartup = 1) public class HelloCountryServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{ doGet(request,response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h3>Hello India!</h3>"); } }
package com.concretepage.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns = "/state/*", loadOnStartup = 1) public class HelloStateServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{ doGet(request,response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h3>Hello Uttar Pradesh!</h3>"); } }
@ServletComponentScan
as following.
SpringBootAppStarter.java
package com.concretepage; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @ServletComponentScan @SpringBootApplication public class SpringBootAppStarter { public static void main(String[] args) { SpringApplication.run(SpringBootAppStarter.class, args); } }
Test Application
We can run our Spring Boot application in following ways.1. Using Maven Command: Download the project source code. Go to the root folder of the project using command prompt and run the command.
mvn spring-boot:run
2. Using Eclipse: Download the project source code using the download link given at the end of article. Import the project into eclipse. Using command prompt, go to the root folder of the project and run.
mvn clean eclipse:eclipse
SpringBootAppStarter
by clicking Run as -> Java Application. Tomcat server will be started.
3. Using Executable JAR: Using command prompt, go to the root folder of the project and run the command.
mvn clean package
java -jar target/spring-boot-app-0.0.1-SNAPSHOT.jar
Now we are ready to test the application. To run
HelloCountryServlet
, find the URL.
http://localhost:8080/country
HelloStateServlet
, find the URL.
http://localhost:8080/state
HelloWorldController
method, find the URL.
http://localhost:8080/world
References
Spring Boot Reference GuideServletRegistrationBean
@ServletComponentScan