Quick Contact

    Hibernate

     

    Deploying Spring Boot WAR file with JSP to Tomcat

    Here we can deploy a Spring Boot application using JSP to external Tomcat server. First we create a simple spring boot web application with JSP then we will create a WAR file of it and finally we deploy the WAR file in the external tomcat server.

    Development Steps

    1. Create Spring Boot Application
    2. Add maven dependencies
    3. Configure InternalResourceViewResolver for JSPs in application.properties file
    4. Create JSP page
    5. Create WAR file
    6. Deploy to Tomcat
    1. Create a Spring Boot application

      There are many ways to create a Spring Boot application. You can refer below articles to create a Spring Boot application.

    2. Add maven dependencies

      < ?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      < modelVersion>4.0.0< /modelVersion>
      < parent>
      < groupId>org.springframework.boot< /groupId>
      < artifactId>spring-boot-starter-parent< /artifactId>
      < version>2.1.0.RELEASE< /version>
      < relativePath />
      < !-- lookup parent from repository -->
      < /parent>
      < groupId>net.ducatindia< /groupId>
      < artifactId>spring-data-war< /artifactId>
      < version>0.0.1-SNAPSHOT< /version>
      < packaging>war< /packaging>
      < name>spring-data-war< /name>
      < description>Demo project for Spring Boot Restful web services< /description>
      < properties>
      < java.version>1.8< /java.version>
      < /properties>
      < dependencies>
      < dependency>
      < groupId>org.springframework.boot< /groupId>
      < artifactId>spring-boot-starter-web< /artifactId>
      < /dependency>
      < !-- Need this to compile JSP -->
      < dependency>
      < groupId>org.apache.tomcat.embed< /groupId>
      < artifactId>tomcat-embed-jasper< /artifactId>
      < scope>provided< /scope>
      < /dependency>
      < dependency>
      < groupId>org.springframework.boot< /groupId>
      < artifactId>spring-boot-starter-tomcat< /artifactId>
      < scope>provided< /scope>
      < /dependency>
      < dependency>
      < groupId>org.springframework.boot< /groupId>
      < artifactId>spring-boot-starter-test< /artifactId>
      < scope>test< /scope>
      < exclusions>
      < exclusion>
      < groupId>org.junit.vintage< /groupId>
      < artifactId>junit-vintage-engine< /artifactId>
      < /exclusion>
      < /exclusions>
      < /dependency>
      < /dependencies>
      < build>
      < finalName>springboot-jsp-web< /finalName>
      < plugins>
      < plugin>
      < groupId>org.springframework.boot< /groupId>
      < artifactId>spring-boot-maven-plugin< /artifactId>
      < /plugin>
      < /plugins>
      < /build>
      < /project>
      
    3. Create HelloWorldController

      Let’s create a simple HelloWorldController class which exposes a “/test” request mapping:

      packagenet.ducatindia.springboot;
      
      importorg.springframework.stereotype.Controller;
      importorg.springframework.web.bind.annotation.RequestMapping;
      
      @Controller
      publicclassHelloWorldController {
      @RequestMapping("/test")
      publicStringsayHello() {
      return"hello";
          }
      }
      
    4. Configure InternalResourceViewResolver for JSPs in application.properties file

      spring.mvc.view.prefix=/WEB-INF/view/

      spring.mvc.view.suffix=.jsp

    5. Create JSP page

      Create JSP view pages under src/main/webapp/WEB-INF/view directory ( If the directory does not exist then create directory structure):

      <%@ pagelanguage="java"contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
      < !DOCTYPE html>
      < html>
      < head>
      < metacharset="ISO-8859-1">
      < title>Insert title here< /title>
      < /head>
      < body>
      < p>
          Hello World! Time is <%=newjava.util.Date() %>
      < /p>
      
      < p>
          We are running on  <%=application.getServerInfo() %>!!!
      < /p>
      
      < /body>
      < /html>
      
    6. Create WAR file

      To create a WAR file, perform the following steps.

      1. Update main Spring Boot application

        In your main Spring Boot application, you need to extend the SpringBootServletInitializer and override the configure(…) method:

        packagenet.ducatindia.springboot;
        
        importorg.springframework.boot.SpringApplication;
        importorg.springframework.boot.autoconfigure.SpringBootApplication;
        importorg.springframework.boot.builder.SpringApplicationBuilder;
        importorg.springframework.boot.web.servlet.support.SpringBootServletInitializer;
        
        @SpringBootApplication
        publicclassSpringDataWarApplicationextendsSpringBootServletInitializer {
        
        @Override
        protectedSpringApplicationBuilderconfigure(SpringApplicationBuilderapplication) {
        returnapplication.sources(SpringDataWarApplication.class);
            }
        
        publicstaticvoidmain(String[] args) {
        SpringApplication.run(SpringDataWarApplication.class, args);
            }
        }
        
      2. Update Maven POM file

          • Update your POM.xml to use WAR packaging

        < packaging>war< /packaging>

          • In POM.xml, add dependency to be able to compile JSPs
        < dependency>
        < groupId>org.apache.tomcat.embed< /groupId>
        < artifactId>tomcat-embed-jasper< /artifactId>
        < scope>provided< /scope>
        < /dependency>
        
          • Make sure the Tomcat embedded does not interfere with the external Tomcat server.
        < dependency>
        < groupId>org.springframework.boot< /groupId>
        < artifactId>spring-boot-starter-tomcat< /artifactId>
        < scope>provided< /scope>
        < /dependency>
        
          • Create the WAR file with the command:

        mvn clean install

        This will generate a WAR file in your project directory: target/springboot-jsp-web.war

    7. Deploy to Tomcat

      Copy your WAR file to the < tomcat-server>/webapps directory, wait for few seconds for Tomcat to deploy your app.

      In a web browser, access your app at: http://localhost:8080/springboot-jsp-web/test

      If everything is successful, you will see your application’s web page.

     

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.