package com.servlet;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet03 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
PrintWriter out = response.getWriter();
// 得到ServletContext对象
ServletContext context = this.getServletContext();
// 得到包含所有初始化参数名的Enumeration对象
Enumeration<String> paramNames = context.getInitParameterNames();
// 遍历所有的初始化参数名,得到相应的参数值,打印到控制台
out.println("all the paramName and paramValue are following:");
// 遍历所有的初始化参数名,得到相应的参数值并打印
while (paramNames.hasMoreElements()) {
String name = paramNames.nextElement();
String value = context.getInitParameter(name);
out.println(name + ": " + value);
out.println("<br>");
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
this.doGet(request, response);
}
}
【通过 ServletContext 查询所有的初始化参数】
最新推荐文章于 2025-11-26 15:59:01 发布
该博客主要介绍了如何在Servlet中获取和遍历应用的初始化参数。通过`ServletContext`对象,可以获取到所有初始化参数名的枚举,并依次打印出每个参数名及其对应的值,展示了在Servlet环境中配置和使用初始化参数的基本操作。
1693

被折叠的 条评论
为什么被折叠?



