加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

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

发布时间:2019-08-17 18:35:10 所属栏目:建站 来源:架构师笔记
导读:背景 上世纪90年代,随着Internet和浏览器的飞速发展,基于浏览器的B/S模式随之火爆发展起来。最初,用户使用浏览器向WEB服务器发送的请求都是请求静态的资源,比如html、css等。 但是可以想象:根据用户请求的不同动态的处理并返回资源是理所当然必须的要
副标题[/!--empirenews.page--]

 背景

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

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

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服务请求

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读