JavaWeb基础 ServletContext getInitParameterNames 读取全局初始变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb基础 ServletContext getInitParameterNames 读取全局初始变量相关的知识,希望对你有一定的参考价值。

礼悟:
     好好学习多思考,尊师重道存感恩。叶见寻根三二一,江河湖海同一体。
          虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。




       javaEE:7           
       javaSE:1.8
          JSTL:1.2.2  
      server:tomcat 8.5
    explorer:Firefox        
             os:windows7 x64
            ide:MyEclipse 2017



工程目录结构
技术分享

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">


	<servlet>
		<servlet-name>ReadInitDataServletContext</servlet-name>
		<servlet-class>jizuiku.web.servlet.ReadInitDataServletContext</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ReadInitDataServletContext</servlet-name>
		<url-pattern>/ReadInitDataServletContext</url-pattern>
	</servlet-mapping>
	
	<context-param>
		<param-name>minNum</param-name>
		<param-value>10</param-value>
	</context-param>
	
	<context-param>
		<param-name>count</param-name>
		<param-value>100</param-value>
	</context-param>
	
	<context-param>
		<param-name>maxNum</param-name>
		<param-value>1000</param-value>
	</context-param>
	
</web-app>

 

代码

package jizuiku.web.servlet;

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

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

/**
 * 读取 所有 全局初始化变量的名称和值
 * 
 * @author 给最苦
 * @version V17.10.21
 */
public class ReadInitDataServletContext extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ServletContext application = this.getServletContext();
		Enumeration<String> e = application.getInitParameterNames();
		String name = null;
		while (e.hasMoreElements()) {
			name = e.nextElement();
			System.out.println(name + " : " + application.getInitParameter(name));
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}
}

 
效果
技术分享

 

 


学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。   

















以上是关于JavaWeb基础 ServletContext getInitParameterNames 读取全局初始变量的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb基础 ServletContext get/setAttribute 保存与读取数据

JavaWeb基础 ServletContext getAttribute 读取不存在的数据时返回null

JavaWeb基础 ServletContext 服务器未关闭前,统计一个网页的浏览次数

Servlet基础之ServletContext应用

javaweb域对象-ServletContext

javaweb-servletContext域对象