this is a minimal style tutorial to connnect the concepts of Java, to servlets, then to spring framework
1. servlet
tutorial
- you need: Java, tomcat
- you create two files:
- HelloWorld.java
- web.xml
HelloWorld.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("Hello, World!");
}
}
web.xml
<?xml version="1.0" ?>
<web-app>
<servlet>
<servlet-name>C6376Servlet</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>C6376Servlet</servlet-name>
<url-pattern>/ZhouURL</url-pattern>
</servlet-mapping>
</web-app>
- build and deploy:
javac HellowWorld.java -cp $CATALINA_HOME/lib/servlet-api.jar
cp HellowWorld.class $CATALINA_HOME/webapps/ROOT/WEB-INF/classes/
cp web.xml $CATALINA_HOME/webapps/ROOT/WEB-INF/
notes:
- compare with a vanilla HelloWorld Java program, a servlet is (1) an object living in the enrivoment of servlet container, e.g. a tomcat; (2) responsible of answering HTTP requests sent to a certain URL
- the example thus inclues two files: one defines the servelt; the other says which request to respond
references:
2. Spring - doing the same thing with Spring MVC and Spring Boot
tutorial
- you need Java, maven
- you create two files
- src/main/java/hello/HelloController.java
- pom.xml
src/main/java/hello/HellowController.java
package hello;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@SpringBootApplication
public class HelloController {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(HelloController.class, args);
}
}
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>hello</groupId>
<artifactId>SpringBootSampleWeb</artifactId>
<version>0.0.1</version>
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>hello.HelloController</start-class>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<!-- Additional lines to be added here... -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- build and run
mvn package
java -jar target/SpringBootSampleWeb-0.0.1.jar
notes
- it responds at the root URL ('/')
- strictly speaking, package is not required for a 'minimal' example, but it is a good habit in my opinion.
- Spring framework can work as a general 'container' of objects; Boot works as a actual servlet container on top of being a general conceptual container.
references
- Spring Boot Quick Start note how SpringBootApplication and RestController are two separate classes
- 'official' Spring Boot tutorial this is not much more than the 'minimal' one, but explains better how it works
3. View - using Spring view with a proper template
tutorial
- you need Java, maven
- create 4 files: 2 Java, 1 HTML, 1 xml
- src/main/java/hello/GreetingController.java
- src/main/java/hello/Application.java
- src/main/resources/templates/greeting.html
- pom.xml
src/main/java/hello/GreetingController.java
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
src/main/java/hello/Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
src/main/resources/templates/greeting.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
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>org.springframework</groupId>
<artifactId>gs-serving-web-content</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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>
- build and run
mvn package
java -jar target/gs-serving-web-content-0.1.0.jar
notes
- maven must be version 3 (or newer I guess)
- it is accessed from /greeting URL, or /greeting?name=Joe. nothing on the rool URL
- greeting.html serves as a 'template'. thymeleaf is used as the the template technology in the original example. you can use other templates, e.g. JSP.
- one of the magics done by Boot: 'if Thymeleaf is on your path, Spring Boot adds a SpringTemplateEngine to your application context automatically'
- Converting a Spring Boot JAR Application to a WAR
- it helps to understand what Spring Boot Maven plugin does (e.g. creating the 'über-jar' file).
refernces
- Serving Web Content with Spring MVC
- compare with legacyrest from the microservice book. legacyrest has a lot of more stuff. I am not sure whether they are useful or relavent. But it is similar to these examples.