1. 在web.xml文件配置全局参数。
<!-- 配置全局的参数 -->
<context-param>
<param-name>userName</param-name>
<param-value>狗娃</param-value>
</context-param>
2.利用ServletContext对象读取
获取参数的方法:
1. getInitParameterNames() 获取所有的参数的名字
2.getInitParameter(name) 根据name获取参数值。
3.代码
public class ContextConfigServlet_03 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取ServletContext
ServletContext context = this.getServletContext();
String userName = context.getInitParameter("userName");
System.out.println("姓名:"+userName);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
ServletContext对象获取全局配置文件信息
猜你喜欢
转载自blog.csdn.net/chenzuen113113/article/details/80918429
今日推荐
周排行