自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 架構(gòu)
上世紀(jì)90年代,隨著Internet和瀏覽器的飛速發(fā)展,基于瀏覽器的B/S模式隨之火爆發(fā)展起來(lái)。最初,用戶使用瀏覽器向WEB服務(wù)器發(fā)送的請(qǐng)求都是請(qǐng)求靜態(tài)的資源,比如html、css等。 但是可以想象:根據(jù)用戶請(qǐng)求的不同動(dòng)態(tài)的處理并返回資源是理所當(dāng)然必須的要求。

背景

上世紀(jì)90年代,隨著Internet和瀏覽器的飛速發(fā)展,基于瀏覽器的B/S模式隨之火爆發(fā)展起來(lái)。最初,用戶使用瀏覽器向WEB服務(wù)器發(fā)送的請(qǐng)求都是請(qǐng)求靜態(tài)的資源,比如html、css等。 但是可以想象:根據(jù)用戶請(qǐng)求的不同動(dòng)態(tài)的處理并返回資源是理所當(dāng)然必須的要求。

[[273844]]

servlet的定義

  • Servlet is a technology which is used to create a web application. servlet是一項(xiàng)用來(lái)創(chuàng)建web application的技術(shù)。
  • Servlet is an API that provides many interfaces and classes including documentation. servlet是一個(gè)提供很多接口和類api及其相關(guān)文檔。
  • Servlet is an interface that must be implemented for creating any Servlet.servlet是一個(gè)接口,創(chuàng)建任何servlet都要實(shí)現(xiàn)的接口。
  • Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests. servlet是一個(gè)實(shí)現(xiàn)了服務(wù)器各種能力的類,對(duì)請(qǐng)求做出響應(yīng)。它可以對(duì)任何請(qǐng)求做出響應(yīng)。
  • Servlet is a web component that is deployed on the server to create a dynamic web page.servlet是一個(gè)web組件,部署到一個(gè)web server上(如tomcat,jetty),用來(lái)產(chǎn)生一個(gè)動(dòng)態(tài)web頁(yè)面。

servlet的歷史

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

web Container

web容器也叫servlet容器,負(fù)責(zé)servlet的生命周期,映射url請(qǐng)求到相應(yīng)的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.

常見(jiàn)的web容器如下:

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

在web容器中,web應(yīng)用服務(wù)器的結(jié)構(gòu)如下:

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

1.普通servlet實(shí)現(xiàn)頁(yè)面訪問(wèn)

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

1.1 實(shí)例1:使用web.xml實(shí)現(xiàn)一個(gè)http服務(wù)

實(shí)現(xiàn)一個(gè)簡(jiǎn)單的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 編程方式實(shí)現(xiàn)一個(gè)http服務(wù)請(qǐng)求

不需要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實(shí)現(xiàn)頁(yè)面訪問(wèn)

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.  } 

內(nèi)部實(shí)現(xiàn)

 

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

3.spring boot

繼承了spring mvc的框架,實(shí)現(xiàn)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.  } 

總結(jié):

1.servlet的本質(zhì)沒(méi)有變化,從web框架的發(fā)展來(lái)看,web框架只是簡(jiǎn)化了開發(fā)servlet的工作,但還是遵循servlet規(guī)范的發(fā)展而發(fā)展的。

2.servlet的歷史發(fā)展,從配置方式向編程方式到自動(dòng)配置方式發(fā)展

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

參考文獻(xiàn):

【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/

責(zé)任編輯:武曉燕 來(lái)源: 今日頭條
相關(guān)推薦

2023-11-02 18:01:24

SpringMVC配置

2016-11-24 22:30:17

DeepLink移動(dòng)App開發(fā)

2010-04-20 11:40:52

網(wǎng)絡(luò)爬蟲

2021-11-16 11:45:00

SpringSpring ClouJava

2010-08-27 14:04:47

2025-02-25 00:11:40

Servlet服務(wù)器Web

2022-06-02 08:37:10

架構(gòu)DDDMVC

2025-03-05 11:03:36

2025-02-27 13:00:00

SpringBoot數(shù)據(jù)鑒權(quán)代碼

2025-02-18 13:00:00

SpringBoot事務(wù)管理代碼

2009-06-19 11:43:59

Spring MVC框

2021-03-10 09:21:00

Spring開源框架Spring基礎(chǔ)知識(shí)

2009-06-19 11:28:45

2012-09-27 13:49:54

2018-03-05 11:29:17

云計(jì)算云服務(wù)服務(wù)器

2015-03-09 15:26:36

2025-04-08 02:22:22

SpringJackson注解

2018-05-31 12:12:12

頁(yè)面可視化工具

2014-03-17 11:05:00

ScriptCode Blocks

2017-08-02 14:44:06

Spring Boot開發(fā)注解
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)