天天看點

Sevlet_ServletContext

ServletContext概述

           Servlet引擎為每一個WEB應用程式都建立了一個對應的ServletContext對象,ServletContext對象被包含在ServletConfig對象中,調用ServletConfig.getServletContext()方法可以傳回ServletContext對象的引用。由于一個WEB應用程式中的所有的Servlet都是共享一個ServletContext對象,是以,ServletContext對象被稱之為application對象(也被稱為Web應用程式對象)。

ServletContext的功能以及代碼實作

1.擷取WEB應用程式中的初始化參數

在web.xml中配置初始化參數

<!--配置目前的WEB應用的初始化參數-->
    <context-param>
    <param-name>user</param-name>
    <param-value>root</param-value>
    </context-param>

    <context-param>
     <param-name>password</param-name>
     <param-value>123456</param-value>
   </context-param>
  
  <servlet>
     <servlet-name>HelloWorld</servlet-name>
     <servlet-class>com.cn.test.HelloWorld</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>

           

擷取配置的參數

public class HelloWorld extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);		
		System.out.println("HelloWorld init method...");
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		ServletConfig servletConfig = this.getServletConfig();
		// TODO Auto-generated method stub
		//擷取ServletContext上下文對象
		ServletContext servletContext = servletConfig.getServletContext();

		//擷取方式1
		String driver = servletContext.getInitParameter("user");
		String password = servletContext.getInitParameter("password");
		System.out.println("user is:"+driver + "----- get password is:" + password);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	
}
           

2.日志記錄

3.application域範圍的屬性

public class HelloWorld extends HttpServlet{

	private static final long serialVersionUID = 1L;

	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//模拟線上使用者
		List<String> userOnline = new ArrayList<String>();
		userOnline.add("admin");
		userOnline.add("張三");
		userOnline.add("李四");
		
		this.getServletContext().setAttribute("onlineUsers", userOnline);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	
}
           

在另一個Servlet中擷取設定的屬性值

public class Test extends HttpServlet{
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//擷取目前線上的使用者
		List<String> getOnlineUsers = (List<String>) this.getServletContext().getAttribute("onlineUsers");
		System.out.println("線上使用者有:" + getOnlineUsers);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

}
           

4.通路目前項目下的資源檔案

5.擷取虛拟路勁所映射的本地路勁

Sevlet_ServletContext
public class HelloWorld extends HttpServlet{

	private static final long serialVersionUID = 1L;

	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//擷取檔案絕對路勁
		String getFilePath = this.getServletContext().getRealPath("/File/Test.txt");
		System.out.println("檔案的路勁是:"+getFilePath);
		
		//檔案的路勁是:C:\Tomcat\webapps\0203\File\Test.txt


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

	
}
           

6.WEB應用程式之間的通路

7.ServletContext的其他方法

public class HelloWorld extends HttpServlet{

	private static final long serialVersionUID = 1L;

	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//擷取目前應用的名稱
		String getContext = this.getServletContext().getContextPath();
		System.out.println("擷取的目前的應用的名稱是:"+getContext);
		
		//輸出結果:擷取的目前的應用的名稱是:/0203

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

	
}