ServletContext+ServletConfig内容
ServletConfig
{
① //读取web.xml配置信息
ServletConfig config = this.getServletConfig(); //读取类名称
config.getServletName(); ②
//读取默认初始化值(自能自己类读取)
ServletConfig config = this.getServletConfig();
/*
初始化值写法
<init-param>
<param-name>xiaojiang</param-name>
<param-value>18</param-value>
</init-param>
*/
//读取某个用户的值
config.getInitParameter("xiaojiang"); ③ //批量读取默认初始化值
//获取web.xml配置
ServletContext config = this.getServletContext();
//获取web.xml中所有的默认值
Enumeration data = config.getInitParameterNames();
while(data.hasMoreElements())
{
String title = (String) data.nextElement();
String name = config.getInitParameter(title);
response.getWriter().write(name);
} } ServletContext() ①
共享数据(当web启动时创建一个域对象,实现共享数据,其他类可获取到)
ServletContext context = new ServletContext();
//创建共享的数据
context.setAttribute("xiaojiang","18");
//在另一个类中获取共享数据
context.getAttribute("xiaojiang"); ②
//读取默认初始化值(面向所有类)
ServletConfig config = this.getServletConfig();
/*
初始化值写法
<context-param>
<param-name>xiaojiang</param-name>
<param-value>18</param-value>
</context-param>
*/
//读取某个用户的值
config.getInitParameter("xiaojiang");
//批量读取默认初始化值
//获取web.xml配置
ServletContext config = this.getServletContext();
//获取web.xml中所有的默认值
Enumeration data = config.getInitParameterNames();
while(data.hasMoreElements())
{
String title = (String) data.nextElement();
String name = config.getInitParameter(title);
response.getWriter().write(name);
} ③
//ServletContext转发 //RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("要转发类的对外映射的虚拟路径");
//dispatcher.forward(request,response);
//运行后直接转发到(要转发类的对外映射的虚拟路径)去执行操作 ④
/读取配置文件 //创建 *.properties配置文件
内容如下
{
username=xiaojiang
password=admin
} /* String is = this.getServletContext().getRealPath("config.properties");
//加载资源文件
Properties prop = new Properties(); //使用InputStream访问配置文件
prop.load(new FileInputStream(is)); //使用Reader访问配置文件
prop.load(new FileReader(is)); //读取所有配置文件的值
System.out.println(prop); //读取指定用户的值
System.out.println(prop.getProperty("username"));
System.out.println(prop.getProperty("password"));
*/ 记录生活。
ServletContext+ServletConfig内容的更多相关文章
- PageContext ServletContext ServletConfig辨析
上面三个东西都是什么关系呀? 先看图 注意几点 1 GenericServlet有两个init方法# 2 GenericServlet既实现了ServletConfig方法,它自己由依赖一个Servl ...
- 小谈-—ServletConfig对象和servletContext对象
一.servletContext概述 servletContext对象是Servlet三大域对象之一,每个Web应用程序都拥有一个ServletContext对象,该对象是Web应用程序的全局对象或者 ...
- Server,Servlet,ServletConfig,ServletContext,Session,Request,Response
Server流程 解析URL->找到应用->找到Servlet->实例化Servlet->调用init->调用service->返回响应->调用destroy ...
- 第一个web程序(web.xml , ServletConfig , ServletContext)
一:第一个jsp程序 1.项目设计结构 2.新建Person.java package com.java.demo; public class Person { public void printSt ...
- JavaWeb学习笔记:ServletConfig()和ServletContext()
ServletConfig()和ServletContext() 1.ServletConfig() ServletConfig是一个接口,它由server提供商来实现. ServletConfig封 ...
- servletconfig和servletcontext学习
servletconfig java.lang.String getInitParameter(java.lang.String name) //根据参数名获取参数值 java.util.Enume ...
- [原创]java WEB学习笔记06:ServletContext接口
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- ServletContext对象应用——三天免登录
1.用到的知识点: (1)Cookie (2)Session (3)ServletContext 其中Cookie和Session是会话技术的组成部分,一次会话从打开浏览器的某个站点开始,到浏览器关闭 ...
- ServletContext(重要)
一个项目只有一个ServletContext对象! 我们可以在N多个Servlet中来获取这个唯一的对象,使用它可以给多个Servlet传递数据! 这个对象在Tomcat启动时就创建,在Tomcat关 ...
随机推荐
- 01 语言基础+高级:1-6 集合_day03【List、Set、Collections工具类】
day03 [List.Set.数据结构.Collections] 主要内容 数据结构 List集合 Set集合 Collections 第一章 数据结构 2.1 数据结构有什么用 我们将常见的数据结 ...
- recurrent NN
RNN应用到音乐数据,资料以及代码 http://www-etud.iro.umontreal.ca/~boulanni/icml2012 Modeling Temporal Dependencies ...
- day56-mysql-其他查询-面试题-重点
一.重点 面试题 1. 临时表查询: -- 查询高于本部门平均工资的人员 #1.先查询部门人员的平均工资 SELECT dept_id,AVG(salary)as sal from person GR ...
- 第一章:ESXi6.7虚拟化环境安装
1.1 硬件环境及镜像引导准备 1.1.1 硬件和系统资源 要安装ESXi6.7,硬件和系统资源必须满足下列要求: ESXi 6.7 要求主机至少具有两个 CPU 内核,生产环境中需要根据 ...
- Servlet&JSP复习笔记 02
1.Servlet获取请求参数 获取请求参数依靠的是表单元素的name属性,广泛意义来说id属性是给客户端使用的,name属性是服务器使用的. a.获取Name-Value的方法: - getPara ...
- opencv处理图像
import cv2 import numpy import os img = numpy.zeros((3, 3), dtype=numpy.uint8) print(img) img = cv2. ...
- Softether使用本地网桥
https://maytalkhao.com/archives/826 以下步骤都是按照上面这篇文章来的,具体细节如下 一.使用Softether VPN Server Manager软件添加本地网 ...
- oracle_(第二课)监听器配置
一. 1.首先我们得安装好oracle数据库(上一课有讲) 再来看看我们没有监听器之前的文件 文件路径:%ORACLE_HOME%/NETWORK/ADMIN 2.关闭所有防火墙,win10的要特别注 ...
- ZOJ-1610 Count the Colors(线段树染色,求染色段)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 https://vjudge.net/contest/318019# ...
- 吴裕雄--天生自然python学习笔记:python 建立 Firebase 数据库连接
Python 程序通过 python-firebase 包可以存取 Firebase 数据库. 使用 python-firebase 包 首先必须安装 python-firebase 包,安装方法如下 ...