TestServletConfig.java

package com.huawei.config;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class TestServletConfig
*/
public class TestServletConfig extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public TestServletConfig() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

/**
* 获取ServletConfig的几种方式
*
*/

//this.getServletConfig();
//重写Servlet中的init方法
}

@Override
public void init(ServletConfig config) throws ServletException {

//得到servlet的实例的名称
//获取的是 在web.xml文件中的servlet配置模块中的servlet-name里面的值
System.out.println(config.getServletName());

System.out.println(config.getInitParameter("username"));
System.out.println(config.getInitParameter("password"));

System.out.println("===========");
//得到所有的名字
Enumeration<?> names = config.getInitParameterNames();
//遍历
while(names.hasMoreElements()){
//
String name = names.nextElement().toString();
System.out.println(name+":"+config.getInitParameter(name));

}
}

}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestServletConfig</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<description></description>
<display-name>TestServletConfig-dis</display-name>
<servlet-name>TestServletConfig-name</servlet-name>
<servlet-class>com.cdsxt.config.TestServletConfig</servlet-class>

<init-param>
<param-name>username</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123123</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>TestServletConfig-name</servlet-name>
<url-pattern>/config</url-pattern>
</servlet-mapping>
</web-app>

获取web.xml配置文件中的初始化值的更多相关文章

  1. web.xml配置文件中<async-supported>true</async-supported>报错

    web.xml配置文件中<async-supported>true</async-supported>报错 http://blog.csdn.net/dream_ll/arti ...

  2. web.xml配置文件中<async-supported>true</async-supported>报错的解决方案

    为什么用到这个: ssh集成了cxf,当登录系统后,发现系统报错,控制台不断输出下面信息: 2016-05-05 11:05:06 - [http-bio-8080-exec-4] - WARN - ...

  3. web.xml配置文件中async-supported报错解决

    项目中配置spring时async-supported报错: 是因为<async-supported>true</async-supported>是web.xml 3.0的新特 ...

  4. web.xml配置文件中的async-supportedtrueasync-supported

    web.xml标题头替换为: <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" ...

  5. 使用ServletConfig获得web.xml资源中的参数

    适用:一些不需要再Servlet文件中初始化的可以使用,例如:数据库用户名和密码 //Servlet文件 //实例化ServletConfig对象  ServletConfig servletConf ...

  6. web.xml配置文件的简单说明

    简单说一下,web.xml的加载过程.当我们启动一个WEB项目容器时,容器包括(JBoss,Tomcat等).首先会去读取web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常的 ...

  7. Java web 项目 web.xml 配置文件加载过程

    转载自:http://blog.csdn.net/luoliehe/article/details/46884757#comments WEB加载web.xml初始化过程: 在启动Web项目时,容器( ...

  8. web.xml配置文件

    一.web.xml里面的标签 <display-name> <context-param> <listener> <filter> 和 <filt ...

  9. 每天学会一点点(spring-mvc.xml与web.xml配置文件)

    1.spring-mvc.xml中拦截器的使用 首先在springMVC.xml配置如下代码: <!-- 拦截器 --> <mvc:interceptors> <bean ...

随机推荐

  1. 《DSP using MATLAB》示例 Example6.7

    代码: M = 32; alpha = (M-1)/2; magHk = [1, 1, 1, 0.5, zeros(1, 25), 0.5, 1, 1]; k1 = 0:15; k2 = 16:M-1 ...

  2. 浅谈ES6新特性

    ES6的了解 新增模板字符串(为JavaScript提供了简单的字符串插值功能).箭头函数(操作符左边为输入的参数,而右边则是进行的操作以及返回的值Inputs=>outputs.).for-o ...

  3. MYSQL数据库设计之字段选择原则

    关于字段的选择其实很多地方都有进行详细的介绍,我这里只写一下我在使用过程中的心得感受.如果想要全面的了解的话,大家可以去看高性能MYSQL这一本书籍,里面有一章节介绍的特别全面,基本涉及MYSQL中全 ...

  4. django gunicorn 各worker class简单性能测试

    版权归作者所有,任何形式转载请联系作者.作者:petanne(来自豆瓣)来源:https://www.douban.com/note/564871723/ ====================== ...

  5. 数组游标实现对数组的各种操作(PHP学习)

    如何不用foreach实现对数组实现循环? 答:我们只需要模拟foreach就行了,数组在执行foreach循环的时候,是有一个游标指向当前数组循环到的值, 那如果我们能拿到这个游标,并且操作游标,使 ...

  6. java代码----substring()方法是按索引截取字符串。。。下标0开始

    总结:按照索引substring(2,5);意思是从字符串的索引为2开始(包括)到第6个字符(不包括)的位置的截取部分 package com.s.x; //substring public clas ...

  7. 20165226 2017-2018-4 《Java程序设计》第8周学习总结

    20165226 2017-2018-4 <Java程序设计>第8周学习总结 教材学习内容总结 第十二章 创建线程的方式有三种,分别是: - 继承Thread类创建线程,程序中如果想要获取 ...

  8. PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  9. nginx web服务器应用

    Nginx介绍 Nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件,因具有高并发(特别是静态资源),占用系统资源少等特性,且功能丰富而逐渐流行起来.功能应用上,Nginx不但是一个优 ...

  10. 十七.jQuery源码解析之入口方法Sizzle(1)

    函数Sizzle(selector,context,results,seed)用于查找与选择器表达式selector匹配的元素集合.该函数是选择器引擎的入口. 函数Sizzle执行的6个关键步骤如下: ...