Web框架的前生今世 从Servlet到Spring MVC到Spring boot

开发 架构
上世纪90年代,随着Internet和浏览器的飞速发展,基于浏览器的B/S模式随之火爆发展起来。最初,用户使用浏览器向WEB服务器发送的请求都是请求静态的资源,比如html、css等。 但是可以想象:根据用户请求的不同动态的处理并返回资源是理所当然必须的要求。

背景

上世纪90年代,随着Internet和浏览器的飞速发展,基于浏览器的B/S模式随之火爆发展起来。最初,用户使用浏览器向WEB服务器发送的请求都是请求静态的资源,比如html、css等。 但是可以想象:根据用户请求的不同动态的处理并返回资源是理所当然必须的要求。

[[273844]]

servlet的定义

  • Servlet is a technology which is used to create a web application. servlet是一项用来创建web application的技术。
  • Servlet is an API that provides many interfaces and classes including documentation. servlet是一个提供很多接口和类api及其相关文档。
  • Servlet is an interface that must be implemented for creating any Servlet.servlet是一个接口,创建任何servlet都要实现的接口。
  • Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests. servlet是一个实现了服务器各种能力的类,对请求做出响应。它可以对任何请求做出响应。
  • Servlet is a web component that is deployed on the server to create a dynamic web page.servlet是一个web组件,部署到一个web server上(如tomcat,jetty),用来产生一个动态web页面。

servlet的历史

web框架的前生今世--从servlet到spring mvc到spring boot

web Container

web容器也叫servlet容器,负责servlet的生命周期,映射url请求到相应的servlet。

A web container (also known as a servlet container;[1] and compare "webcontainer"[2]) is the component of a web server that interacts with Java servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access-rights.A web container handles requests to servlets, JavaServer Pages (JSP) files, and other types of files that include server-side code. The Web container creates servlet instances, loads and unloads servlets, creates and manages request and response objects, and performs other servlet-management tasks.A web container implements the web component contract of the Java EE architecture. This architecture specifies a runtime environment for additional web components, including security, concurrency, lifecycle management, transaction, deployment, and other services.

常见的web容器如下:

web框架的前生今世--从servlet到spring mvc到spring boot

在web容器中,web应用服务器的结构如下:

web框架的前生今世--从servlet到spring mvc到spring boot

1.普通servlet实现页面访问

web框架的前生今世--从servlet到spring mvc到spring boot

1.1 实例1:使用web.xml实现一个http服务

