zoukankan      html  css  js  c++  java
  • What is a servletcontext

    What is a servletcontext

    ServletContext is a configuration Object which is created when web application is started. It contains different initialization parameter that can be configured in web.xml.

    Example of ServletContext with scenario

    You are developing a web application where you want to access the email of administrator in whole application. You want to set this email id @ one location and every Servlet and Jsp can access that email. In that case you will take the help of ServletContext. You can add this email in servletcontext via init parameter in web.xml. Now this value (Email id of administrator) will be available to every Jsp and Servlet in the web application.

    Life Cycle of ServletContext

    Step 1: Servlet container reads the DD (Deployment Descriptor – web.xml) and creates the name/value string pair for each <context-param> when web application is getting started.

    Step 2: Container creates the new Instance of ServletContext.

    Note*: ServletContext is an Interface.

    Step 3: Servlet container gives the ServletContext a reference to each name/value pair of the context init parameter.

    Step 4: Every servlet and JSP in the same web application will now has access to this ServletContext.

    Servlet context init parameter Syntax

    Difference from ServletConfig

    • ServletConfig is one per servlet while ServletContext is one per web application.
    • ServletContext is available to all servlet & jsp in web application while ServletConfig will be available only specific servlet.
    • Difference in the syntax for servletcontext and servletconfig.

    What is servletcontextlistener

    Now you know that you can set init parameter in ServletContext & this Servletcontext will be initialized when application starts. After initialization this servletcontext will be available to all servlet and jsp. But what if you want to perform some kind of action when context initialization/destruction process happens. This is the situation when you will take the help of servletcontextlistener.

    Bullet Points

    • Only one ServletContext is available per web application.
    • Every servlet in the same web application will have access to servletcontext.
    • It is used to access application level parameter.
    • If application is in distributed environment then there will be one servletcontext per JVM.
    • ServletContext has init parameter that can be configured in web.xml.
  • 相关阅读:
    (原创)c#学习笔记10--定义类成员01--成员定义06--自动属性
    (原创)c#学习笔记10--定义类成员01--成员定义03--定义属性
    (原创)c#学习笔记10--定义类成员01--成员定义02--建立方法
    【BZOJ3110】K大数查询(ZJOI2013)-整体二分+线段树
    【BZOJ3110】K大数查询(ZJOI2013)-整体二分+线段树
    【HDU5412】CRB and Queries-整体二分:带修改区间第K小
    【HDU5412】CRB and Queries-整体二分:带修改区间第K小
    【NOI2007T2】货币兑换-DP斜率优化+CDQ分治
    【NOI2007T2】货币兑换-DP斜率优化+CDQ分治
    【BZOJ3295】动态逆序对(CQOI2011)-CDQ分治:三维偏序
  • 原文地址:https://www.cnblogs.com/hephec/p/4556594.html
Copyright © 2011-2022 走看看