Spring Boot web application example

In this post, we will see how to create Spring boot web application example using JSP.

Let’s see how Spring boot makes over life simpler.I am going to create Spring MVC hello world example using Spring Boot.

Github Source code:

Spring Boot web application example Using JSP:

Here are steps to create a Spring boot web application example Using JSP.

Project structure:

Spring boot project structure

Tools used for creating below project:

  1. Spring Boot 1.5.3.RELEASE
  2. Spring 4.3.8.RELEASE
  3. Tomcat Embed 8
  4. Maven 3
  5. Java 8
  6. Eclipse
Step 1: Create a dynamic web project using maven in eclipse named “SpringBootHelloWorldExample”.

Maven dependencies:

Step 2: Change pom.xml as below:

The spring-boot-starter-parent provides you all maven defaults required for any spring project.
Since we are developing a web application, we also need to add spring-boot-starter-web dependency.
This will include additional dependencies such Spring boot, tomcat etc which are required for this application.
If you notice, we did not provide any version for specific components. You just need to provide version no.(1.5.3.RELEASE) for spring boot.
step 3: Create a package named “org.arpit.java2blog.springboot”
create a controller class named “HelloWorldController.java”

As request first goes to dispatcherServlet and it redirects to the controller class. Here @Controller depicts that this is our controller class. @RequestMapper is used to map incoming HTTP request to handler method(hello() in above controller).So hello() method of HelloWorldController.java will handle GET request from dispatcher.So when we have url of

above method will get called.

Step 4: Create a package named “org.arpit.java2blog”
create a class named “SpringBootHelloWorldApplication.java”

We have just added @SpringBootApplication and it does all the work.
Let’s understand more about this annotation.
@SpringBootApplication is an annotation that adds all of the following:

@Configuration makes the class as a source of bean definitions for the application context.
@EnableAutoConfiguration enables Spring boot to add beans presents in classpath setting and various property setting.
Normally you would add @EnableWebMvc for a Spring MVC application, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath.
This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
@ComponentScan tells Spring to look for other components, configurations, and services in the default package, allowing it to find the controllers.
If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

Step 5: Create a property file named application.properties as below and put it in src/main/resoures.

spring.mvc.view.prefix: /WEB-INF/
spring.mvc.view.suffix: .jsp

Above two properties are very much similar to used in springmvc-dispatcher-servlet.xml in Spring MVC example.

Step 6: Modify index.jsp as below:

Step 7: Create hello.jsp in /WEB-INF/ folder

That’s all about Spring Boot hello world example.
Step 8: It ‘s time to do maven build.

Right click on project -> Run as -> Maven build

Step 9: Provide goals as clean install spring-boot:run (given below) and click on run
Spring boot maven build
you will see below output at console:

