Spring Boot: Sample web app with embedded tomcat

Datetime:2016-08-23 03:53:33          Topic: Tomcat           Share

In this post we will see how to create a view page and way to render it. Here we will use embedded tomcat as a container as well.

You can easily understand and run this example if you know Spring MVC, some configuration and dependency related changes are required.

Let’s see files used in this example in detail.

pom.xml

Code

<?xmlversion="1.0" encoding="UTF-8"?>
<projectxmlns="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.kode12</groupId>
 <artifactId>simple-web-app-tomcat</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 
 <name>simple-web-app-tomcat</name>
 <description>Sampleprojectto showuse ofembeddedtomcatas a container.</description>
 
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.3.3.RELEASE</version>
 <relativePath /> <!-- lookupparent fromrepository -->
 </parent>
 
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <java.version>1.7</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-starter-tomcat</artifactId>
 </dependency>
 
 <dependency>
 <groupId>org.apache.tomcat.embed</groupId>
 <artifactId>tomcat-embed-jasper</artifactId>
 </dependency>
 
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>
 
 <build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
 </build>
 
</project>

Only 2 new dependency added here is

  • spring-boot-starter-tomcat:
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    
    This starter will add required dependency for embedded tomcat, screen below shows what exactly downloaded by maven.
  • tomcat-embed-jasper:
    <dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    
    This dependency provide JSP support for tomcat. Boot has some limitations related to JSP as per Spring docs. ( Read more…

    )

    When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

    • With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work because of a hard coded file pattern in Tomcat.
    • Jetty does not currently work as an embedded container with JSPs.
    • Undertow does not support JSPs.

    So to get JSP support we need to add this dependency.

application.properties

Code

server.port=8081
 
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp

Here boot provides built-in properties to set prefix and suffix for view, which is exactly same in Spring MVC like

<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/WEB-INF/view/" />
 <property name="suffix" value=".jsp" />
</bean>

WelcomeController.java

Code

package com.kode12.simplewebapptomcat.controller;
 
import java.util.Date;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
@RequestMapping("/")
public class WelcomeController {
 
 @RequestMapping("/welcome")
 public String welcome(ModelMapmap) {
 map.put("currentDate", new Date());
 return "welcome";
 }
}

Here we just created on controller annotated by @Controller annotation and one method which returns view name as a String.

welcome.jsp

Code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
 <metahttp-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>kode12.com | Welcome</title>
 </head>
 
 <body>
 Welcome koder! Current Date is ${currentDate }
 </body>
</html>

This file created under /WEB-INF/view directory and used to display message and render variable currentDate which is set from WelcomeController.java .

SimpleWebAppTomcatApplication.java

Code

package com.kode12.simplewebapptomcat;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SimpleWebAppTomcatApplication {
 
 public static void main(String[] args) {
 SpringApplication.run(SimpleWebAppTomcatApplication.class, args);
 }
}

As we see in previous demos file contain main method and used as a starting point of boot application.

Run an application

To run this example execute method from SimpleWebAppTomcatApplication.java file

And open http://localhost:8081/welcome url in your favorite browser., output will be shown in below image.

At Last, i would like to hear from you guys for some mistakes, new blogs, demo, ideas or anything which will help us to make our blog better.

Share current post by copy: http://goo.gl/pVMljU





About List