实现一个简单的servlet

  1. package com.howtodoinjava.servlets; 
  2.   
  3. import java.io.IOException; 
  4. import java.io.PrintWriter; 
  5.   
  6. import javax.servlet.ServletException; 
  7. import javax.servlet.http.HttpServlet; 
  8. import javax.servlet.http.HttpServletRequest; 
  9. import javax.servlet.http.HttpServletResponse; 
  10.   
  11. public class MyFirstServlet extends HttpServlet { 
  12.   
  13.  private static final long serialVersionUID = -1915463532411657451L; 
  14.   
  15.  @Override 
  16.  protected void doGet(HttpServletRequest request, 
  17.  HttpServletResponse response) throws ServletException, IOException 
  18.  { 
  19.  response.setContentType("text/html;charset=UTF-8"); 
  20.  PrintWriter out = response.getWriter(); 
  21.  try { 
  22.  // Write some content 
  23.  out.println("<html>"); 
  24.  out.println("<head>"); 
  25.  out.println("<title>MyFirstServlet</title>"); 
  26.  out.println("</head>"); 
  27.  out.println("<body>"); 
  28.  out.println("<h2>Servlet MyFirstServlet at " + request.getContextPath() + "</h2>"); 
  29.  out.println("</body>"); 
  30.  out.println("</html>"); 
  31.  } finally { 
  32.  out.close(); 
  33.  } 
  34.  } 
  35.   
  36.  @Override 
  37.  protected void doPost(HttpServletRequest request, 
  38.  HttpServletResponse response) throws ServletException, IOException { 
  39.  //Do some other work 
  40.  } 
  41.   
  42.  @Override 
  43.  public String getServletInfo() { 
  44.  return "MyFirstServlet"
  45.  } 

web.xml配置servlet

/MyFirstServlet MyFirstServlet com.howtodoinjava.servlets.MyFirstServlet MyFirstServlet /MyFirstServlet

1.2 编程方式实现一个http服务请求

不需要xml

  1. package com.journaldev.first
  2. import java.io.IOException; 
  3. import java.io.PrintWriter; 
  4. import java.util.Date
  5. import javax.servlet.ServletException; 
  6. import javax.servlet.annotation.WebInitParam; 
  7. import javax.servlet.annotation.WebServlet; 
  8. import javax.servlet.http.HttpServlet; 
  9. import javax.servlet.http.HttpServletRequest; 
  10. import javax.servlet.http.HttpServletResponse; 
  11. /** 
  12.  * Servlet implementation class FirstServlet 
  13.  */ 
  14. @WebServlet(description = "My First Servlet", urlPatterns = { "/FirstServlet" , "/FirstServlet.do"}, initParams = {@WebInitParam(name="id",value="1"),@WebInitParam(name="name",value="pankaj")}) 
  15. public class FirstServlet extends HttpServlet { 
  16.  private static final long serialVersionUID = 1L; 
  17.  public static final String HTML_START="<html><body>"
  18.  public static final String HTML_END="</body></html>"
  19.   
  20.  /** 
  21.  * @see HttpServlet#HttpServlet() 
  22.  */ 
  23.  public FirstServlet() { 
  24.  super(); 
  25.  // TODO Auto-generated constructor stub 
  26.  } 
  27.  /** 
  28.  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
  29.  */ 
  30.  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  31.  PrintWriter out = response.getWriter(); 
  32.  Date date = new Date(); 
  33.  out.println(HTML_START + "<h2>Hi There!</h2><br/><h3>Date="+date +"</h3>"+HTML_END); 
  34.  } 
  35.  /** 
  36.  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
  37.  */ 
  38.  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  39.  // TODO Auto-generated method stub 
  40.  } 

2.spring mvc实现页面访问

2.1 web.xml方式

web框架的前生今世--从servlet到spring mvc到spring boot

示例:

  1. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  2.  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  3.  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  4.  version="2.5"
  5.  <display-name>Gradle + Spring MVC Hello World + XML</display-name
  6.  <description>Spring MVC web application</description> 
  7.  <!-- For web context --> 
  8.  <servlet> 
  9.  <servlet-name>hello-dispatcher</servlet-name
  10.  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
  11.  <init-param> 
  12.  <param-name>contextConfigLocation</param-name
  13.  <param-value>/WEB-INF/spring-mvc-config.xml</param-value> 
  14.  </init-param> 
  15.  <load-on-startup>1</load-on-startup> 
  16.  </servlet> 
  17.  <servlet-mapping> 
  18.  <servlet-name>hello-dispatcher</servlet-name
  19.  <url-pattern>/</url-pattern> 
  20.  </servlet-mapping> 
  21.  <!-- For root context --> 
  22.  <listener> 
  23.  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  24.  </listener> 
  25.  <context-param> 
  26.  <param-name>contextConfigLocation</param-name
  27.  <param-value>/WEB-INF/spring-core-config.xml</param-value> 
  28.  </context-param> 
  29. </web-app> 

2.2 编码方式

  1. public class MyWebAppInitializer implements WebApplicationInitializer { 
  2.   
  3.  @Override 
  4.  public void onStartup(ServletContext container) { 
  5.  // Create the 'root' Spring application context 
  6.  AnnotationConfigWebApplicationContext rootContext = 
  7.  new AnnotationConfigWebApplicationContext(); 
  8.  rootContext.register(AppConfig.class); 
  9.   
  10.  // Manage the lifecycle of the root application context 
  11.  container.addListener(new ContextLoaderListener(rootContext)); 
  12.   
  13.  // Create the dispatcher servlet's Spring application context 
  14.  AnnotationConfigWebApplicationContext dispatcherContext = 
  15.  new AnnotationConfigWebApplicationContext(); 
  16.  dispatcherContext.register(DispatcherConfig.class); 
  17.   
  18.  // Register and map the dispatcher servlet 
  19.  ServletRegistration.Dynamic dispatcher = 
  20.  container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 
  21.  dispatcher.setLoadOnStartup(1); 
  22.  dispatcher.addMapping("/"); 
  23.  } 
  24.   
  25.  } 

