javaWeb--(5)ServletContext

在web.xml中配置全局參數mysql

<!-- 配置當前WEB應用的初始化參數 -->
  <context-param>
  	<param-name>driver</param-name>
  	<param-value>com.mysql.jdbc.Driver</param-value>
  </context-param>
  <context-param>
  	<param-name>jdbcUrl</param-name>
  	<param-value>jdbc:mysql://localhost</param-value>
  </context-param>

在servlet中獲取參數web

@Override
	public void init(ServletConfig arg0) throws ServletException {
		
		//獲取servletContext對象
		ServletContext servletContext = arg0.getServletContext();
		String diver = servletContext.getInitParameter("driver");
		System.out.println("driver:"+diver);
		
		Enumeration<String> names2 = servletContext.getInitParameterNames();
		while (names2.hasMoreElements()) {
			String name = names2.nextElement();
			System.out.println("獲取內容:"+name);
			String value = servletContext.getInitParameter(name);
			System.out.println("內容值:"+value);
		}
	}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息