ServletConfig:表示servlet的配置信息,一個servlet對象對應一個servletconfig對象 方法: 1.獲取初始化參數 config.getInitParameter() ServletContext:表示servlet的全局配置信息,一個WebApplication只有 ...
ServletConfig:表示servlet的配置信息,一個servlet對象對應一個servletconfig對象 方法: 1.獲取初始化參數 config.getInitParameter() ServletContext:表示servlet的全局配置信息,一個WebApplication只有一個ServletContext對象,該對象被所有Servlet共用 方法: 1.獲取全局的初始化參數 context.getInitParameter() 2.獲取上下文路徑(部署在tomcat中的項目目錄名) context.getContextPath() 3.獲取文件的絕對路徑(從WebRoot下開始定位文件) context.getRealPath() 4.獲取資源,將資源作為流返回 context.getResourceAsStream(); 5.顯示目錄下的資源 Set<String> paths = sc1.getResourcePaths(); 6.存儲一個key-value數據 context.setAttribute(key,value) 根據key獲取value context.getAttribute(key) ServletConfig:
//web中 <init-param> <param-name>listings</param-name> <param-value>true</param-value> </init-param>
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.獲取ServletConfig對象 // 表示servlet的配置信息 ServletConfig config = this.getServletConfig(); //2.使用config讀取初始化參數 String value = config.getInitParameter("listings"); System.out.println(value); //獲取servletName System.out.println(config.getServletName()); } }
/*@Override public void init(ServletConfig config) throws ServletException { String value = config.getInitParameter("listings"); System.out.println(value); }*/
ServletContext
//web中 <context-param> <param-name>aaa</param-name> <param-value>bbb</param-value> </context-param>
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.獲取ServletContext:servlet的全局對象,整個項目就一個ServletContext對象 ServletContext sc1 = this.getServletContext(); ServletContext sc2 = request.getServletContext(); ServletContext sc3 = this.getServletConfig().getServletContext(); //2.使用該對象 //讀取全局配置信息 String val = sc1.getInitParameter("aaa"); System.out.println(val); //* 獲取上下文路徑 String contextPath = sc1.getContextPath(); System.out.println(contextPath); //* 獲取文件的絕對路徑:從WebRoot下開始定位文件 String realPath = sc1.getRealPath("/image/1.jpg"); System.out.println(realPath); //獲取資源,將資源作為流返回 InputStream in = sc1.getResourceAsStream("/WEB-INF/car.properties"); //顯示目錄下的資源 Set<String> paths = sc1.getResourcePaths("/aaa"); for (String string : paths) { System.out.println(string); } }
作用域對象 生命周期 作用範圍 HttpServletRequest 一次請求 一次請求經過的所有servlet HttpSession 一次會話 一次會話中,所有的servlet ServletContext 項目從載入到卸載 一個項目中,所有servlet 作用域對象,有三個方法: 1.setAttribute(String key,Object value); 2.getAttribute(String key); 3.removeAttribute(String key);
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.request對象 request.setAttribute("name", "zhangsan"); //request.getRequestDispatcher("scope2").forward(request, response); //2.session對象 先向瀏覽器輸入,在用下麵輸出 HttpSession session = request.getSession(); session.setAttribute("age", 18); //3.application對象 先向瀏覽器輸入,所有瀏覽器都可以輸出 ServletContext sc = request.getServletContext(); sc.setAttribute("gender", "男"); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.獲取request對象中數據 /*String name = (String) request.getAttribute("name"); System.out.println(name); //2.獲取session對象中的數據 int age = (int) request.getSession().getAttribute("age"); System.out.println(age);*/ //3.獲取application對象中的數據 String gender = (String) request.getServletContext().getAttribute("gender"); System.out.println(gender); }