• What is a Servlet?


    Servlet 工作原理解析

    https://www.ibm.com/developerworks/cn/java/j-lo-servlet/index.html

    你可以理解为,Spring MVC是基于servlet的,它有一个DispatherServlet,然后它负责处理请求,并且调用了你的controller。

    打一个比方,web网站是应用程序么?你可以说浏览器是一个应用程序,而web网站是让浏览器这个应用程序作为基础,成为通用的应用的容器。

    a small, server-resident program that typically runs automatically in response to user input.

    What is a Servlet?

    • A servlet is simply a class which responds to a particular type of network request - most commonly an HTTP request.
    • Basically servlets are usually used to implement web applications - but there are also various frameworks which operate on top of servlets (e.g. Struts) to give a higher-level abstraction than the "here's an HTTP request, write to this HTTP response" level which servlets provide.
    • Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc). One of the best-known open source servlet containers is Tomcat.
    • In a request/response paradigm, a web server can serve only static pages to the client
    • To serve dynamic pages, a we require Servlets.
    • Servlet is nothing but a Java program
    • This Java program doesn’t have a main method. It only has some callback methods.
    • How does the web server communicate to the servlet? Via container or Servlet engine.
    • Servlet lives and dies within a web container.
    • Web container is responsible for invoking methods in a servlets. It knows what callback methods the Servlet has.

    Flow of Request

    • Client sends HTTP request to Web server
    • Web server forwards that HTTP request to web container.
    • Since Servlet can not understand HTTP, its a Java program, it only understands objects, so web container converts that request into valid request object
    • Web container spins a thread for each request
    • All the business logic goes inside doGet() or doPost() callback methods inside the servlets
    • Servlet builds a Java response object and sends it to the container. It converts that to HTTP response again to send it to the client

    How does the Container know which Servlet client has requested for?

    • There’s a file called web.xml
    • This is the master file for a web container
    • You have information about servlet in this file- 
      • servlets
        • Servlet-name
        • Servlet-class
      • servlet-mappings- the path like /Login or /Notifications is mapped here in
        • Servlet-name
        • url-pattern
      • and so on
    • Every servlet in the web app should have an entry into this file
    • So this lookup happens like- url-pattern -> servlet-name -> servlet-class

    How to "install" Servlets? * Well, the servlet objects are inherited from the library- javax.servlet.* . Tomcat and Spring can be used to utilize these objects to fit the use case.

    Ref- Watch this on 1.5x- https://www.youtube.com/watch?v=tkFRGdUgCsE . This has an awesome explanation.

    293

    A servlet is simply a class which responds to a particular type of network request - most commonly an HTTP request. Basically servlets are usually used to implement web applications - but there are also various frameworks which operate on top of servlets (e.g. Struts) to give a higher-level abstraction than the "here's an HTTP request, write to this HTTP response" level which servlets provide.

    Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc). One of the best-known open source servlet containers is Tomcat.

    https://stackoverflow.com/questions/7213541/what-is-java-servlet

  • 相关阅读:
    docker node中uid与gid的授权问题
    windows下docker无法进行端口映射的问题
    IOS/Safari下document对象的scrollHeight值比Chrome更大
    Vue/Egg大型项目开发(二)数据库设计
    .babelrc和babel.config.js的相同配置不能合并
    es6 class中责任链模式与AOP结合
    JS设计模式(10)职责链模式(重要)
    Vue/Egg大型项目开发(一)搭建项目
    你不知道的JS(3)来聊聊this
    CentOS7为php7.2安装php-redis扩展(redis环境搭建二)
  • 原文地址:https://www.cnblogs.com/feng9exe/p/11425350.html
Copyright © 2020-2023  润新知