Java Servlet

Servlet 简介

一种用于创建 web 应用的技术

  • 用于创建动态 web Application
  • an API

    • 包括多个接口和类
  • 创建一个 Servlet 必须要实现 Servlet 的接口
  • 用于扩展服务器 Server, 处理 Request 的功能
  • Servlet 是一个 Web component, 用于部署到服务器来创建动态网页

Web Application

组成组件 Components

  • Servlet
  • JSP
  • Filter
  • HTML
  • CSS
  • JavaScript

JSP ---- Java Template tool to make web pages

JSP 简述

Java Server Pages

  • 响应 Request, 动态生成 Html, XML 等文档的 Web 网页技术标准
  • 以 html 模板 + 嵌入式 Java 代码的形式编写

文件后缀 *.jsp

以 Java 为脚本语言

  • 以 <% %> 标签插入 java 代码
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  <html>
    <head>
<title>第一个 JSP 程序</title>
    </head>
    <body>
<%
out.println("Hello World!");
%>
    </body>
  </html>

动态网页开发技术

以 Servlet 的形式在低层运行

环境搭建

jdk

Tomcat

JSP 调用流程

  1. Web Server 收到 Request
  2. Web Server 判断是 JSP 网页请求,把 Request 交给 JSP 引擎

Spring Boot notes

Rest Web Service

https://www.tutorialspoint.com/spring_boot/spring_boot_building_restful_web_services.htm

Resource Controller

  • URL 参数处理

类 @RestController

  • Model, View(这里没有), Controller 模型
  • 把 URL 路由相关的输入,通过方法,映射到模型 Model 来处理,并返回这 个模型
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  package com.example.restservice;

  import java.util.concurrent.atomic.AtomicLong;

  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;


  @RestController
  public class GreetingController {

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();

@GetMapping("/greeting") 
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
   return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
  }

方法 @RequestMapping

映射路由到方法