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内容的更多相关文章

  1. PageContext ServletContext ServletConfig辨析

    上面三个东西都是什么关系呀? 先看图 注意几点 1 GenericServlet有两个init方法# 2 GenericServlet既实现了ServletConfig方法,它自己由依赖一个Servl ...

  2. 小谈-—ServletConfig对象和servletContext对象

    一.servletContext概述 servletContext对象是Servlet三大域对象之一,每个Web应用程序都拥有一个ServletContext对象,该对象是Web应用程序的全局对象或者 ...

  3. Server,Servlet,ServletConfig,ServletContext,Session,Request,Response

    Server流程 解析URL->找到应用->找到Servlet->实例化Servlet->调用init->调用service->返回响应->调用destroy ...

  4. 第一个web程序(web.xml , ServletConfig , ServletContext)

    一:第一个jsp程序 1.项目设计结构 2.新建Person.java package com.java.demo; public class Person { public void printSt ...

  5. JavaWeb学习笔记:ServletConfig()和ServletContext()

    ServletConfig()和ServletContext() 1.ServletConfig() ServletConfig是一个接口,它由server提供商来实现. ServletConfig封 ...

  6. servletconfig和servletcontext学习

    servletconfig java.lang.String getInitParameter(java.lang.String name)  //根据参数名获取参数值 java.util.Enume ...

  7. [原创]java WEB学习笔记06:ServletContext接口

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  8. ServletContext对象应用——三天免登录

    1.用到的知识点: (1)Cookie (2)Session (3)ServletContext 其中Cookie和Session是会话技术的组成部分,一次会话从打开浏览器的某个站点开始,到浏览器关闭 ...

  9. ServletContext(重要)

    一个项目只有一个ServletContext对象! 我们可以在N多个Servlet中来获取这个唯一的对象,使用它可以给多个Servlet传递数据! 这个对象在Tomcat启动时就创建,在Tomcat关 ...

随机推荐

  1. 吴裕雄--天生自然 JAVA开发学习:修饰符

    public class InstanceCounter { private static int numInstances = 0; protected static int getCount() ...

  2. 吴裕雄--天生自然 PHP开发学习:MySQL子句

    <?php $con=mysqli_connect("localhost","username","password","d ...

  3. 你需要了解的JIT Debugging

    原总结debug调试dump转储文件windbgprocdumpJIT Debugger 如果你还不清楚什么是转储文件,不知道什么时候需要转储文件,请参考转储文件系列文章的第一篇 -- 转储文件知多少 ...

  4. PAT甲级——1073 Scientific Notation (20分)

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  5. 1. Ruby基础知识

    1. Ruby执行选项 符号 作用 -c 检查代码正确性 -w 警告模式运行 -e 字面脚本 -l 行模式运行 单独 ruby -c Hello.rb 组合 ruby -le 'print " ...

  6. Oracle连接Navicat Premium遇到的问题

      ORA-28040: 没有匹配的验证协议. 通过查找资料找到了好的解决方案.可以不需要到官网上下载新的驱动来解决问题.    方法:在Oracle的安装路径下找到sqlnet.ora文件.(我的安 ...

  7. 剑指offer【09】- 跳台阶

    题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 对于本题,前提只有 一次 1阶或者2阶的跳法. a.如果两种跳法,1阶或者 ...

  8. Android音视频处理之基于MediaCodec合并音视频

    Android提供了一个MediaExtractor类,可以用来分离容器中的视频track和音频track,下面的例子展示了使用MediaExtractor和MediaMuxer来实现视频的换音: p ...

  9. 懒人JS

    1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafte ...

  10. 形参和实参|默认值|可选实参|tuple|*tuple|args|*args | **kwargs|args[:]|

    #!/usr/bin/python def hello(i,greet='long time to see!'): out = "hello "+i+" "+g ...