博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
What is a servletcontext
阅读量:5216 次
发布时间:2019-06-14

本文共 2898 字,大约阅读时间需要 9 分钟。

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.

转载于:https://www.cnblogs.com/hephec/p/4556594.html

你可能感兴趣的文章
minggw 安装
查看>>
Jquery操作cookie,实现简单的记住用户名的操作
查看>>
[BZOJ1196][HNOI2006]公路修建问题 二分答案+最小生成树
查看>>
PHP基础入门(二)
查看>>
[Luogu P3119] [USACO15JAN]草鉴定Grass Cownoisseur (缩点+图上DP)
查看>>
【原创】大数据基础之Zookeeper(4)应用场景
查看>>
18款在线代码片段测试工具
查看>>
20.C++- &&,||逻辑重载操作符的缺陷、,逗号重载操作符的分析
查看>>
静态变量数组实现LRU算法
查看>>
在SQL中怎么把一列字符串拆分为多列
查看>>
中文系统 上传file的input显示英文
查看>>
css样式写一个三角形
查看>>
比callback更简洁的链式执行promise
查看>>
android permission
查看>>
javascript获取textarea中所选文本的开始位置、结束位置和选择的文本
查看>>
【译】在Asp.Net中操作PDF - iTextSharp - 使用字体
查看>>
事务备份还原分离附加
查看>>
JSch - Java实现的SFTP(文件上传详解篇)
查看>>
一些注意点
查看>>
.net 文本框只允许输入XX,(正则表达式)
查看>>