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

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

发布时间:2019-08-15 19:32:44 所属栏目:建站 来源:架构师笔记
导读:配景 上世纪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处事哀求

(编辑:湖南网)

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

热点阅读