当前位置: 首页 > news >正文

淮安市建设工程施工图审查处网站/chatgpt 网站

淮安市建设工程施工图审查处网站,chatgpt 网站,深圳定制网站,公司网站费怎么做分录💗wei_shuo的个人主页 💫wei_shuo的学习社区 🌐Hello World ! ServletContext对象 概述 全局对象,拥有作用域,对应Tomcat的Web应用Web服务器启动时,会为每一个Web应用程序创建一块共享的存储区…

在这里插入图片描述

💗wei_shuo的个人主页

💫wei_shuo的学习社区

🌐Hello World !


ServletContext对象

概述

  • 全局对象,拥有作用域,对应Tomcat的Web应用
  • Web服务器启动时,会为每一个Web应用程序创建一块共享的存储区域(ServletContext)
  • ServletContext在Web服务器启动时创建,服务器关闭时销毁

获取ServletContext对象

  • GenericServlet提供 getServletContext(); 方法 this.getServletContext();
  • HttpServletRequest提供 getServletContext(); 方法
  • HttpSession提供 getServletContext(); 方法
//获取ServletContext对象
ServletContext servletContext = this.getServletContext();//通过request对象,获取ServletContext对象
ServletContext servletContext1 = request.getServletContext();//通过request获取session对象,获取ServletContext对象
HttpSession session = request.getSession();
ServletContext servletContext2 = session.getServletContext();

ServletContext 作用

  • 获取项目真实路径(获取当前项目在服务器发布的真实路径)
String realpath = servletContext.getRealPath("/")
        //获取ServletContext对象ServletContext servletContext = this.getServletContext();//获取项目真实路径System.out.println(servletContext.getRealPath("/"));/*输出结果D:\Eclipse\apache-tomcat-8.5.82-windows-x64\apache-tomcat-8.5.82\webapps\Servlet_Projects_war\*/
  • 获取项目上下文路径(获取当前项目上下文路径:应用程序名称)
System.out.println(servletContext.getContextPath());
System.out.println(request.getContextPath());
//获取ServletContext对象
ServletContext servletContext = this.getServletContext();
//获取项目上下文路径
System.out.println(servletContext.getContextPath());
System.out.println(request.getContextPath());
/*
输出结果:
/Servlet_Projects_war
/Servlet_Projects_war
*/
  • 全局容器

    ServletContext拥有作用域,可以存储数据到全局容器中

    • 存储数据: servletContext.setAttribute(“name”,“value”);
    • 获取数据: servletContext.getAttribute(“name”);
    • 移除数据: servletContext.removeAttribute(“name”);
  • ServletContextController类

@WebServlet(name = "ServletContextController", value = "/ctxController")
public class ServletContextController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = this.getServletContext();//获取项目真实路径System.out.println(servletContext.getRealPath("/"));//存储数据servletContext.setAttribute("context","info");}
}
  • ShowContextController类
@WebServlet(name = "ShowContextController", value = "/ShowController")
public class ShowContextController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = this.getServletContext();//获取数据String context = (String) servletContext.getAttribute("context");System.out.println(context);/*输出结果:D:\Eclipse\apache-tomcat-8.5.82-windows-x64\apache-tomcat-8.5.82\webapps\Servlet_Projects_war\info*/}
}

ServletContext 特点

  • 唯一性:一个应用对应一个ServletContext
  • 生命周期:只要容器不关闭或者应用不卸载,ServletContext就一直存在

ServletContext 应用场景

  • CounterController类
@WebServlet(name = "CounterController", value = "/counterController")
public class CounterController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = request.getServletContext();//获取计数器Integer counter = (Integer) servletContext.getAttribute("counter");//计数器初始化if (counter == null) {counter = 1;//counter存入计数器servletContext.setAttribute("counter", counter);} else {counter++;servletContext.setAttribute("counter", counter);}System.out.println("counter:" + counter);}
}
  • ShowCounterController类
@WebServlet(name = "ShowCounterController", value = "/ShowCounterController")
public class ShowCounterController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = request.getServletContext();//获取计数器Integer counter = (Integer) servletContext.getAttribute("counter");//计数器初始化if (counter == null) {counter = 1;//counter存入计数器servletContext.setAttribute("counter", counter);} else {counter++;servletContext.setAttribute("counter", counter);}System.out.println("show:" + counter);}
}
  • 结果
counter:1
show:2
show:3
show:4
show:5
show:6
show:7

作用域总结

  • HttpServletRequest:一次请求,请求响应之前有效
  • HttpSession:一次会话开始,浏览器不关闭或不超时之前有效
  • ServletContext:服务器启动开始,服务器停止之前有效

🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝


在这里插入图片描述

http://www.jmfq.cn/news/4947985.html

相关文章:

  • 网站关键词越多越好吗/网址查询域名
  • 免费个人网站模板/郑州计算机培训机构哪个最好
  • 沈阳做网站的公司推荐/广告投放平台
  • 公司做网站找谁做网站的公司/天津seo霸屏
  • 四川省的住房和城乡建设厅网站/上海最新政策
  • 个人网站 服务器/需要推广的app在哪里找
  • 做网站横幅 的网站推荐几个/超八成搜索网站存在信息泄露问题
  • 用来做区位分析的地图网站/最快新闻资讯在哪看
  • 自己建网站做推广/无锡百度公司王东
  • jsp网站开发教学/百度网站推广申请
  • 北京最大网站建设公司排名/全网投放广告的渠道有哪些
  • 电商网站方案/百度关键词排名点击
  • 做网站用微软雅黑侵权吗/怎么做网站推广多少钱
  • 晋州做网站/视频号下载器手机版
  • 做网站用笔记本做服务器吗/网络推广软文范文
  • 医院做网站定位/有什么引流客源的软件
  • 哪个网站可以做兼职ppt/游戏代理平台哪个好
  • 电脑网站转换手机网站怎么做/南宁seo产品优化服务
  • 外贸网站建设哪家比较好/站内seo优化
  • 品牌形象网站建设/拼多多代运营公司十大排名
  • 怎样利用网站做推广/北京网站seo公司
  • 推荐几个网站/百度ai入口
  • 苏州网站公司/优化近义词
  • 阳谷网站建设/全球网站排名查询
  • 公司网站建设情况说明/网站优化公司大家好
  • 网站建设及验收标准/网址百度刷排名
  • 网站备案号在哪儿查询/软文有哪些发布平台
  • 万盛集团网站建设/恩施seo整站优化哪家好
  • 微网站建设教程视频教程/公司企业网站制作需要多少钱
  • 网站推广优化平台/如何进行网络推广和宣传