Servlet配置方式java
ServletContext 獲取全局參數web
注意(每一個web工程都只有一個 ServletContext 對象。 說白了也就是無論在哪一個 servlet 裏面,獲取到的這個類的對象都是同一個。)apache
<context-param> <param-name>address</param-name> <param-value>重慶</param-value> </context-param> <servlet> <description></description> <display-name>ServletContext01</display-name> <servlet-name>ServletContext01</servlet-name> <servlet-class>com.heima.servlet.ServletContext01</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletContext01</servlet-name> <url-pattern>/ServletContext01</url-pattern> </servlet-mapping>
上面是 web.xml 中,下面是 測試類中,tomcat
注意:是全局參數,在任何測試類裏,均可以拿這個 address 參數.服務器
package com.heima.servlet; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletContext01 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 獲取對象 ServletContext context = getServletContext(); String address = context.getInitParameter("address"); System.out.println("address"+address); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
ServletContext 獲取資源文件(一)app
1. 獲取資源在tomcat裏面的絕對路徑 先獲得路徑,而後本身new InpuStream context.getRealPath("") //這裏獲得的是項目在tomcat裏面的根目錄。 D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\ String path = context.getRealPath("file/config.properties"); D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\file\config.properties
package com.heima.servlet; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class demo03 */ public class demo03 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲得 Servlet 對象 ServletContext context = getServletContext(); //得到指定文件在服務器上的絕對路徑 String path = context.getRealPath("file/config.properties"); //什麼都不寫是根目錄 System.out.println("path"+path); //1. 建立屬性對象 Properties properties = new Properties(); InputStream is = new FileInputStream(path); properties.load(is); //3. 獲取name屬性的值 String name = properties.getProperty("name"); System.out.println("name="+name); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
eclipse 快捷鍵使用小貼士eclipse
Alt+Shift+M抽取方法 MyEclipse、Eclipse 調整格式快捷鍵,ctrl+shift+F 選中你要try的代碼,alt+shift+z 就會彈出一個菜單
ServletContext 獲取資源文件(二)webapp
2. getResourceAsStream 獲取資源 流對象 直接給相對的路徑,而後獲取流對象。
private void text02(){ try { // 獲得 Servlet 對象 ServletContext context = getServletContext(); // 1. 建立屬性對象 Properties properties = new Properties(); //獲取 web 下的資源轉化成流對象,前面隱藏這當前工程的根目錄。 InputStream is = context.getResourceAsStream("file/config.properties"); properties.load(is); // 3. 獲取name屬性的值 String name = properties.getProperty("name"); System.out.println("name22=" + name); is.close(); } catch (Exception e) { e.printStackTrace(); } }