06 ServletConfig、ServletContext_作用域对象
//web中
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.获取ServletConfig对象
// 表示servlet的配置信息
ServletConfig config = this.getServletConfig();
//2.使用config读取初始化参数
String value = config.getInitParameter("listings");
System.out.println(value);
//获取servletName
System.out.println(config.getServletName()); } }
/*@Override
public void init(ServletConfig config) throws ServletException {
String value = config.getInitParameter("listings");
System.out.println(value);
}*/
ServletContext
//web中
<context-param>
<param-name>aaa</param-name>
<param-value>bbb</param-value>
</context-param>
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //1.获取ServletContext:servlet的全局对象,整个项目就一个ServletContext对象
ServletContext sc1 = this.getServletContext();
ServletContext sc2 = request.getServletContext();
ServletContext sc3 = this.getServletConfig().getServletContext(); //2.使用该对象
//读取全局配置信息
String val = sc1.getInitParameter("aaa");
System.out.println(val); //* 获取上下文路径
String contextPath = sc1.getContextPath();
System.out.println(contextPath); //* 获取文件的绝对路径:从WebRoot下开始定位文件
String realPath = sc1.getRealPath("/image/1.jpg");
System.out.println(realPath); //获取资源,将资源作为流返回
InputStream in = sc1.getResourceAsStream("/WEB-INF/car.properties"); //显示目录下的资源
Set<String> paths = sc1.getResourcePaths("/aaa");
for (String string : paths) {
System.out.println(string);
} }
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.request对象
request.setAttribute("name", "zhangsan");
//request.getRequestDispatcher("scope2").forward(request, response); //2.session对象 先向浏览器输入,在用下面输出
HttpSession session = request.getSession();
session.setAttribute("age", 18); //3.application对象 先向浏览器输入,所有浏览器都可以输出
ServletContext sc = request.getServletContext();
sc.setAttribute("gender", "男"); }
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.获取request对象中数据
/*String name = (String) request.getAttribute("name");
System.out.println(name); //2.获取session对象中的数据
int age = (int) request.getSession().getAttribute("age");
System.out.println(age);*/ //3.获取application对象中的数据
String gender = (String) request.getServletContext().getAttribute("gender");
System.out.println(gender); }
06 ServletConfig、ServletContext_作用域对象的更多相关文章
- JSP(二):JSP九大内置对象、四个作用域对象
jsp的九大内置对象: 内置对象: jsp文件在转译成其对应的Servlet文件的时候自动生成的并声明的对象.我们在jsp页面中直接使用即可. 注意: ...
- JSP九大内置对象和四大作用域和Servlet的三大作用域对象
一.JSP九大内置对象:内置对象(又叫隐含对象,有9个内置对象):不需要预先声明就可以在脚本代码和表达式中随意使用 内置对象特点: 由JSP规范提供,不用编写者实例化. 通过Web容器实现和管理 所有 ...
- servlet的三大作用域对象和jsp的九大内置对象及其四大作用域对象
servlet的三大作用域对象: request(HttpServletRequest) session(HttpSession): application(ServletContext):tomca ...
- Struts2的Action中如何操作作用域对象
得到作用域对象有三种方法,这里用代码来解释: package com.cy.action; import javax.servlet.ServletContext; import javax.serv ...
- day05 Servlet 开发和 ServletConfig 与 ServletContext 对象
day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...
- EL作用域对象
EL与jsp的作用域对象对应关系,,,,,及EL具体作用域对象介绍,如下
- JSP的9大内置对象和4打作用域对象
一.9大内置对象 二.4大内置作用域对象
- SpringBoot入门06-Thymeleaf显示作用域对象种的对象
作用域对象request,session, servletContext中的数据在Thymeleaf中的显示都是相同的 作用域对象中的 List和Set的集合在html中的显示是相同的 作用域对象中的 ...
- ServletConfig与ServletContext对象(接口)
ServletConfig:封装servlet的配置信息. 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数. <ser ...
随机推荐
- Linux make menuconfig打开失败
OS:Ubuntu 16.04 LTS 使用“make menuconfig”配置kernel时,提示make menuconfig打开失败 $ make menuconfig *** Unable ...
- base64和hex
base64和hex 我们知道,字符分为二种:一种是可见字符:另一种是不可见字符. 1)三种编码方式 hex也称为base16,意思是使用16个可见字符来表示一个二进制数组,编码后数据大小将翻倍,因为 ...
- GIS 空间分析案例教程-坐标高斯投影正反算
GIS 空间分析案例教程-坐标高斯投影正反算 商务科技合作:向日葵,135-4855__4328,xiexiaokui#qq.com 特点: 1. 地理处理工具,可以与任何arcgis 工具和语言集成 ...
- pycharm 中文乱码
新机的pycharm首次输出中文竟然在控制台报错了,着实让我头疼了一下午 我用的PyCharm是2018.3版本 在调用os.system()的过程中遇到了控制台中文乱码的问题,具体如下 找到对应 ...
- 在Linux下使用LLVM Clang以及Blocks
可以从这个链接下载:http://llvm.org/releases/download.html sudo apt-get install llvm sudo apt-get install clan ...
- 原生app是什么意思?
原生的就是用 Android 和ios 写的 完全符合手机系统 其他的都是通过各种工具对代码转换为手机系统可以识别
- MySQL数据库之多线程备份工具mydumper
Mydumper介绍: 1)Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具 2)特性: 轻量级C语言编写 执行速度比mysqldump快10倍 快速的文件压缩 支持导 ...
- Mysql的安全配置向导命令mysql_secure_installation
mysql_secure_installation安全配置向导 [root@youxi1 ~]# mysql_secure_installation Securing the MySQL server ...
- SpringCloud学习成长之路二 服务客户端(rest+ribbon)
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的. Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是f ...
- 使用MockMvc进行springboot调试(SpringbootTest)
测试前关闭web项目.springboot启动程序WebApplication.class 笔者本地自定了端口SpringBootTest.WebEnvironment.DEFINED_PORT 代码 ...