[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building SpringBootHelloWorldExample Maven Webapp 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-clean-plugin:2.6.1:clean (default-clean) @ SpringBootHelloWorldExample —
[INFO] Deleting /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ SpringBootHelloWorldExample —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ SpringBootHelloWorldExample —
[INFO] Changes detected – recompiling the module!
[INFO] Compiling 2 source files to /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/classes
[INFO]
[INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ SpringBootHelloWorldExample —
[INFO] Not copying test resources
[INFO]
[INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SpringBootHelloWorldExample —
[INFO] Not compiling test sources
[INFO]
[INFO] — maven-surefire-plugin:2.18.1:test (default-test) @ SpringBootHelloWorldExample —
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.18.1/maven-surefire-common-2.18.1.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.18.1/maven-surefire-common-2.18.1.pom (7 KB at 2.0 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.pom (2 KB at 2.6 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-tools/3.3/maven-plugin-tools-3.3.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-tools/3.3/maven-plugin-tools-3.3.pom (13 KB at 16.1 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.18.1/surefire-api-2.18.1.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.18.1/surefire-api-2.18.1.pom (3 KB at 2.8 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.18.1/surefire-booter-2.18.1.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.18.1/surefire-booter-2.18.1.pom (3 KB at 3.5 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.pom (3 KB at 3.7 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/15/maven-shared-components-15.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/15/maven-shared-components-15.pom (10 KB at 7.5 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom (23 KB at 20.3 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/apache/7/apache-7.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/apache/7/apache-7.pom (15 KB at 17.2 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.18.1/maven-surefire-common-2.18.1.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.18.1/maven-surefire-common-2.18.1.jar (269 KB at 72.2 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.18.1/surefire-booter-2.18.1.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.18.1/surefire-booter-2.18.1.jar (39 KB at 34.6 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.jar (11 KB at 12.8 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.18.1/surefire-api-2.18.1.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.18.1/surefire-api-2.18.1.jar (145 KB at 31.4 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.jar (14 KB at 12.2 KB/sec)
[INFO] Tests are skipped.
[INFO]
[INFO] — maven-war-plugin:2.6:war (default-war) @ SpringBootHelloWorldExample —
[INFO] Packaging webapp
[INFO] Assembling webapp [SpringBootHelloWorldExample] in [/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/SpringBootHelloWorldExample]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/src/main/webapp]
[INFO] Webapp assembled in [375 msecs]
[INFO] Building war: /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/SpringBootHelloWorldExample.war
[INFO]
[INFO] — maven-install-plugin:2.5.2:install (default-install) @ SpringBootHelloWorldExample —
[INFO] Downloading: https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.pom (11 KB at 13.2 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.pom (4 KB at 4.8 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.pom (4 KB at 4.4 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.jar (228 KB at 51.9 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.jar (152 KB at 78.3 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar (234 KB at 65.0 KB/sec)
[INFO] Installing /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/SpringBootHelloWorldExample.war to /Users/apple/.m2/repository/org/arpit/java2blog/SpringBootHelloWorldExample/0.0.1-SNAPSHOT/SpringBootHelloWorldExample-0.0.1-SNAPSHOT.war
[INFO] Installing /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/pom.xml to /Users/apple/.m2/repository/org/arpit/java2blog/SpringBootHelloWorldExample/0.0.1-SNAPSHOT/SpringBootHelloWorldExample-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) > test-compile @ SpringBootHelloWorldExample >>>
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ SpringBootHelloWorldExample —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ SpringBootHelloWorldExample —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ SpringBootHelloWorldExample —
[INFO] Not copying test resources
[INFO]
[INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SpringBootHelloWorldExample —
[INFO] Not compiling test sources
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) < test-compile @ SpringBootHelloWorldExample <<<
[INFO]
[INFO] — spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) @ SpringBootHelloWorldExample —. ____ _ __ _ _
/\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)2017-04-28 14:20:10.519 INFO 1051 — [ main] o.a.j.SpringBootHelloWorldApplication : Starting SpringBootHelloWorldApplication on apples-MacBook-Air.local with PID 1051 (/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/classes started by apple in /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample)
2017-04-28 14:20:10.522 INFO 1051 — [ main] o.a.j.SpringBootHelloWorldApplication : No active profile set, falling back to default profiles: default
2017-04-28 14:20:10.611 INFO 1051 — [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4c624d60: startup date [Fri Apr 28 14:20:10 IST 2017]; root of context hierarchy
2017-04-28 14:20:12.697 INFO 1051 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-04-28 14:20:12.720 INFO 1051 — [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-04-28 14:20:12.725 INFO 1051 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14
2017-04-28 14:20:13.227 INFO 1051 — [ost-startStop-1] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2017-04-28 14:20:13.240 INFO 1051 — [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-04-28 14:20:13.240 INFO 1051 — [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2629 ms
2017-04-28 14:20:13.436 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: ‘dispatcherServlet’ to [/]
2017-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘characterEncodingFilter’ to: [/*]
2017-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘hiddenHttpMethodFilter’ to: [/*]
2017-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘httpPutFormContentFilter’ to: [/*]
2017-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘requestContextFilter’ to: [/*]
2017-04-28 14:20:13.807 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4c624d60: startup date [Fri Apr 28 14:20:10 IST 2017]; root of context hierarchy
2017-04-28 14:20:13.921 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/helloworld]}” onto public org.springframework.web.servlet.ModelAndView org.arpit.java2blog.springboot.HelloWorldController.hello()
2017-04-28 14:20:13.927 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/error]}” onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-04-28 14:20:13.928 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/error],produces=[text/html]}” onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse)
2017-04-28 14:20:13.966 INFO 1051 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-28 14:20:13.966 INFO 1051 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-28 14:20:14.033 INFO 1051 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-28 14:20:14.254 INFO 1051 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-04-28 14:20:14.372 INFO 1051 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-04-28 14:20:14.390 INFO 1051 — [ main] o.a.j.SpringBootHelloWorldApplication : Started SpringBootHelloWorldApplication in 4.582 seconds (JVM running for 47.545)

Step 10: Let’s test the application now.
As Spring uses embedded tomcat, you can access the project using http://localhost:8080/index.jsp.
Spring boot index
When you click on above link, you will get below screen.
Spring boot Hello World
That’s all about Spring boot web application example using JSP. If you find any issue with this project, please put a comment.

Join Our Newsletter

Subscribe to Awesome Java Content

Add Comment

Join Our Newsletter

Subscribe to Awesome Java Content