簡體   English   中英

“ServletContext”中的“Context”是什么意思?

[英]What does “Context” in “ServletContext” mean?

方法getServletContextName()返回“Web應用程序”的名稱。 這意味着,“ServletContext”只不過是“Web應用程序”。 好。

API定義:

ServletContextListener接收有關它們所屬的Web應用程序的servlet上下文更改的通知。

“Web應用程序”的“servlet上下文”是什么意思? 實際上“ServletContext”中的“Context”是什么?

這個名字確實是IMO,非常糟糕。

我們必須將ServletContext讀作“基於servlet API的Web應用程序的一般上下文”。 而我們必須將ServletConfig(另一個標准類)讀作“servlet的配置”。

他們應該IMO將ServletContext命名為“WebAppContext”或“ApplicationContext”,將ServletConfig命名為“ServletContext”。

順便說一句,在JSP中,鏈接到JspPage的范圍被命名為“page”; 鏈接到HttpServletRequest的范圍被命名為“request”; 命名為HttpSession的作用域名為“session”,鏈接到ServletContext的作用域名為“”application“。

“上下文”表示..上下文 - 它具有特定Web應用程序的上下文信息和功能:

  • 應用程序范圍的參數
  • 應用程序事件監聽器
  • 有關應用程序的元數據

ServletContext是Java Web應用程序的上下文(因為它使用servlet)

Context在這里意味着Web應用。

啟動或停止Web App時, ServletContextListener會收到通知。 這樣,您可以自動運行需要在Web應用程序啟動或停止時運行的任務。

ServletContext是Web應用程序的運行時表示。

ServletContext意味着servlet的Context或Runtime環境。 Servlet在像tomcat這樣的Servlet容器中運行。 Servlet容器創建並提供運行時環境,以便servlet執行並管理其生命周期。 它還包含其他信息,如: -

  • 應用程序范圍的參數
  • 應用程序事件監聽器
  • 有關應用程序的元數據

ServletContext是一個接口,它包含一組與自己的servlet容器通信的方法。

  • 每個jvm上每個Web應用程序的上下文

這允許servlet訪問服務器的各個部分進行通信的上下文。

ServletContext的生命周期

  1. Servlet容器讀取DD(部署描述符 - web.xml),並在Web應用程序啟動時為每個創建名稱/值字符串對。
  2. Container創建新的ServletContext實例。
  3. Servlet容器為ServletContext提供對上下文init參數的每個名稱/值對的引用。
  4. 現在,同一Web應用程序中的每個servlet和JSP都可以訪問此ServletContext。 ServletConfig與ServletContext

    • ServletContext可用於Web應用程序中的所有servlet和jsp,而ServletConfig僅可用於特定的servlet。
    • Servlet配置是每個servlet一個,Servlet上下文是每個Web應用程序一個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM