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. Java面向对象设计主要有三大特征:封装性、继承性和多态性

    Java面向对象设计主要有三大特征:封装性.继承性和多态性 一  封装性   1.1  概念:它是将类的一些敏感信息隐藏在类的类部,不让外界直接访问到,但是可以通过getter/setter方法间接访 ...

  2. springMVC+Mybatis的maven-web项目的pom.xml文件内容

    pom.xml文件内容 <!-- 第一行是XML头,指定了该xml文档的版本和编码方式 --> <project xmlns="http://maven.apache.or ...

  3. CH1802 表达式计算4

    题意 给出一个表达式,其中运算符仅包含+,-,*,/,^(加 减 乘 整除 乘方)要求求出表达式的最终值 数据可能会出现括号情况,还有可能出现多余括号情况 数据保证不会出现>=2^31的答案 数 ...

  4. nodejs之fs模块

    nodejs中的file system文件系统模块 1.文件的读取readFile //引入文件系统模块 const fs = require('fs'); //文件读取是异步操作 fs.readFi ...

  5. 【Python教程】《零基础入门学习Python》(小甲鱼)

    [Python教程]<零基础入门学习Python>(小甲鱼) 讲解通俗易懂,诙谐. 哈哈哈. https://www.bilibili.com/video/av27789609

  6. oracle之 11g RAC R2 体系结构---Grid

    -- 查看cluster 所维护的资源列表,不包括 OHAS 栈的 daemon [root@node1 bin]# ./crsctl status resource -t-------------- ...

  7. sqlserver2008事务日志已满

    --将数据库设为简单模式.日志文件自动断开. alter database CustomerInfoProject set recovery simple --查看日志文件状况  use Custom ...

  8. xunsearch的使用(二)

    1.查看配置文件vim /data/local/xunsearch/sdk/php/app/demo.ini [pid] type = id [subject] type = title [messa ...

  9. Unit01: Web概述 、 HTML概述 、 文本处理 、 图像和超链接 、 表格 、 表单

    Unit01: Web概述 . HTML概述 . 文本处理 . 图像和超链接 . 表格 . 表单 demo1.html <!-- 声明网页的版本(文档类型) --> <!doctyp ...

  10. jq 全选

    $(".checkall").change(function(){ if($(this).is(":checked")){ $(".checkchil ...