(JavaWeb)ServletContext对象
文章目录
- ServletContext
- 1.共享数据
- 2.获取初始化参数
- 3.请求转发
- 4.读取资源文件
ServletContext
- web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
1.共享数据
Servlet1可以将数据存放在ServletContext中,Servlet2和Servlet3可以在ServletContext中取得Servlet1在ServletContext中存放的数据。
在HelloServlet中存放String数据 伊泽瑞尔。
public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取servletContext对象ServletContext servletContext = this.getServletContext();String name = "伊泽瑞尔";//以键值对的形式将数据放入servletContext对象中servletContext.setAttribute("name",name);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
在GetNameServlet中取得数据并显示在网站上
public class GetNameServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = this.getServletContext();//获取HelloServlet中在获取ServletContext对象中存放的数据String name = (String) servletContext.getAttribute("name");//设置响应信息为中文resp.setContentType("text/html");resp.setCharacterEncoding("utf-8");resp.getWriter().write(name);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
先访问HelloServlet页面将数据存放在ServletContext中,再访问GetNameServlet拿出数据并显示在网页。
2.获取初始化参数
<!--配置一些web应用初始化参数--><context-param><param-name>url</param-name><param-value>jdbc:mysql://localhost:3306/mybatis</param-value></context-param>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext context = this.getServletContext();String url = context.getInitParameter("url");resp.getWriter().print(url);
}
3.请求转发
public class ServletDemo01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//创建servletContext对象ServletContext servletContext = this.getServletContext();//请求转发servletContext.getRequestDispatcher("/getName").forward(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
运行,显示的是GetNameServlet的页面,这里null是由于没有访问hello的缘故。
显示的是GetNameServlet的页面,但是路径却还是自己的。
如图,A向B请求资源,B向C请求资源,B将C返回的资源再返回给A,A自始至终没有接触C,所以显示的还是B的路径。
4.读取资源文件
Properties
- 在resources目录下新建db.properties文件
username=ez
password=123456
public class ServletDemo02 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//创建servletContext对象ServletContext servletContext = this.getServletContext();//获取properties文件的字节输入流对象InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");//创建properies对象Properties prop = new Properties();//加载流prop.load(is);String user = prop.getProperty("username");String pwd = prop.getProperty("password");//显示resp.getWriter().write(user+":"+pwd);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
(JavaWeb)ServletContext对象相关推荐
- javaweb_笔记1(系统架构;servlet分析,注册,生命周期;Servletconfig对象(四个方法)和Servletcontext对象;http协议,get,post)
1.关于系统架构 1.1系统架构包括 C/S架构: Client/Server(客户端/服务器) 例如电脑上安装的各种软件,qq等,需要安装特定的软件 优点: 速度快(软件中数据大部分在客户端中,少量 ...
- java servletcontext_Java ServletContext对象用法解析
ServletContext对象: ServletContext类似字节码文件对象,在web创建的时候就自动生成了,并且是唯一的,跟随着项目和服务器共存亡了.通过这个对象,我们可以向里面存数据(键值对 ...
- ServletContext对象详解
ServletContext对象 1. 概念:代表整个web应用,可以和程序的容器(服务器)来通信 2. 获取: 1. 通过request对象获取 request.getSer ...
- 使用ServletContext对象完成网页计数器
使用ServletContext对象完成网页计数器 * 在用户登录校验中创建计数器并自增,然后存储到ServletContext对象中 * 在主页面里取出计数器数据显示给 ...
- ServletContext 对象
ServletContext 对象 问题: Request 解决了一次请求内的数据共享问题,session 解决了 用户不同请求的数据共享问题,那么不同的用户的数据 共享该怎 ...
- ServletConfig对象和ServletContext对象
ServletConfig对象是servlet配置对象,(web.xml中的)servlet信息封装在ServletConfig对象中,因此在一个web应用可存在多个ServletConfig.Ser ...
- 秒懂servletContext对象
servletContext对象 ServletContext对象,官方称为servlet上下文:服务器会为每一个web应用创建一个servletContext对象,它具有全局唯一性,web应用中的所 ...
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法...
今天遇到了一个在servlet的service方法中获取ServletContext对象出现java.lang.NullPointerException(空指针)异常,代码如下: String pat ...
- java中servletcontext_Java中的ServletContext对象
ServletContext对象: ServletContext类似字节码文件对象,在web创建的时候就自动生成了,并且是唯一的,跟随着项目和服务器共存亡了.通过这个对象,我们可以向里面存数据(键值对 ...
最新文章
- android ui动画效果怎么做,AndroidUI 布局动画-为列表添加布局动画效果
- 树莓派安装OpenELEC
- 电脑上显示没有其他服务器服务,电脑显示没有远程服务器地址
- arduino nano 蓝牙_用Arduino玩转掌控板(ESP32):ESP32概述与Arduino软件准备
- EJS 模板中,js 如何获取后端传来的数据
- 计数信号量的删除与状态查询
- pythonfor循环案例教程_python开发之for循环操作实例详解,pythonfor实例详解
- C++实现N选R的实现算法(附完整源码)
- Vue监听滚动条事件 点击回到顶部
- Android之JSON处理器FastJson
- redis 哨兵 异步_突破Java面试(23-8) - Redis哨兵主备切换的数据丢失问题-阿里云开发者社区...
- 北大青鸟:比尔盖茨:我在微软的10大失误
- php isinstance,Python issubclass和isinstance
- Hive中HSQL中left semi join
- NLP --- 最大熵模型的解法(GIS算法、IIS算法)
- Android系统Surface机制的SurfaceFlinger服务渲染应用程序UI的过程分析(2)
- DPDK-VPP 学习笔记-01
- 物联网技术体系主要分为哪些关键技术,具体包含哪些技术?
- Android总结笔记04:仿QQ空间登录UI,解决软键盘弹出挡住输入框的问题
- 绘制风向、风速玫瑰图
热门文章
- C++ Primer 5th笔记(chap 19 特殊工具与技术)union
- C++ Primer 5th笔记(chap 12 动态内存)智能指针概述
- BUUCTF--[GWCTF 2019]re3学习记录
- MySQL—不相关子查询(多行子查询)
- matlab入门操作
- Microsoft RTF栈溢出漏洞(CVE-2010-3333)漏洞分析
- Python多线程之构建线程池
- 方框(HPU暑期第四次积分赛)
- 1008 Elevator (20 分)【难度: 简单 / 知识点: 模拟】
- 1.4 os的运行机制和体系结构