内部实现

 

web框架的前生今世--从servlet到spring mvc到spring boot

3.spring boot

继承了spring mvc的框架,实现SpringBootServletInitializer

  1. package com.mkyong; 
  2. import org.springframework.boot.SpringApplication; 
  3. import org.springframework.boot.autoconfigure.SpringBootApplication; 
  4. import org.springframework.boot.builder.SpringApplicationBuilder; 
  5. import org.springframework.boot.web.support.SpringBootServletInitializer; 
  6. @SpringBootApplication 
  7. public class SpringBootWebApplication extends SpringBootServletInitializer { 
  8.  @Override 
  9.  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
  10.  return application.sources(SpringBootWebApplication.class); 
  11.  } 
  12.  public static void main(String[] args) throws Exception { 
  13.  SpringApplication.run(SpringBootWebApplication.class, args); 
  14.  } 

然后controller

  1. package com.mkyong; 
  2. import java.util.Map; 
  3. import org.springframework.beans.factory.annotation.Value; 
  4. import org.springframework.stereotype.Controller; 
  5. import org.springframework.web.bind.annotation.RequestMapping; 
  6. @Controller 
  7. public class WelcomeController { 
  8.  // inject via application.properties 
  9.  @Value("${welcome.message:test}"
  10.  private String message = "Hello World"
  11.  @RequestMapping("/"
  12.  public String welcome(Map<String, Object> model) { 
  13.  model.put("message", this.message); 
  14.  return "welcome"
  15.  } 

总结:

1.servlet的本质没有变化,从web框架的发展来看,web框架只是简化了开发servlet的工作,但还是遵循servlet规范的发展而发展的。

2.servlet的历史发展,从配置方式向编程方式到自动配置方式发展

3.spring mvc框架的分组:root和child(可以有多个dispatcherservlet),多个child可以共享root,child直接不共享

参考文献:

【1】https://en.wikipedia.org/wiki/Web_container

【2】https://baike.baidu.com/item/servlet/477555?fr=aladdin

【3】https://www.javatpoint.com/servlet-tutorial

【4】https://www.journaldev.com/1854/java-web-application-tutorial-for-beginners#deployment-descriptor

【5】https://blog.csdn.net/qq_22075041/article/details/78692780

【6】http://www.mkyong.com/spring-mvc/gradle-spring-mvc-web-project-example/

【7】http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/

责任编辑:武晓燕 来源: 今日头条
相关推荐

2023-11-02 18:01:24

SpringMVC配置

2010-04-20 11:40:52

网络爬虫

2016-11-24 22:30:17

DeepLink移动App开发

2021-11-16 11:45:00

SpringSpring ClouJava

2010-08-27 14:04:47

2021-03-10 09:21:00

Spring开源框架Spring基础知识

2022-06-02 08:37:10

架构DDDMVC

2012-09-27 13:49:54

2009-06-19 11:43:59

Spring MVC框

2009-06-19 11:28:45

2018-03-05 11:29:17

云计算云服务服务器

2015-03-09 15:26:36

2014-03-17 11:05:00

ScriptCode Blocks

2018-05-31 12:12:12

页面可视化工具

2017-08-02 14:44:06

Spring Boot开发注解

2020-11-25 11:20:44

Spring注解Java

2019-01-04 10:41:07

系统内存SRAM

2009-06-18 14:18:23

Spring secu

2009-01-03 14:39:04

ibmdwDojoMVC

2018-08-15 10:51:01

JavaSpring MVC框架
点赞
收藏

51CTO技术栈公众号